脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 如何使用yolov5输出检测到的目标坐标信息

如何使用yolov5输出检测到的目标坐标信息

2022-11-14 10:42一位安分的码农 Python

YOLOv5是一系列在 COCO 数据集上预训练的对象检测架构和模型,下面这篇文章主要给大家介绍了关于如何使用yolov5输出检测到的目标坐标信息的相关资料,需要的朋友可以参考下

找到detect.py,在大概113行,找到plot_one_box

?
1
2
3
4
5
6
7
8
9
10
                # 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函数,修改函数为

?
1
2
3
4
5
6
7
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]) + ")")

即可输出目标坐标信息了

如何使用yolov5输出检测到的目标坐标信息

附:python yolov5检测模型返回坐标的方法实例代码

python yolov5检测模型返回坐标的方法 直接搜索以下代码替换下 

?
1
2
3
4
5
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]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);

总结

到此这篇关于如何使用yolov5输出检测到的目标坐标信息的文章就介绍到这了,更多相关yolov5输出目标坐标内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_44612221/article/details/115384742

延伸 · 阅读

精彩推荐
  • PythonWin 10下Anaconda虚拟环境的教程

    Win 10下Anaconda虚拟环境的教程

    这篇文章主要介绍了Win 10下Anaconda虚拟环境的相关知识,本文通过实例截图相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,...

    tangu0ping9482020-05-19
  • Pythonpython中实现指定时间调用函数示例代码

    python中实现指定时间调用函数示例代码

    函数function是python编程核心内容之一,也是比较重要的一块。下面这篇文章主要给大家介绍了关于python中实现指定时间调用函数的相关资料,文中通过示例代...

    caimouse4852020-12-07
  • Pythonpython基于tkinter点击按钮实现图片的切换

    python基于tkinter点击按钮实现图片的切换

    这篇文章主要介绍了python基于tkinter点击按钮实现图片的切换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    Chauncey_Wang4832021-10-07
  • PythonPython教程自己实现软件加密功能

    Python教程自己实现软件加密功能

    网上的加密软件不太安全,但对于日常学习而言,我们可以借助异或操作,实现一个简单的文件加密程序,从而强化自身的编程能力...

    燕山58810692022-01-18
  • Python10分钟教你用Python实现微信自动回复功能

    10分钟教你用Python实现微信自动回复功能

    今天,我们就来用Python实现微信的自动回复功能吧,并且把接收到的消息统一发送到文件助手里面,方便统一查看。感兴趣的朋友跟随小编一起看看吧...

    短短的路走走停停6682021-04-22
  • Pythonpython实现高效的遗传算法

    python实现高效的遗传算法

    这篇文章主要介绍了python实现高效的遗传算法。想了解算法的同学,可以参考下...

    哇哇咔咔MZ:y12302021-10-06
  • Pythonpython实现简单的名片管理系统

    python实现简单的名片管理系统

    这篇文章主要为大家详细介绍了python实现名片管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    奈落~3742021-10-20
  • Python浅谈python在提示符下使用open打开文件失败的原因及解决方法

    浅谈python在提示符下使用open打开文件失败的原因及解决方法

    今天小编就为大家分享一篇浅谈python在提示符下使用open打开文件失败的原因及解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看...

    雾幻6602021-04-24