98 lines
4.8 KiB
Python
Executable File
98 lines
4.8 KiB
Python
Executable File
'''
|
|
author £º Hatcher W
|
|
Email£º hcwang@semi.ac.cn
|
|
use: ÀûÓÃÄ¿±ê¼ì²â±ê×¢Êý¾Ý¼¯Éú³ÉϸÁ£¶Èʶ±ðÔ¤Êý¾Ý¼¯£¨Î´±ê×¢£©£¬½«Ä¿±ê¼ì²â±ê×¢µÄͼÏñ²Ã¼ôΪϸÁ£¶Èʶ±ðµÄ½á¹û
|
|
input:Ä¿±ê¼ì²â±êÇ©Îļþ¸ù·¾¶ xml£¬Í¬Ä¿Â¼ÏÂÃæÓаüº¬ËùÓÐͼƬµÄJPEGImages·¾¶
|
|
output£ºÊä³öËùÓеĽá¹ûͼƬµ½FineOrimages
|
|
'''
|
|
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
|
|
|
|
root_path = "/data/DataManagement/Project/005_kunmingdianchi/0002_20220412_800_200/Dataset/VOC00502/Annotations"
|
|
class_name = ["Anser albifrons" ,"Fulica atra" ,"Leucogeranus leucogeranus" ,"Egretta garzetta","Platalea leucorodia" ,"Mergellus albellus" ,"Grus monacha" ,"Tringa ochropus" ,"Numenius arquata" ,"Anser indicus","Anas zonorhyncha","Ardea cinerea","Mareca strepera" ,"Mareca penelope" ,"Tadorna ferruginea","Ardea alba","Numenius madagascariensis" ,"Botaurus stellaris" ,"Ciconia boyciana" ,"Anser fabalis" ,"Recurvirostra avosetta" ,"Podiceps cristatus","Vanellus vanellus" ,"Aythya fuligula" ,"Tringa erythropus","Calidris alpina" ,"Ciconia nigra" ,"Gallinula chloropus","Porphyrio poliocephalus","Larus ridibundus" ,"Anser cygnoides" ,"Charadrius alexandrinusw" ,"Pluvialis squatarola" ,"Grus grus" ,"Anser anser" ,"Pluvialis fulva" ,"Charadrius dubius" ,"Anas crecca","Anas platyrhynchos","Spatula clypeata","Phalacrocorax carbo","Mergus merganser" ,"Tadorna tadorna" ,"Tringa nebulariaw","Bucephala clangula" ,"Gallinago gallinago" ,"Anser erythropus","Cygnus columbianus" ,"Ichthyaetus ichthyaetus","Anas acuta" ,"Ardea intermedia"]
|
|
image_T = 100
|
|
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)
|
|
num_count = 0
|
|
#for xml_path in os.listdir(root_path):
|
|
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("anno","images"),xml_path.replace("xml","jpg")))
|
|
f = open("./Hatcher_ALL.txt", "a")
|
|
txt_lists = []
|
|
if not os.path.exists(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T))))):
|
|
os.makedirs(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T)))))
|
|
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
|
|
xmin = int(xmin)
|
|
xmax = int(xmax)
|
|
ymin = int(ymin)
|
|
ymax = int(ymax)
|
|
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))
|
|
cv2.imwrite(os.path.join(root_path.replace("Annotations","FineDataset_{}".format(str(image_T))),"{}.jpg".format(str(num_count).zfill(6))), img1)
|
|
txt_lists.append("{}.jpg".format(str(num_count).zfill(6))+'\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()
|
|
print(num_count)
|
|
|
|
|
|
|
|
|
|
#######################
|
|
|
|
###Õë¶ÔÄ¿±ê¼ì²âÊý¾Ý¼¯Ö±½ÓÉú³ÉϸÁ£¶ÈÊý¾Ý¼¯
|
|
|
|
|
|
##########################
|
|
|
|
|