当前位置:网站首页>According to the category information and coordinate information of the XML file, the category area corresponding to the image is pulled out and stored in the folder.
According to the category information and coordinate information of the XML file, the category area corresponding to the image is pulled out and stored in the folder.
2022-04-23 03:35:00 【Crispy eggplant strips】
""" according to xml Category information and location coordinate information of the file , The corresponding category object crop come out , And save it into a folder named after the category information Purpose : It is convenient to recheck the drawing quality after drawing . Input : Folder containing pictures imgs, contain xml Information folder xmls Output : Folder crop_img,crop_img A subfolder named after the label category under , as well as crop The children of """
import cv2
import os
import xml.etree.ElementTree as ET
xml_file_dir = "./xmls"
img_file_dir = "./imgs"
xmls = os.listdir(xml_file_dir)
for xml in xmls:
# analysis xml
tree = ET.parse(os.path.join(xml_file_dir,xml))
root = tree.getroot()
img_name = root.find("filename").text
img = cv2.imread(os.path.join(img_file_dir,img_name))
objects = root.findall('object')
for i, obj in enumerate(objects):
id = 0
label = obj.find('name').text
if label == ":":
label = "colon"
bb = obj.find('bndbox')
xmin = bb.find('xmin').text
ymin = bb.find('ymin').text
xmax = bb.find('xmax').text
ymax = bb.find('ymax').text
# preservation crop img
crop_img = img[int(ymin):int(ymax), int(xmin):int(xmax)]
# crop_img_name Naming method :img_name+"_"+label+"_"+i
crop_img_name = os.path.splitext(img_name)[0] + "_" + label+"_"+str(i)+".jpg"
crop_img_save_dir = os.path.join(os.getcwd(),"crop_img",label)
print(crop_img_save_dir)
if os.path.exists(crop_img_save_dir) == 0:
os.makedirs(crop_img_save_dir)
crop_img_save_path = os.path.join(crop_img_save_dir, crop_img_name)
cv2.imwrite(crop_img_save_path,crop_img)
版权声明
本文为[Crispy eggplant strips]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220602452299.html
边栏推荐
- When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
- Super easy to use asynchronous export function of Excel
- Oracle JDK vs OpenJDK
- 51 single chip microcomputer: D / a digital to analog conversion experiment
- Using swagger in. Net5
- Use of rotary selector wheelpicker
- Alphafpld upgrade alphafold multimer
- 7-3 poly width
- Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
- 7-2 Tushare
猜你喜欢

2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)

Code forces round # 784 (DIV. 4) solution (First AK CF (XD)

Instructions for fastmock

2022 group programming ladder simulation l2-1 blind box packaging line (25 points)

Basic use of Charles

51 single chip microcomputer: D / a digital to analog conversion experiment
![[microservices] (x) -- Unified gateway](/img/f8/0c4516a802086cbe4031183b27a775.jpg)
[microservices] (x) -- Unified gateway

2022 团体程序设计天梯赛 模拟赛 L2-3 浪漫侧影 (25 分)

Seekbar custom style details

Unity knowledge points (ugui)
随机推荐
Design and implementation of redis (3): persistence strategy RDB, AOF
Visual programming - drawing assignment
[microservices] (x) -- Unified gateway
Several common methods of multithreading
Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
Unity Basics
Use of rotary selector wheelpicker
7-3 poly width
Query stored procedures in PostgreSQL
JS takes out the same elements in two arrays
Problem a: face recognition
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
Design and implementation of redis (2): how to handle expired keys
Laboratory safety examination
Section 1 array and slicing in Chapter 6
Unity knowledge points (ugui)
Using swagger in. Net5
Design and implementation of redis (6): how redis achieves high availability
If statement format flow
51 single chip microcomputer: D / a digital to analog conversion experiment