当前位置:网站首页>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
边栏推荐
- Visual programming - Experiment 1
- Create virtual machine
- Applet - more than two pieces of folding and expansion logic
- Do you really understand hashcode and equals???
- Applet - WXS
- VS Studio 修改C语言scanf等报错
- 【微服务】(十)—— 统一网关Gateway
- PWA I'm here
- Scenario Title: how does system a use the page of system B
- 2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
猜你喜欢

The art of concurrent programming (3): an in-depth understanding of the principle of synchronized

Development record of primary sensitive word detection

Unity knowledge points (ugui 2)

The principle and solution of not allowing pasting in an English Network

Notes sur l'apprentissage profond (Ⅱ) - - Principe et mise en oeuvre de la fonction d'activation

Optimization of especially slow startup in idea debugging mode

Supersocket is Use in net5 - concept

Wechat applet canvas draws a simple asymptotic color of the dashboard

Basic use of Charles

51 single chip microcomputer: D / a digital to analog conversion experiment
随机推荐
Unity knowledge points (common core classes)
Three column layout (fixed width on both sides in the middle and fixed width on both sides in the middle)
Variable definition and use
集合之List接口
Visual programming - drawing assignment
String input problem
Do you really understand hashcode and equals???
Applet - canvas drawing Poster
Docker pulls MySQL and connects
JS takes out the same elements in two arrays
第四次作业
Create virtual machine
Chapter 8 exception handling, string handling and file operation
mui. Plusready does not take effect
2022 group programming ladder game simulation L2-4 Zhezhi game (25 points)
Oracle JDK vs OpenJDK
Idempotency practice operation, explaining idempotency based on business
Seekbar custom style details
Supersocket is Use in net5 - concept
Design and implementation of redis (1): understand data structures and objects