如何通过YOLACT保存有关实例分割结果的信息?

2024-04-19 21:37:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用YOLACT执行实例分割时,是否有任何方法将检测到的类别、它们的编号、掩码区域等保存到TXT文件或CSV文件

我使用YOLACT(https://github.com/dbolya/yolact)来挑战实例分割。 我能够使用eval.py对自己的数据进行实例分割,并保存图像或视频。 然而,我真正需要的是由YOLACT的AI检测和分类的类名及其编号,以及​​面具 如果我们可以将这些信息输出到txt文件或csv文件,我们就可以使用YOLACT更高级

如果我可以通过在eval.py中添加选项或修改代码来实现,请教我

多谢各位


Tags: 文件csv实例方法pyhttpsgithubtxt
1条回答
网友
1楼 · 发布于 2024-04-19 21:37:23

您已经从eval.py中获得了该信息

eval.py中的这一行提供了信息

# line 160
classes, scores, boxes = [x[:args.top_k].cpu().numpy() for x in t[:3]]
masks = t[3][:args.top_k]

areas = []

for mask in masks:
    # Use OpenCV's findContours() to detect and obtain the contour of the variable mask
    # Use OpenCV's contourArea() to calculate the area
    areas.append(area)

获取检测数量:

# line 162
num_dets_to_consider = min(args.top_k, classes.shape[0])
    for j in range(num_dets_to_consider):
        if scores[j] < args.score_threshold:
            num_dets_to_consider = j
            break

要将课程和分数保存为csv文件,请执行以下操作:

import pandas as pd

df = pd.DataFrame(columns=['class', 'score'])
c = 0
for i in reversed(range(num_dets_to_consider)):
    classname = cfg.dataset.class_names[classes[j]]
    score = scores[i]
    df.loc[c] = [classname, score]
    c += 1
df.to_csv('info.csv', index=None)

编辑1: 您可以从prep_display()返回值(类、分数、区域),然后在evalimage()中检索它们。请注意,evalimage()调用prep_display()。您可以这样做:

classes, scores, areas = prep_display()
# Here you can include the pandas code. Note that it stores the information only for one image. You can use a loop and then store the information of all the images.

编辑2:

# This is the default line in eval.py at line ~600
# This is found at evalimage()
img_numpy = prep_display(preds, frame, None, None, undo_transform=False)

# Change the above line to this:
classes, scores, areas, img_numpy = prep_display(preds, frame, None, None, undo_transform=False)

# Also, remember to return these 4 values from the prep_display(). 
# Otherwise, it will lead to an error. 
# img_numpy is the default one that will be returned from prep_display(). 
# You're now simply returning 4 values instead of 1.

# NOTE: I have included img_numpy because I found that prep_display() is being used in many places. 
# You need to modify the returns wherever prep_display() is being called in the eval.py. I think it's being called 3 times from different regions in eval.py.

相关问题 更多 >