Open BlcaKHat opened 6 years ago
Use this file and generate xml file ......please change location in the given file. save the code in two python file .
File name ...........> gen_xml.py (first file)
import os import cv2 from lxml import etree import xml.etree.cElementTree as ET
def write_xml(folder, img, objects, tl, br, savedir): if not os.path.isdir(savedir): os.mkdir(savedir)
image = cv2.imread(img.path)
height, width, depth = image.shape
annotation = ET.Element('annotation')
ET.SubElement(annotation, 'folder').text = folder
ET.SubElement(annotation, 'filename').text = img.name
ET.SubElement(annotation, 'segmented').text = '0'
size = ET.SubElement(annotation, 'size')
ET.SubElement(size, 'width').text = str(width)
ET.SubElement(size, 'height').text = str(height)
ET.SubElement(size, 'depth').text = str(depth)
for obj, topl, botr in zip(objects, tl, br):
ob = ET.SubElement(annotation, 'object')
ET.SubElement(ob, 'name').text = obj
ET.SubElement(ob, 'pose').text = 'Unspecified'
ET.SubElement(ob, 'truncated').text = '0'
ET.SubElement(ob, 'difficult').text = '0'
bbox = ET.SubElement(ob, 'bndbox')
ET.SubElement(bbox, 'xmin').text = str(topl[0])
ET.SubElement(bbox, 'ymin').text = str(topl[1])
ET.SubElement(bbox, 'xmax').text = str(botr[0])
ET.SubElement(bbox, 'ymax').text = str(botr[1])
xml_str = ET.tostring(annotation)
root = etree.fromstring(xml_str)
xml_str = etree.tostring(root, pretty_print=True)
save_path = os.path.join(savedir, img.name.replace('jpeg', 'xml'))
with open(save_path, 'wb') as temp_xml:
temp_xml.write(xml_str)
if name == 'main': """ for testing """
folder = '/home/assetone04/Music/Darkflow-object-detection-master/new_model_data/images'
img = [im for im in os.scandir('/home/assetone04/Music/Darkflow-object-detection-master/new_model_data/images') if '1' in im.name][0]
objects = ['solar_panel']
tl = [(10, 10)]
br = [(100, 100)]
savedir = '/home/assetone04/Music/Darkflow-object-detection-master/new_model_data/annotations'
write_xml(folder, img, objects, tl, br, savedir)
File name ...........> draw_box.py (New python file.)
import os import matplotlib.pyplot as plt import cv2 from matplotlib.widgets import RectangleSelector from gen_xml import write_xml
img = None tl_list = [] br_list = [] object_list = []
image_folder = '/home/assetone04/Music/Darkflow-object-detection-master/new_model_data/images' savedir = '/home/assetone04/Music/Darkflow-object-detection-master/new_model_data/annotations' obj = 'solar_panel'
def line_select_callback(clk, rls): global tl_list global br_list global object_list tl_list.append((int(clk.xdata), int(clk.ydata))) br_list.append((int(rls.xdata), int(rls.ydata))) object_list.append(obj)
def onkeypress(event): global object_list global tl_list global br_list global img if event.key == 'q': # if we click q than automatically it save where you given the path otherwise if we save through button on image viewer it did not generate the xml file.
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
def toggle_selector(event): toggle_selector.RS.set_active(True)
if name == 'main': for n, image_file in enumerate(os.scandir(image_folder)): img = image_file fig, ax = plt.subplots(1, figsize=(10.5, 8))
#mngr.window.setGeometry(250, 40, 800, 600)
image = cv2.imread(image_file.path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ax.imshow(image)
toggle_selector.RS = RectangleSelector(
ax, line_select_callback,
drawtype='box', useblit=True,
button=[1], minspanx=5, minspany=5,
spancoords='pixels', interactive=True,
)
bbox = plt.connect('key_press_event', toggle_selector)
key = plt.connect('key_press_event', onkeypress)
plt.tight_layout()
plt.show()
plt.close(fig)
I have downloaded mrked object image datset from openImage. the annotation files is in the format( class_name x y, h w). how do i convert this file into .xml file ?