当前位置:网站首页>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
边栏推荐
- Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
- Learn about I / O flow and file operations
- Super easy to use asynchronous export function of Excel
- Unity knowledge points (common core classes)
- Visual programming - Experiment 1
- Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
- Problem C: realize Joseph Ring with linked list
- 7-1 introduction to finance
- Section 1 array and slicing in Chapter 6
- Design and implementation of redis (4): what is the event driver of redis
猜你喜欢

Flink customizes the application of sink side sinkfunction

Explanation keyword of MySQL

. net webapi access authorization mechanism and process design (header token + redis)

Visual programming - drawing assignment

Codeforces round 784 (Div. 4) (AK CF (XD) for the first time)

you need to be root to perform this command

The art of concurrent programming (6): explain the principle of reentrantlock in detail

PYMOL-note

Software testing process

Visual programming - Experiment 1
随机推荐
2022 团体程序设计天梯赛 模拟赛 L1-7 矩阵列平移 (20 分)
Commonly used classes
Using jsonserialize to realize data type conversion gracefully
PyMOL usage
Detailed explanation of socket programming send() and recv() functions
Codeforces Round #784 (Div. 4)题解 (第一次AK cf (XD
File upload vulnerability summary and upload labs shooting range documentary
you need to be root to perform this command
2021-08-31
L3-011 直捣黄龙 (30 分)
C interface
Leetcode 617 merge binary tree
Unity games and related interview questions
Supersocket is Use in net5 - concept
Three types of jump statements
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
Idea view history [file history and project history]
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
标识符、关键字、数据类型
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径