当前位置:网站首页>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
边栏推荐
- Design and implementation of redis (5): master-slave replication strategy and optimization
- SQL topic exercise summary
- PYMOL-note
- Learn about I / O flow and file operations
- Batch download of files ---- compressed and then downloaded
- Seekbar custom style details
- QT learning summary
- Problem B: small challenge
- Design and implementation of redis (3): persistence strategy RDB, AOF
- you need to be root to perform this command
猜你喜欢
Visual programming - drawing assignment
Deep learning notes (II) -- principle and implementation of activation function
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Test questions (2)
Supersocket is Used in net5 - command
JS - accuracy issues
Download and configuration of idea
C set
对象和类的概念
随机推荐
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
Common exceptions
Visual programming - Experiment 2
Punch in: 4.23 C language chapter - (1) first knowledge of C language - (12) structure
7-1 introduction to finance
Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)
On the principle of concurrent programming and the art of notify / Park
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
C-10 program error correction (recursive function): number to character
Romantic silhouette of L2-3 of 2022 group programming ladder Simulation Competition (25 points)
Design and implementation of redis (2): how to handle expired keys
The content of the website is prohibited from copying, pasting and saving as JS code
集合之List接口
Deep learning notes (II) -- principle and implementation of activation function
you need to be root to perform this command
Unity knowledge points (ugui)
Visual programming - drawing assignment
Problem B: small challenge
POI create and export Excel based on data