参考:yolov5(pytorch)实现目标检测后输出检测框的位置信息
修改detect.py文件
在如下位置添加:
x1 = int(xyxy[0].item())
y1 = int(xyxy[1].item())
x2 = int(xyxy[2].item())
y2 = int(xyxy[3].item())
class_index = cls # 获取属性
object_name = names[int(cls)]
#print('bounding box is', x1, y1, x2, y2) # 打印坐标
xxxx1 = (x1+x2)/2
yyyy1 = (y1+y2)/2
print(label, '的预测匡中心点坐标:', '(',xxxx1,yyyy1,')') # 打印坐标
# print('class index is',class_index.item())#打印属性,由于我们只有一个类,所以是0
# print('object_names is',object_name)#打印标签名字,
# --------------写入csv-----------------#
import csv
data = [[str(label), str(xxxx1), str(yyyy1)]]
with open('坐标位置.csv', 'a', encoding='utf-8', newline='') as fp:
# 写
writer = csv.writer(fp)
# 将数据写入
writer.writerows(data)
(上面预测匡中心点坐标不知道是那样算的不)
注意注释调上面两行
#if save_crop:
# save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
运行测试命令:
python3 detect.py --source 0