如何使用yolov5输出检测到的目标坐标信息
作者:一位安分的码农 时间:2021-07-12 17:25:14
找到detect.py,在大概113行,找到plot_one_box
# Write results
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
if save_img or view_img: # Add bbox to image
label = '%s %.2f' % (names[int(cls)], conf)
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
ctr+鼠标点击,进入general.py,并自动定位到plot_one_box函数,修改函数为
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
# Plots one bounding box on image img
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
print("左上点的坐标为:(" + str(c1[0]) + "," + str(c1[1]) + "),右下点的坐标为(" + str(c2[0]) + "," + str(c2[1]) + ")")
即可输出目标坐标信息了
附:python yolov5检测模型返回坐标的方法实例代码
python yolov5检测模型返回坐标的方法 直接搜索以下代码替换下
if save_img or view_img: # Add bbox to image
label = f'{names[int(cls)]} {conf:.2f}'
c1, c2 = (int(xyxy[0]), int(xyxy[1])), (int(xyxy[2]), int(xyxy[3]))
print("左上点的坐标为:(" + str(c1[0]) + "," + str(c1[1]) + "),右下点的坐标为(" + str(c2[0]) + "," + str(c2[1]) + ")")
return [c1,c2]
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
parser.add_argument('--source', type=str, default='data/images', help='source') # file/folder, 0 for webcam
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='display results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default='runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
opt = parser.parse_args()
check_requirements(exclude=('pycocotools', 'thop'))
opt.source='data/images/1/'
result=detect()
print('最终检测结果:',result);
来源:https://blog.csdn.net/weixin_44612221/article/details/115384742
标签:yolov5,目标,坐标
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
int在python中的含义以及用法
2023-07-28 08:48:21
css布局查看器
2008-10-29 11:22:00
百度地图API之本地搜索与范围搜索
2023-08-23 17:24:38
![](https://img.aspxhome.com/file/2023/6/56176_0s.jpg)
网页设计三剑客
2010-08-31 17:05:00
如何让用户再次访问我的网站时不需再提交相关信息?
2010-05-16 15:05:00
保护你的ASP页面的两种办法
2008-06-10 16:53:00
Python实现从多表格中随机抽取数据
2022-07-01 01:58:18
![](https://img.aspxhome.com/file/2023/1/83931_0s.png)
pycharm sciview的图片另存为操作
2022-09-26 08:03:07
![](https://img.aspxhome.com/file/2023/6/79606_0s.jpg)
Golang实现断点续传功能
2023-07-24 08:19:26
![](https://img.aspxhome.com/file/2023/8/99058_0s.jpg)
数据库复制性能测试 推送模式性能测试
2012-07-11 16:13:52
python获取一组汉字拼音首字母的方法
2023-04-08 16:43:02
python实现批量转换文件编码(批转换编码示例)
2023-07-28 22:03:47
IE6局部调用PNG32合并图片
2009-03-11 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-12s.jpg)
IE6,IE7中定位相关的怪异问题
2009-12-08 12:49:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/position_demo1-45s.png)
Jupyter notebook中5个有趣的魔法命令分享
2021-11-28 23:26:30
![](https://img.aspxhome.com/file/2023/5/78695_0s.png)
OpenCV实现人脸识别
2023-01-07 02:38:58
aspjpeg组件安装问题
2008-09-27 17:52:00
白鸦:Design IT. (1),迭代的设计
2008-08-27 21:11:00
![](https://img.aspxhome.com/file/UploadPic/20088/27/2008827211653577s.png)
asp MYSQL出现问号乱码的解决方法
2011-04-15 11:13:00
将string类型的数据类型转换为spark rdd时报错的解决方法
2023-05-03 00:35:12