当前位置:网站首页>从标注好的xml文件中截取坐标点(人脸框四个点坐标)人脸图像并保存在指定文件夹
从标注好的xml文件中截取坐标点(人脸框四个点坐标)人脸图像并保存在指定文件夹
2022-07-26 13:20:00 【付付要努力呀】
在目标检测中,数据集常常使用labelimg标注,会生成xml文件。本文旨在根据xml标注文件来裁剪目标,以达到去除背景信息的目的。
详细连接:
细节
import cv2
import xml.etree.ElementTree as ET
import os
img_path = r'Y:\V47\Indoor\BWPhoto\Normal\2203181450_Indoor_Normal' # 图片路径
xml_path = r'Y:\V47\Indoor\BWPhoto\Normal\2203181450_Indoor_Normal' # 标签路径
obj_img_path = r'D:/cut' # 目标裁剪图片存放路径
for img_file in os.listdir(img_path): # 遍历图片文件夹
if img_file[-4:] in ['.bmp', '.jpg', '.png']: # 判断文件是否为图片格式
img_filename = os.path.join(img_path, img_file) # 将图片路径与图片名进行拼接
img_cv = cv2.imread(img_filename) # 读取图片
img_name = (os.path.splitext(img_file)[0]) # 分割出图片名
img_name_ = img_name.split('_')[1]
#找到标签xml文件路径名
xml_name = xml_path + '\\' + '%s.xml' % ('PADRecord_'+ img_name_) # 利用标签路径、图片名、xml后缀拼接出完整的标签路径名
print(xml_name)
if os.path.exists(xml_name): # 判断与图片同名的标签是否存在,因为图片不一定每张都打标
root = ET.parse(xml_name).getroot() # 利用ET读取xml文件
count = 0 # 目标框个数统计,防止目标文件覆盖
for obj in root.iter('LocalizedFaces'): # 遍历所有目标框
name = obj.find('LocalizedFace').text # 获取目标框名称,即label名
xmlbox = obj.find('LocalizedFace') # 找到框目标
x0 = xmlbox.find('JFace').text # 将框目标的四个顶点坐标取出
y0 = xmlbox.find('IFace').text
x1 = xmlbox.find('WidthFace').text
y1 = xmlbox.find('HeightFace').text
obj_img = img_cv[int(y0):int(y0)+int(y1), int(x0):int(x0)+int(x1)] # cv2裁剪出目标框中的图片
# cv2.imshow("image", obj_img) # 显示图像
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.imwrite(obj_img_path + '\\' + '%s_%s' % (img_name, count) + '.jpg', obj_img) # 保存裁剪图片
count += 1 # 目标框统计值自增1
print("裁剪完成!")
边栏推荐
- 目标检测网络R-CNN 系列
- Router. Push(), router. Reply(), router. Go()
- Analysis on the current situation and optimization strategy of customer experience management in banking industry
- 深度学习3D人体姿态估计国内外研究现状及痛点
- A college archives management system based on asp.net
- 时间复杂度和空间复杂度
- 基于WebRTC和WebSocket实现的聊天系统
- JVM: what does the class loading subsystem do? What is it made of? What eight part essay do you need to remember?
- Leetcode 1523. count odd numbers within the interval
- 算法--连续数列(Kotlin)
猜你喜欢

天津市应急局与驻津央企签署协议深化应急联动机制建设

详解关系抽取模型 CasRel

JSON数据传递参数&日期型参数传递

Solution: unable to load the file c:\users\user\appdata\roaming\npm\npx PS1, because running scripts is prohibited on this system.

Unity中序列化类为json格式

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

MVVM architecture encapsulation of kotlin series (kotlin+mvvm)

Slam 02. overall framework

Elementary notes of binary tree

牛客刷SQL---2
随机推荐
Kubelet CRI 容器运行时
概率论与数理统计
深度学习3D人体姿态估计国内外研究现状及痛点
Slam 02. overall framework
7-25 0-1 backpack (50 points)
同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web
Golang port scanning design
Detailed explanation of factory mode
Photoshop(CC2020)未完
冒泡排序的时间复杂度分析
【开源之美】nanomsg(2) :req/rep 模式
We were tossed all night by a Kong performance bug
牛客刷SQL---2
B+树索引使用(7)匹配列前缀,匹配值范围(十九)
Target detection network r-cnn series
pomerium
Elementary notes of binary tree
Analysis on the current situation and optimization strategy of customer experience management in banking industry
One stroke problem (Chinese postman problem)
The child component triggers the defineemits of the parent component: the child component passes values to the parent component