This commit is contained in:
Semi212 committed 2022-04-28 22:02:20 +08:00
1 parent 54ccfb11fe
commit a0818377bb
194 files changed
+41681 -262

No files matched your search

+78 -20
View File
@@ -9,20 +9,73 @@ import os
import cv2
import xml.dom.minidom
from tqdm import tqdm
from lxml import etree
image_path="./JPEGImages/"
annotation_path="./Annatations/"
annotation_path="/data/Hatcher/Fine_HC/anno_xml/"
save_path ="./check/"
num = 0
files_name = os.listdir(image_path)
files_name = os.listdir(annotation_path)
class GEN_Annotations:
def __init__(self, filename):
self.root = etree.Element("annotation")
child1 = etree.SubElement(self.root, "folder")
child1.text = "Hatcher"
child2 = etree.SubElement(self.root, "filename")
child2.text = filename
child3 = etree.SubElement(self.root, "source")
child4 = etree.SubElement(child3, "annotation")
child4.text = "MultiModel"
child5 = etree.SubElement(child3, "database")
child5.text = "Unknown"
child6 = etree.SubElement(child3, "image")
child6.text = "flickr"
child7 = etree.SubElement(child3, "flickrid")
child7.text = "35435"
def set_size(self, witdh, height, channel):
size = etree.SubElement(self.root, "size")
widthn = etree.SubElement(size, "width")
widthn.text = str(witdh)
heightn = etree.SubElement(size, "height")
heightn.text = str(height)
channeln = etree.SubElement(size, "depth")
channeln.text = str(channel)
def savefile(self, filename):
tree = etree.ElementTree(self.root)
tree.write(filename, pretty_print=True, xml_declaration=False, encoding='utf-8')
def add_pic_attr(self, label, xmin, ymin, xmax, ymax):
object = etree.SubElement(self.root, "object")
namen = etree.SubElement(object, "name")
namen.text = label
bndbox = etree.SubElement(object, "bndbox")
xminn = etree.SubElement(bndbox, "xmin")
xminn.text = str(xmin)
yminn = etree.SubElement(bndbox, "ymin")
yminn.text = str(ymin)
xmaxn = etree.SubElement(bndbox, "xmax")
xmaxn.text = str(xmax)
ymaxn = etree.SubElement(bndbox, "ymax")
ymaxn.text = str(ymax)
def make_xml(image_path,shape0,shape1,x_left, y_left, x_right, y_right):
print(image_path)
print("\n")
anno = GEN_Annotations(image_path)
anno.set_size(shape0, shape1, 3)
anno.add_pic_attr("bird", x_left, y_left, x_right, y_right)
anno.savefile(image_path)
for filename_ in tqdm(files_name):
filename, extension= os.path.splitext(filename_)
#filename=filename.split('__')[0]
img_path =image_path+filename+'.jpg'
xml_path =annotation_path+filename+'.xml'
img = cv2.imread(img_path)
if img is None:
pass
xml_path = os.path.join(annotation_path,filename_)
try:
dom = xml.dom.minidom.parse(xml_path)
except:
@@ -30,6 +83,10 @@ for filename_ in tqdm(files_name):
continue
root = dom.documentElement
objects=dom.getElementsByTagName("object")
shapes = dom.getElementsByTagName("size")
for shape in shapes:
width = shape.getElementsByTagName('width')[0].childNodes[0].data
height = shape.getElementsByTagName('height')[0].childNodes[0].data
for object in objects:
bndbox = object.getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0]
@@ -38,18 +95,19 @@ for filename_ in tqdm(files_name):
ymax = bndbox.getElementsByTagName('ymax')[0]
#print(xmin.childNodes[0].data)
#print(type(xmin.childNodes[0].data))
xmin_data=int(xmin.childNodes[0].data)
ymin_data=int(ymin.childNodes[0].data)
xmax_data=int(xmax.childNodes[0].data)
ymax_data=int(ymax.childNodes[0].data)
xmin_data=int(float(xmin.childNodes[0].data.replace("tensor(","").replace(", device='cuda:0')","")))
ymin_data=int(float(ymin.childNodes[0].data.replace("tensor(","").replace(", device='cuda:0')","")))
xmax_data=int(float(xmax.childNodes[0].data.replace("tensor(","").replace(", device='cuda:0')","")))
ymax_data=int(float(ymax.childNodes[0].data.replace("tensor(","").replace(", device='cuda:0')","")))
label_name=object.getElementsByTagName('name')[0].childNodes[0].data
cv2.rectangle(img,(xmin_data,ymin_data),(xmax_data,ymax_data),(55,255,155),1)
num +=1
cv2.putText(img,label_name,(xmin_data,ymin_data-3),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)
# cv2.rectangle(img,(xmin_data,ymin_data),(xmax_data,ymax_data),(55,255,155),1)
# num +=1
# cv2.putText(img,label_name,(xmin_data,ymin_data-3),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)
make_xml(xml_path, width, height, xmin_data, ymin_data, xmax_data, ymax_data)
flag=0
flag=cv2.imwrite(save_path+"{}.jpg".format(filename),img)
if not (flag):
print(filename,"error")
# flag=cv2.imwrite(save_path+"{}.jpg".format(filename),img)
# if not (flag):
# print(filename,"error")
print("all done ====================================")
print("number = ",num)