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
0
投稿
猜你喜欢
- MSDN:包含正在 SQL Server 实例上运行的进程的相关信息。这些进程可以是客户端进程或系统进程。 视图中主要的字段: 1. Spi
- 一、图片验证码django-simple-captcha配置1.在pycharm中,File====》Settings====》Projec
- Pyppeteer简介介绍Pyppeteer之前先说一下Puppeteer,Puppeteer是谷歌出品的一款基于Node.js开发的一款工
- 搜索是大数据领域里常见的需求。Splunk和ELK分别是该领域在非开源和开源领域里的领导者。本文利用很少的Python代码实现了一个基本的数
- 什么是正则表达式?1、正则表达式是检擦、匹配字符串的表达式2、正则表达式是描述规则,主流语言都有良好支持3、字符串校验、查找与替换是正则表达
- 代码如下:<% myDSN="DSN=xur;uid=xur;pwd=xur"mySQL="s
- 1、我的源码在 /home/topsec/Documents/php-7.0.11 ,安装位置在 /usr/local/php7, php.
- Summary主要包括以下三种途径:使用独立的函数;使用torch.type()函数;使用type_as(tesnor)将张量转换为给定类型
- 在运行Go语言程序之前,先要将其编译成二进制的可执行文件我们通常在编辑器编写完源码后使用go build或go run命令对GO语言程序进行
- 大家都在关注视觉的盛宴,西方的美学;今天就分享下,中国最为古老的美,也是身边随处可见的美学–中国汉字书法之美;古文者,仓颉做造也。仰观奎星园
- 假如你目前需要在Microsoft Access数据库中指示不存在数据,可以在“文本”或&l
- CAPTCHA,全称为“Completely Automated Public Turing test to tell Computers
- 对所有数据进行整合与管理当你使用SQL Server 2008企业级的数据仓库平台时,你可以高效的操纵所有数据,并对其进行统一管理存储。◆合
- Python中的中文是个很头痛的问题,Python2和Python3都会出现,而且py2中出现的概率要大些。 有一道面试题: Py
- 作为一个非设计出生的研究思路偏向的设计师,带着少了设计师自恋和自怜的眼光,我记下最近一年来感受到的交互设计师的尴尬。交互设计师就是出界面的,
- 见下:<% FOR i = 1 TO 1000 n =
- 一. 建库,建表,加约束. 1.1建库 代码如下:use master go if exists (select * from sysdat
- 1 Git简介Git 是目前世界上最先进的分布式版本控制系统(没有之一)作用: 源代码管理为什么要进行源代码管理?
- 本文介绍了linux下如何备份与恢复mysql数据库。数据库备份是非常重要的。如果定期做好备份,这样就可以在发生系统崩溃时恢复数据到最后一次
- key_buffer_size - 这对MyISAM表来说非常重要。如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%。合理