Last active
July 22, 2021 17:58
-
-
Save yahyatawil/14d06424607ec3a769fc98ddac876fe1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# adapted and modified: | |
# https://gist.github.com/kodekracker/1777f35e8462b2b21846c14c0677f611 | |
import cv2 | |
import sys | |
import os | |
import xml.etree.ElementTree as ET | |
imagePath = "C:/Users/DELL/Desktop/samples/bbox/MP_SEL_B027484.jpg" | |
xmlpath = 'C:/Users/DELL/Desktop/samples/bbox/bbox_sample.xml' | |
filename = os.path.basename(imagePath) | |
tree = ET.parse(xmlpath) | |
imgcv = cv2.imread(imagePath) | |
def drawBoundingBoxes(inferenceResults, color): | |
for res in inferenceResults: | |
left = int(res['left']) | |
top = int(res['top']) | |
right = int(res['left']) + int(res['width']) | |
bottom = int(res['top']) + int(res['height']) | |
label = res['label'] | |
imgHeight, imgWidth, _ = imgcv.shape | |
thick = int((imgHeight + imgWidth) // 900) | |
cv2.rectangle(imgcv,(left, top), (right, bottom), color, thick) | |
cv2.putText(imgcv, label, (left, top - 12), 0, 1e-3 * imgHeight, color, thick) | |
def draw( x1, y1, x2, y2, label): | |
color = (0,0,255) | |
height = y2-y1 | |
width = x2-x1 | |
results = [ | |
{ | |
"left": x1, | |
"top": y1, | |
"width": width, | |
"height": height, | |
"label": label | |
} | |
] | |
drawBoundingBoxes(results, color) | |
def save_output(): | |
cv2.imwrite('./{}'.format(filename), imgcv) | |
root = tree.getroot() | |
for elem in root.iter('image'): | |
if elem.attrib['name'] == filename: | |
for i,e in enumerate(elem.iter()): | |
if i==0: | |
continue | |
label_name = e.attrib['label'] | |
x1 = e.attrib['xtl'] | |
x2 = e.attrib['xbr'] | |
y1 = e.attrib['ytl'] | |
y2 = e.attrib['ybr'] | |
draw(float(x1),float(y1),float(x2),float(y2),label_name) | |
save_output() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment