yolov5返回坐标的方法实例
作者:weixin_44726793 发布时间:2023-10-05 20:09:43
标签:yolov5,坐标,输出
yolov5返回坐标(v6版)
1 、从yolov5文件夹李找到detect.py,按Ctrl+F 输入annotator.box_label;
if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
annotator.box_label(xyxy, label, color=colors(c, True))
2、找到这个代码后按住ctrl键,鼠标点击box_label,就会跳到plots.py文件并定位到box_label定义的地方;
3、找到p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),在这行代码下面新增:
print("左上点的坐标为:(" + str(p1[0]) + "," + str(p1[1]) + "),右下点的坐标为(" + str(p2[0]) + "," + str(p2[1]) + ")")
4、完成后的代码如下:
def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
# Add one xyxy box to image with label
if self.pil or not is_ascii(label):
self.draw.rectangle(box, width=self.lw, outline=color) # box
if label:
w, h = self.font.getsize(label) # text width, height
outside = box[1] - h >= 0 # label fits outside box
self.draw.rectangle([box[0],
box[1] - h if outside else box[1],
box[0] + w + 1,
box[1] + 1 if outside else box[1] + h + 1], fill=color)
# self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0
self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
else: # cv2
p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
print("左上点的坐标为:(" + str(p1[0]) + "," + str(p1[1]) + "),右下点的坐标为(" + str(p2[0]) + "," + str(p2[1]) + ")")
cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
5、测试情况:回到命令行,cd到yolov5文件夹,输入指令:python detect.py --source ../mask.1.jpg,其中mask.1.jpg应改为你yolov5文件夹下的图片名称;按回车键后运行就发现输出的信息多了刚才添加的一行
(venv) (base) rongxiao@rongxiao:~/PycharmProjects/yolococo/yolov5$ python detect.py --source ../mask.1.jpg
detect: weights=yolov5s.pt, source=../mask.1.jpg, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs/detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
YOLOv5 🚀 v6.0-147-g628817d torch 1.8.2+cpu CPU
Fusing layers...
Model Summary: 213 layers, 7225885 parameters, 0 gradients
左上点的坐标为:(982,384),右下点的坐标为(1445,767)
左上点的坐标为:(724,237),右下点的坐标为(770,277)
左上点的坐标为:(711,226),右下点的坐标为(1689,938)
image 1/1 /home/rongxiao/PycharmProjects/yolococo/mask.1.jpg: 384x640 2 persons, 1 airplane, Done. (0.182s)
Speed: 1.1ms pre-process, 181.7ms inference, 1.0ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs/detect/exp15
附参考: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]) + ")")
即可输出目标坐标信息了
来源:https://blog.csdn.net/weixin_44726793/article/details/122022467


猜你喜欢
- async官方DOC介绍node安装npm install async --save使用var async = require('a
- 聚集索引,数据实际上是按顺序存储的,数据页就在索引页上。就好像参考手册将所有主题按顺序编排一样。一旦找到了所要搜索的数据,就完成了这次搜索,
- 前言本文主要介绍属性、事件和插槽这三个vue基础概念、使用方法及其容易被忽略的一些重要细节。如果你阅读别人写的组件,也可以从这三个部分展开,
- 今天早上用pycharm启动django工程的时候,一直卡在如下提示:Performing system checks...System c
- 1.使用npm进行初始化在本地创建项目的文件夹名称,如 node_test,并在该文件夹下进行黑窗口执行初始化命令 2. 安装 e
- 用golang来实现的webserver通常是是这样的//main.gopackage mainimport ("fmt"
- 暴力的重启服务方案一般服务器重启可以直接通过 kill 命令杀死进程,然后重新启动一个新的进程即可。但这种方法比较粗暴,有可能导致某些正在处
- 之前看到过很多头条,说哪国某人坚持了多少年自学使用excel画画,效果十分惊艳。 对于他们的耐心我十分敬佩。 但是作为一个程序员,自然也得挑
- 基础知识在关系型数据库中每一个数据表相当于一个文件,而不同的存储引擎则会构建出不同的表类型。存储引擎的作用是规定数据表如何存储数据,如何为存
- 过去一段时间人们似乎又非常热衷于探讨网络文档的印刷格式,涌现了很多与之相关的技术与理论资料,其中相当重要的一个领域就是关于印刷中字号和行高的
- 实时读取logstash日志,有异常错误keywork即触发报警。# /usr/bin/env python3# -*- coding: u
- 特么的,上次写了一堆,发现,原来下载网易云的歌曲根本不用这么费劲,直接用!http://music.163.com/song/media/o
- 一、绑定class属性的方式1、通过数组的方式,为元素绑定多个class<style> .red {
- 一行代码实现灰度图抠图抠图是ps的最基本技能,利用python可以实现用一行代码实现灰度图抠图。基础算法是确定图像二值化分割阈值的大津法,将
- 在深度学习中,模型的输入size通常是正方形尺寸的,比如300 x 300这样.直接resize的话,会把图像拉的变形.通常我们希望resi
- Redisredis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串
- 加上这句代码:print torch.cuda.is_available()判断完毕!说说在pytorch中如何查看gpu信息吧~为什么将数
- 从接触互联网开始,一直在关注每个网站页面浏览速度,不管大小国内外网站,而且每个用户的浏览时间都不一样,从而了解用户在浏览页面上时间体验尺度的
- osql 工具是一个 Microsoft Windows 32 命令提示符工具,您可以使用它运行 Transact-SQL 语句和脚本文件。
- 转眼间上次写文章已经是 2022年12月15日的事情啦,本来从2022年7月份开始写作之后保持着每周一篇,然而从12月15日后断更了这么久,