python目标检测给图画框,bbox画到图上并保存案例

所属分类: 脚本专栏 / python 阅读数: 802
收藏 0 赞 0 分享

我就废话不多说了,还是直接上代码吧!

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画到图上并保存案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多