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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python进行目标检测时,可以通过调用相应的目标检测模型和库来检测像中的目标,并将检测到的目标用边界(bbox)标记出来,然后将标记后的保存下来。 首先,我们需要导入所需的库和模型。常用的目标检测库包括OpenCV、TensorFlow Object Detection API、Detectron等。根据使用的库不同,导入的方式也会有所区别。以下是使用OpenCV和CascadeClassifier进行目标检测和边界标记的示例代码: ```python import cv2 # 加载目标检测模型 cascade = cv2.CascadeClassifier('path/to/your/model.xml') # 读取像 image = cv2.imread('path/to/your/image.jpg') # 转为灰度像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 目标检测 bbox = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # 在像上画出边界保存 for (x, y, w, h) in bbox: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) # 保存标记后的cv2.imwrite('path/to/your/save/image.jpg', image) ``` 上述代码中,我们首先加载了目标检测模型,并读取了要检测的像。然后,将像转为灰度像,然后调用目标检测模型对像进行检测。检测到的目标的边界信息存储在bbox中。 接下来,我们使用循环对每个检测到的目标绘制边界,并设置边界的线条颜色、宽度等参数。最后,将标记后的保存到指定路径。 需要注意的是,不同的目标检测模型输出的bbox格式可能有所差异,有些模型可能输出的是(x, y, w, h),而有些模型可能输出的是(xmin, ymin, xmax, ymax)。根据具体情况进行适当的调整。 以上就是用python进行目标检测,并将bbox画到像上并保存的简单示例。具体的实现方式会根据使用的库、模型及其调用方式的不同而有所变化,请根据实际情况进行具体实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值