yolov5返回坐標(biāo)的方法實(shí)例
yolov5返回坐標(biāo)(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、找到這個(gè)代碼后按住ctrl鍵,鼠標(biāo)點(diǎn)擊box_label,就會(huì)跳到plots.py文件并定位到box_label定義的地方;
3、找到p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),在這行代碼下面新增:
print("左上點(diǎn)的坐標(biāo)為:(" + str(p1[0]) + "," + str(p1[1]) + "),右下點(diǎn)的坐標(biāo)為(" + 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("左上點(diǎn)的坐標(biāo)為:(" + str(p1[0]) + "," + str(p1[1]) + "),右下點(diǎn)的坐標(biāo)為(" + str(p2[0]) + "," + str(p2[1]) + ")") cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
5、測(cè)試情況:回到命令行,cd到y(tǒng)olov5文件夾,輸入指令:python detect.py --source ../mask.1.jpg,其中mask.1.jpg應(yīng)改為你yolov5文件夾下的圖片名稱;按回車鍵后運(yùn)行就發(fā)現(xiàn)輸出的信息多了剛才添加的一行
(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 左上點(diǎn)的坐標(biāo)為:(982,384),右下點(diǎn)的坐標(biāo)為(1445,767) 左上點(diǎn)的坐標(biāo)為:(724,237),右下點(diǎn)的坐標(biāo)為(770,277) 左上點(diǎn)的坐標(biāo)為:(711,226),右下點(diǎn)的坐標(biāo)為(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輸出檢測(cè)到的目標(biāo)坐標(biāo)信息(舊版本)
找到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+鼠標(biāo)點(diǎn)擊,進(jìn)入general.py,并自動(dòng)定位到plot_one_box函數(shù),修改函數(shù)為
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("左上點(diǎn)的坐標(biāo)為:(" + str(c1[0]) + "," + str(c1[1]) + "),右下點(diǎn)的坐標(biāo)為(" + str(c2[0]) + "," + str(c2[1]) + ")")
即可輸出目標(biāo)坐標(biāo)信息了
總結(jié)
到此這篇關(guān)于yolov5返回坐標(biāo)的文章就介紹到這了,更多相關(guān)yolov5返回坐標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python OpenCV實(shí)現(xiàn)人物動(dòng)漫化效果
這篇文章主要介紹了利用Python和OpenCV實(shí)現(xiàn)人物的動(dòng)漫化特效,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python和OpenCV有一定的幫助,需要的可以了解一下2022-01-01Python中DataFrame判斷兩列數(shù)據(jù)是否相等的方法
本文主要介紹了DataFrame判斷兩列數(shù)據(jù)是否相等的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04解決Shell執(zhí)行python文件,傳參空格引起的問題
今天小編就為大家分享一篇解決Shell執(zhí)行python文件,傳參空格引起的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10python Pandas中數(shù)據(jù)的合并與分組聚合
大家好,本篇文章主要講的是python Pandas中數(shù)據(jù)的合并與分組聚合,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Python函數(shù)高級(jí)(命名空間、作用域、裝飾器)
這篇文章介紹了Python函數(shù)的高級(jí)用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05scrapy中的spider傳參實(shí)現(xiàn)增量的方法
有時(shí)候需要根據(jù)項(xiàng)目的實(shí)際需求向spider傳遞參數(shù)來控制spider的運(yùn)行方式,本文主要介紹了scrapy中的spider傳參實(shí)現(xiàn)增量的方法,具有一定的參考價(jià)值,感興趣的可以了解一下2022-06-06Python實(shí)現(xiàn)輸出程序執(zhí)行進(jìn)度百分比的方法
這篇文章主要介紹了Python實(shí)現(xiàn)輸出程序執(zhí)行進(jìn)度百分比的方法,涉及Python數(shù)值運(yùn)算與系統(tǒng)輸出相關(guān)操作技巧,需要的朋友可以參考下2017-09-09基于Python獲取docx/doc文件內(nèi)容代碼解析
這篇文章主要介紹了基于Python獲取docx/doc文件內(nèi)容代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02