您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用python實現目標檢測給圖畫框,bbox畫到圖上并保存案例,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
代碼如下
import os import xml.dom.minidom import cv2 as cv ImgPath = 'C:/Users/49691/Desktop/gangjin/gangjin_test/JPEGImages/' AnnoPath = 'C:/Users/49691/Desktop/gangjin/gangjin_test/Annotations/' #xml文件地址 save_path = '' def draw_anchor(ImgPath,AnnoPath,save_path): imagelist = os.listdir(ImgPath) for image in imagelist: image_pre, ext = os.path.splitext(image) imgfile = ImgPath + image xmlfile = AnnoPath + image_pre + '.xml' # print(image) # 打開xml文檔 DOMTree = xml.dom.minidom.parse(xmlfile) # 得到文檔元素對象 collection = DOMTree.documentElement # 讀取圖片 img = cv.imread(imgfile) filenamelist = collection.getElementsByTagName("filename") filename = filenamelist[0].childNodes[0].data print(filename) # 得到標簽名為object的信息 objectlist = collection.getElementsByTagName("object") for objects in objectlist: # 每個object中得到子標簽名為name的信息 namelist = objects.getElementsByTagName('name') # 通過此語句得到具體的某個name的值 objectname = namelist[0].childNodes[0].data bndbox = objects.getElementsByTagName('bndbox') # print(bndbox) for box in bndbox: x1_list = box.getElementsByTagName('xmin') x1 = int(x1_list[0].childNodes[0].data) y1_list = box.getElementsByTagName('ymin') y1 = int(y1_list[0].childNodes[0].data) x2_list = box.getElementsByTagName('xmax') #注意坐標,看是否需要轉換 x2 = int(x2_list[0].childNodes[0].data) y2_list = box.getElementsByTagName('ymax') y2 = int(y2_list[0].childNodes[0].data) cv.rectangle(img, (x1, y1), (x2, y2), (255, 255, 255), thickness=2) cv.putText(img, objectname, (x1, y1), cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), thickness=2) # cv.imshow('head', img) cv.imwrite(save_path+'/'+filename, img) #save picture
補充知識:深度學習python之用Faster-rcnn 檢測結果(txt文件) 在原圖畫出box
使用Faster-rcnn 的test_net.py 檢測網絡的mAP等精度會生成一個檢測結果(txt文件),格式如下:
000004 0.972 302.8 94.5 512.0 150.0 000004 0.950 348.1 166.1 512.0 242.9 000004 0.875 1.0 25.7 292.6 126.3 000004 0.730 1.0 138.5 488.3 230.0 000004 0.699 1.0 120.9 145.5 139.9 000004 0.592 54.4 227.4 431.9 343.4 000004 0.588 1.0 159.8 18.8 231.6 000004 0.126 1.0 247.1 342.3 270.0 000004 0.120 1.0 225.4 185.7 309.3
每行分別為 名稱 檢測概率 xmin ymin xmax ymax
問題在于每一行只顯示一個box數據,每幅圖像可能包括多個box,需要判斷提取的多行數據是不是屬于同一圖片
下面使用python提取這些數據,在原圖上畫出box并且保存起來
import os import os.path import numpy as np import xml.etree.ElementTree as xmlET from PIL import Image, ImageDraw import cPickle as pickle txt_name = 'comp4_8a226fd7-753d-40fc-8013-f68d2a465579_det_test_ship.txt' file_path_img = '/home/JPEGImages' save_file_path = '/home/detect_results' source_file = open(txt_name) img_names = [] for line in source_file: staff = line.split() img_name = staff[0] img_names.append(img_name) name_dict = {} for i in img_names: if img_names.count(i)>0: name_dict[i] = img_names.count(i) source_file.close() source_file = open(txt_name) for idx in name_dict: img = Image.open(os.path.join(file_path_img, idx + '.jpg')) draw = ImageDraw.Draw(img) for i in xrange(name_dict[idx]): line = source_file.readline() staff = line.split() score = staff[1] box = staff[2:6] draw.rectangle([int(np.round(float(box[0]))), int(np.round(float(box[1]))), int(np.round(float(box[2]))), int(np.round(float(box[3])))], outline=(255, 0, 0)) img.save(os.path.join(save_file_path, idx + '.jpg')) source_file.close()
運行完即可在保存文件夾中得到效果圖。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何使用python實現目標檢測給圖畫框,bbox畫到圖上并保存案例”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。