import glob
import json
import json
import cv2
import os
file_dir=r'F:projectjushidataCAM-G3-GM10-M1280-D_7E04790PAK00005label'
ori_pic = 'F:projectjushidataCAM-G3-GM10-M1280-D_7E04790PAK00005images'
files=glob.glob(file_dir+'/*')
for json_file in files:
fileJson=json.load(open(json_file, 'r'))[0]
# print(val)
# file = open(json_file, "r", encoding='utf-8')
# fileJson = json.load(file)
field = fileJson["annotations"]
img_name = fileJson['image']
img = cv2.imread(os.path.join(ori_pic, img_name))
result = {}
for line in field:
bouding_box = line['coordinates']
label = line['label']
x1 = int(bouding_box['x']-bouding_box['width']/2)
y1 = int(bouding_box['y']-bouding_box['height']/2)
y2 = int(bouding_box['y']) + int(bouding_box['height']/2)
x2 = int(bouding_box['x']) + int(bouding_box['width']/2)
img2 = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255))
print(label,x1, y1,x2-x1, y2-y1)
cv2.imshow("asdfsdf",img2)
cv2.waitKey()
labelme转voc格式:
import glob
import json
import json
import cv2
import os
from lxml import etree
class GEN_Annotations:
def __init__(self, filename):
self.root = etree.Element("annotation")
child1 = etree.SubElement(self.root, "folder")
child1.text = "VOC2007"
child2 = etree.SubElement(self.root, "filename")
child2.text = filename
child3 = etree.SubElement(self.root, "source")
# child2.set("database", "The VOC2007 Database")
child4 = etree.SubElement(child3, "annotation")
child4.text = "PASCAL VOC2007"
child5 = etree.SubElement(child3, "database")
child6 = etree.SubElement(child3, "image")
child6.text = "flickr"
child7 = etree.SubElement(child3, "flickrid")
child7.text = "35435"
# root.append( etree.Element("child1") ) # root.append( etree.Element("child1", interesting="totally")) # child2 = etree.SubElement(root, "child2")
# child3 = etree.SubElement(root, "child3") # root.insert(0, etree.Element("child0"))
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, "channel")
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, x, y, w, h):
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(x)
yminn = etree.SubElement(bndbox, "ymin")
yminn.text = str(y)
xmaxn = etree.SubElement(bndbox, "xmax")
xmaxn.text = str(x + w)
ymaxn = etree.SubElement(bndbox, "ymax")
ymaxn.text = str(y + h)
if __name__ == '__main__':
file_dir=r'F:projectjushidataCAM-G3-GM10-M1280-D_7E04790PAK00005label'
ori_pic = 'F:projectjushidataCAM-G3-GM10-M1280-D_7E04790PAK00005images'
label_dir=r'F:projectjushidataCAM-G3-GM10-M1280-D_7E04790PAK00005Annotations/'
os.makedirs(label_dir,exist_ok=True)
files=glob.glob(file_dir+'/*')
label_index={'旋窑生产':"xuanyao",'50kg':'zhongliang'}
for json_file in files:
fileJson=json.load(open(json_file, 'r'))[0]
field = fileJson["annotations"]
img_name = fileJson['image']
anno = GEN_Annotations(img_name)
anno.set_size(1280, 1024, 3)
img = cv2.imread(os.path.join(ori_pic, img_name))
result = {}
for line in field:
bouding_box = line['coordinates']
label = line['label']
x1 = int(bouding_box['x']-bouding_box['width']/2)
y1 = int(bouding_box['y']-bouding_box['height']/2)
y2 = int(bouding_box['y']) + int(bouding_box['height']/2)
x2 = int(bouding_box['x']) + int(bouding_box['width']/2)
img2 = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255))
print(label,x1, y1,x2-x1, y2-y1)
if label in label_index:
x = x1
y = y1
w = x2-x1
h =y2-y1
anno.add_pic_attr(label_index[label], x, y, w, h)
anno.savefile(label_dir+img_name.replace(".bmp",".xml"))
# cv2.imshow("asdfsdf",img2)
# cv2.waitKey()



