当前位置:网站首页>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
边栏推荐
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- 2021-08-11
- The art of concurrent programming (2): synchronized usage scenarios
- L3-011 直捣黄龙 (30 分)
- Unity knowledge points (ugui 2)
- C set
- Leetcode 617 merge binary tree
- Explanation keyword of MySQL
- 2021-08-31
- When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
猜你喜欢
Unity knowledge points (common core classes)
常用的辅助类
Optimization of especially slow startup in idea debugging mode
Supersocket is Use in net5 - startup
Explanation keyword of MySQL
移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
. net 5 Web custom middleware implementation returns the default picture
New ORM framework -- Introduction to beetlsql
Build websocket server in. Net5 webapi
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
随机推荐
Using jsonserialize to realize data type conversion gracefully
Design and implementation of redis (3): persistence strategy RDB, AOF
JS changes the words separated by dashes into camel style
Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics
The content of the website is prohibited from copying, pasting and saving as JS code
Problem C: Hanoi Tower III
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
Test questions and some space wars
Redis (17) -- redis cache related problem solving
7-1 introduction to finance
Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
The query type of MySQL is very inefficient.
Generate QR code through zxing
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
Oracle query foreign keys contain comma separated data
Alphafpld upgrade alphafold multimer
深度學習筆記(二)——激活函數原理與實現
If statement format flow
Wechat payment iframe sub page has no response