如何使用yolov5输出检测到的目标坐标信息
作者:一位安分的码农 发布时间:2021-07-12 17:25:14
标签:yolov5,目标,坐标
找到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


猜你喜欢
- 数据加载分为加载torchvision.datasets中的数据集以及加载自己使用的数据集两种情况。torchvision.datasets
- 这篇文章主要介绍了基于Python实现ComicReaper漫画自动爬取脚本过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具
- 场景报修范围取值不同时 ,区域有时需要禁用代码<el-form-item label="报修范围" prop=&q
- 在windows操作系统上使用IE作为浏览器时。常常会发生这样的问题:在浏览使用UTF-8编码的网页时,浏览器无法自动侦测(即没有设定“自动
- 1. TVP, 表变量,临时表,CTE 的区别 TVP和临时表都是可以索引的,总是存在tempdb中,会增加系统数据库开销,而表变量和CTE
- 很多开发人员在刚开始学Python 时,都考虑过像 c++ 那样来实现 singleton 模式,但后来会发现 c++ 是 c++,Pyth
- 我要一大群的类都具有一 * 点,我怎么给他们加上呢?模板模板吗,我从这个模板创建一群类不就OK了?那就需要元类了。霍霍》 定义一个元类(就所一
- 关于本文:本着互联网共享主义精神,特写此文献给建站新手,授人以鱼,不如授人以渔,本文所讲的只是方法和原理,希望大家看完此文,能够从中得到些帮
- 初级的图像拼接为将两幅图像简单的粘贴在一起,仅仅是图像几何空间的转移与合成,与图像内容无关。高级图像拼接也叫作基于特征匹配的图像拼接,拼接时
- 前言小伙伴a,b,c围着吃火锅,当菜上齐了,请客的主人说:开吃!,于是小伙伴一起动筷子,这种场景如何实现Event(事件)Event(事件)
- 本文实例为大家分享了python webp图片格式转化的具体代码,供大家参考,具体内容如下1、将本地的webp图片转换为jpg2、将下载的w
- 前言上次写博客还在去年的8月底了,期间有了小宝,换工作等诸多事宜让我踩坑采的起飞,时隔4个月,逐渐找回状态。这篇的主题是python的第三方
- Python2默认是不支持中文的,一般我们在程序的开头加上#-*-coding:utf-8-*-来解决这个问题,但是在我用open()方法打
- 一、触发器的介绍触发器是与表有关的数据库对象,指在insert/update/delete 之前或之后,触发并执行触发器中定义的SQL语句集
- 本文介绍了目前6种比较常用的进度条,让大家都能直观地看到脚本运行最新的进展情况1.普通进度条在代码迭代运行中可以自己进行统计计算,并使用格式
- 本文中,abigale代表查询字符串,ada代表数据表名,alice代表字段名。技巧一:问题类型:ACCESS数据库字段中含有日文片假名或其
- 做开发中难免时间类型之间的转换, 最近就发现前端js和后端django经常要用到这个转换, 其中jsDate.now()精确到毫秒,而Pyt
- /usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysqlunzip mysql-5.
- 一、实例代码HTML:<!DOCTYPE html><html lang="en"><he
- 前言 :上一篇文章:如何使用python生成大量数据写入es数据库并查询操作模拟学生个人信息写入es数据库,包括姓名、性别、年龄、特点、科目