当前位置:网站首页>从标注好的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("裁剪完成!")
边栏推荐
- Exploration on cache design optimization of community like business
- How to face scientific and technological unemployment?
- Unicode file parsing methods and existing problems
- [applet] why can't the onreachbottom event be triggered? (one second)
- 基于ASP.NET的某高校学院档案管理系统
- [flower carving hands-on] fun music visualization series small project (12) -- meter tube fast rhythm light
- 基于BERT的情感分析模型
- B+树(4)联合索引 --mysql从入门到精通(十六)
- Implementation of SAP ABAP daemon
- Sword finger offer (VII): Fibonacci sequence
猜你喜欢

Unicode file parsing methods and existing problems

Can I take your subdomain? Exploring Same-Site Attacks in the Modern Web
![[typescript] typescript common types (Part 1)](/img/80/5c8c51b92d3a9d76f38beba7be0aa6.png)
[typescript] typescript common types (Part 1)

AI theory knowledge map 1 Foundation

Solution 5g technology helps build smart Parks

基于Bézier曲线的三维造型与渲染

Solve the problem that the remote host cannot connect to the MySQL database

Precautions for triggering pytest.main() from other files

postgresql官网下载出错

MVVM architecture encapsulation of kotlin series (kotlin+mvvm)
随机推荐
JVM: what does the class loading subsystem do? What is it made of? What eight part essay do you need to remember?
【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯
MVVM architecture encapsulation of kotlin series (kotlin+mvvm)
vector的一些实用操作
Kubernetes APIServer 限流策略
1312_ Apply 7z command for compression and decompression
Brief introduction of reflection mechanism
pomerium
B+树索引使用(6)最左原则 --mysql从入门到精通(十八)
目标检测网络R-CNN 系列
JSON format execution plan (6) - MySQL execution plan (52)
Hcip day 12 notes sorting (BGP Federation, routing rules)
Kubelet CRI container runtime
B+ tree index use (9) grouping, back to table, overlay index (21)
Unicode file parsing methods and existing problems
Extra(5)—mysql执行计划(五十一)
AI theory knowledge map 1 Foundation
Sword finger offer (IX): abnormal jumping steps
panic: Error 1045: Access denied for user ‘root‘@‘117.61.242.215‘ (using password: YES)
File upload and download performance test based on the locust framework