铁雪资源网 Design By www.gsvan.com
我就废话不多说了,还是直接上代码吧!
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画到图上并保存案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com
暂无python目标检测给图画框,bbox画到图上并保存案例的评论...