網站首頁 編程語言 正文
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
相關推薦
- 2022-07-20 react中事件處理與柯里化的實現_React
- 2021-12-01 C++?解決求兩個鏈表的第一個公共結點問題_C 語言
- 2022-06-18 Elasticsearch之文檔批量操作示例_其它綜合
- 2022-05-28 C語言數據結構深入探索順序表_C 語言
- 2022-06-09 ASP.NET?Core使用EF創建關系模型_實用技巧
- 2022-04-12 Python實現數據地址實體抽取_python
- 2022-11-27 C語言移除元素的三種思路講解_C 語言
- 2022-09-26 Josephus_problem_bidirectional 雙向約瑟夫問題
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支