import cv2
import xml.etree.ElementTree as ET
import pdb
import os
# path_ori="/home/huangs/00-Projects/FruitsRecognition/DataSet/MakingDataSet/100fruits/phoneShot4/img/"
# xml_fold = '/home/huangs/00-Projects/FruitsRecognition/DataSet/MakingDataSet/100fruits/phoneShot4/annotation/'
# path_new = '/home/huangs/00-Projects/FruitsRecognition/DataSet/MakingDataSet/100fruits/phoneShot4/imgsmall/'
path_ori="/home/hs/data/yili/images/"
xml_fold = '/home/hs/data/yili/annotations/'
path_new = '/home/hs/data/yili/imgs0/'
imgslist = os.listdir(path_ori)
numy = 0
numx = 0
for img in imgslist: #20200313080026_jx004_0.jpg
print(img)
jpg_file = img.strip() #20200313080026_jx004_0.jpg
jpg_id = jpg_file.split('.')[0] #20200313080026_jx004_0
xml_file = jpg_id + '.xml' #20200313080026_jx004_0.xml
image = cv2.imread(path_ori + img, -1)
height_origin, width_origin = image.shape[:2]
xml_file = os.path.join(xml_fold, xml_file)
if os.path.exists(xml_file):
updateTree = ET.parse(xml_file)
root = updateTree.getroot()
element_object = root.findall('object')
print(image.shape)
for i in range(len(element_object)):
xmin = element_object[i].find('bndbox').find('xmin').text
xmax = element_object[i].find('bndbox').find('xmax').text
ymin = element_object[i].find('bndbox').find('ymin').text
ymax = element_object[i].find('bndbox').find('ymax').text
smallimg = image[int(float(ymin)):int(float(ymax)), int(float(xmin)):int(float(xmax))]
position = [int(float(xmin)), int(float(xmax)), int(float(ymin)), int(float(ymax))]
h=smallimg.shape[0]
w=smallimg.shape[1]
if h*w>20:
smallimg_path = path_new + jpg_id
#cv2.imwrite(new_jpg_file, cropped)
cv2.imwrite(smallimg_path +"_"+str(i)+".jpg",smallimg)
else:
print(position)
小图检测数据处理删除边缘小框
最新推荐文章于 2023-03-20 10:40:04 发布