yolov5记录检测框中心坐标
最近用到yolov5进行目标检测并确定目标在图片中的位置,直接上代码:
第一步在detect.py中建立文件夹并对每张图片建立txt文件:
location_center_dir = str(save_dir) + '/location_center'
if not os.path.exists(location_center_dir):
os.makedirs(location_center_dir)
location_center_path = location_center_dir + '\\' + str(p.stem) + ('' if dataset.mode == 'image' else f'_{frame}')
flocation = open(location_center_path + '.txt', 'a')
将代码添加到图中位置:
第二步写入坐标:
x0 = (int(xyxy[0].item()) + int(xyxy[2].item()))/2
y0 = (int(xyxy[1].item()) + int(xyxy[3].item()))/2
class_index = cls
object_name = names[int(cls)]
flocation.write(object_name + ': ' + str(x0) + ', ' + str(y0) + '\n')
添加到图中位置:
不要忘了关闭文件。
效果:
在预测文件夹exp中新建文件夹记录每个图片检测到的目标中心位置:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)