如何使用python实现目标检测给图画框,bbox画到图上并保存案例

发布时间:2021-07-02 10:31:00 作者:小新
来源:亿速云 阅读:141

这篇文章主要介绍了如何使用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画到图上并保存案例”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. php多图上传保存路径的方法
  2. python对视频画框标记后保存的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Python爬虫程序架构和运行的示例分析

下一篇:在Mac中PyCharm如何配置python Anaconda环境

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》