将yolov5中val.py中save_txt函数改为根据置信度大小不同存入不同的excel
时间: 2024-02-28 22:56:19 浏览: 101
好的,你可以将以下代码替换val.py中的save_txt函数,实现根据置信度大小将结果存入不同的excel文件中:
```python
def save_txt(save_dir, img, detections, conf_thres=0.5):
# Create directory
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Get image size
img_size = img.shape[2]
# Create excel files for different confidence levels
high_conf_f = openpyxl.Workbook()
high_conf_sheet = high_conf_f.active
high_conf_sheet.append(['class', 'confidence', 'xmin', 'ymin', 'xmax', 'ymax'])
med_conf_f = openpyxl.Workbook()
med_conf_sheet = med_conf_f.active
med_conf_sheet.append(['class', 'confidence', 'xmin', 'ymin', 'xmax', 'ymax'])
low_conf_f = openpyxl.Workbook()
low_conf_sheet = low_conf_f.active
low_conf_sheet.append(['class', 'confidence', 'xmin', 'ymin', 'xmax', 'ymax'])
# Iterate through detections and save to the appropriate excel file based on confidence level
for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:
if cls_conf > conf_thres:
if cls_conf >= 0.9:
high_conf_sheet.append([classes[int(cls_pred)], cls_conf.item(), x1.item(), y1.item(), x2.item(), y2.item()])
elif cls_conf >= 0.5:
med_conf_sheet.append([classes[int(cls_pred)], cls_conf.item(), x1.item(), y1.item(), x2.item(), y2.item()])
else:
low_conf_sheet.append([classes[int(cls_pred)], cls_conf.item(), x1.item(), y1.item(), x2.item(), y2.item()])
# Save excel files
high_conf_f.save(os.path.join(save_dir, 'high_conf.xlsx'))
med_conf_f.save(os.path.join(save_dir, 'med_conf.xlsx'))
low_conf_f.save(os.path.join(save_dir, 'low_conf.xlsx'))
}
```
这个函数会在指定的目录中创建三个不同的excel文件,分别存储置信度高、中、低的检测结果。你可以调整`conf_thres`参数来控制置信度的阈值。记得要安装`openpyxl`库,否则无法运行。
阅读全文