Files
MPKFNet/get_fine_dataset.py
T
2022-04-28 22:02:20 +08:00

143 lines
6.4 KiB
Python
Executable File

import xml.dom.minidom
from tqdm import tqdm
from glob import glob
import cv2
import os
from PIL import Image
import numpy as np
from jpg import jpg
txt_root_path = "/data/DataManagement/Project/005_kunmingdianchi/0002_20220412_800_200/Dataset/VOC0050201"
#xml_files = glob('./temp2/*.xml')
image_T = 50
num_count = 0
root_path = "/data/DataManagement/Project/005_kunmingdianchi/0002_20220412_800_200/Dataset/VOC0050201/Annotations/"
def cv2_crop(im, box):
'''cv2实现类似PIL的裁剪
:param im: 加载好的图像
:param box: 裁剪的矩形,元组(left, upper, right, lower).
'''
return im.copy()[box[1]:box[3], box[0]:box[2], :]
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
# for xml_path in os.listdir(root_path):
# xml_files = glob(os.path.join(root_path,xml_path,"*.xml"))
# print(os.path.join(root_path.replace("xml","image"),xml_path))
# jpg(os.path.join(root_path.replace("xml","image"),xml_path))
# f = open("./Hatcher_ALL.txt", "a")
# txt_lists = []
# for xml_file in tqdm(xml_files):
#
# print(xml_file+" is doing")
# DOMTree = xml.dom.minidom.parse(xml_file)
# collection = DOMTree.documentElement
# #img_name = collection.getElementsByTagName("filename")[0].childNodes[0].data
# img_name = xml_file.replace("\\xml\\","\\image\\").replace(".xml",".jpg")
# objects = collection.getElementsByTagName("object")
# #img = cv2.imread(img_name)
# img = cv2.imdecode(np.fromfile(img_name,dtype=np.uint8),-1)
# #cv2.imwrite(img_name,img)
# #img = cv2.imdecode(np.fromfile(img_name,dtype=np.uint8),-1)
# # im = Image.open(img_name)
# # im.save(img_name, quality=100)
# # im = Image.open(img_name)
# for object_id in objects:
# xmin = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('xmin')[0].childNodes[0].data
# ymin = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('ymin')[0].childNodes[0].data
# xmax = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('xmax')[0].childNodes[0].data
# ymax = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('ymax')[0].childNodes[0].data
# name = object_id.getElementsByTagName('name')[0].childNodes[0].data
# xmin = int(xmin)
# xmax = int(xmax)
# ymin = int(ymin)
# ymax = int(ymax)
# class_index = class_name.index(str(name).replace("\t","").strip())
# img1 = cv2_crop(img,(xmin, ymin,xmax, ymax))
# cv2.imwrite("H:\\Fine-grained-identification\\2021.3.25\Hatcher_One\\" + str(name).replace("\t","").strip().replace(" ","_" )+"_{}.jpg".format(num_count), img1)
# txt_lists.append(str(name).replace("\t","").strip().replace(" ","_" )+"_{}.jpg".format(num_count)+" "+str(class_index)+'\n')
# #f.write(str(name).replace("\t","").strip().replace(" ","_" )+"_{}.jpg".format(num_count)+" "+str(class_index)+'\n')
# num_count += 1
# for txt_list in txt_lists:
# f.write(txt_list)
# f.close()
# f_num = open("./num.txt", "w")
# f_num.write(str(num_count))
# f_num.close()
# print(num_count)
# for xml_file in tqdm(xml_files):
# os.remove(xml_file)
#
# #os.remove(xml_file)
# print(num_count)
xml_files = glob(os.path.join(root_path,"*.xml"))
#print(os.path.join(root_path.replace("Annotations","JPEGImages")))
jpg(os.path.join(root_path.replace("Annotations","JPEGImages")))
f = open(txt_root_path+"/Hatcher_ALL.txt", "a")
txt_lists = []
for xml_file in tqdm(xml_files):
#print(xml_file+" is doing")
DOMTree = xml.dom.minidom.parse(xml_file)
collection = DOMTree.documentElement
#img_name = collection.getElementsByTagName("filename")[0].childNodes[0].data
img_name = xml_file.replace("Annotations","JPEGImages").replace(".xml",".jpg")
objects = collection.getElementsByTagName("object")
#img = cv2.imread(img_name)
img = cv2.imdecode(np.fromfile(img_name,dtype=np.uint8),-1)
#cv2.imwrite(img_name,img)
#img = cv2.imdecode(np.fromfile(img_name,dtype=np.uint8),-1)
# im = Image.open(img_name)
# im.save(img_name, quality=100)
# im = Image.open(img_name)
for object_id in objects:
xmin = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('xmin')[0].childNodes[0].data
ymin = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('ymin')[0].childNodes[0].data
xmax = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('xmax')[0].childNodes[0].data
ymax = object_id.getElementsByTagName('bndbox')[0].getElementsByTagName('ymax')[0].childNodes[0].data
name = object_id.getElementsByTagName('name')[0].childNodes[0].data
ratio_x = (3840.0/3840.0)
ratio_y = (2160.0/2160.0)
#print(ratio_x," ",ratio_y)
#print(type(xmin))
#exit()
xmin = int(float(xmin)*ratio_x)
xmax = int(float(xmax)*ratio_x)
ymin = int(float(ymin)*ratio_y)
ymax = int(float(ymax)*ratio_y)
if ((xmax - xmin)<image_T) | ((ymax - ymin)<image_T):
continue
#class_index = class_name.index(str(name).replace("\t","").strip())
img1 = cv2_crop(img,(xmin, ymin,xmax, ymax))
if not os.path.exists(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T))),str(name).replace("\t","").strip())):
os.makedirs(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T))),str(name).replace("\t","").strip()))
cv2.imwrite(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T))),str(name).replace("\t","").strip(),"{}.jpg".format(str(num_count).zfill(5))), img1)
#txt_lists.append(str(name).replace("\t","").strip()+"{}.jpg".format(str(num_count).zfill(5))+" "+str(class_index)+'\n')
#f.write(str(name).replace("\t","").strip().replace(" ","_" )+"_{}.jpg".format(num_count)+" "+str(class_index)+'\n')
num_count += 1
for txt_list in txt_lists:
f.write(txt_list)
f.close()
f_num = open("./num.txt", "w")
f_num.write(str(num_count))
f_num.close()
print(num_count)
# for xml_file in tqdm(xml_files):
# os.remove(xml_file)
#os.remove(xml_file)
print(num_count)