-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdraw_boxes.py
57 lines (50 loc) · 1.81 KB
/
draw_boxes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from PIL import Image, ImageDraw
import sys
import xml.etree.ElementTree as ET
import os
import traceback
import random
r_color = lambda: random.randint(0,255)
def get_boxes_from_filepath(filepath):
tree = ET.parse(filepath)
root = tree.getroot()
boxes = []
for child in root:
if child.tag == "object":
box_dict = {}
child_object = child
for attribs in child_object:
if attribs.tag == "name":
box_dict["name"]=attribs.text
if attribs.tag == "bndbox":
for coord in attribs:
box_dict[coord.tag] = int(coord.text)
boxes.append(box_dict)
return boxes
def draw_rectangle(im, coordinates, color, width=1):
drawing = ImageDraw.Draw(im)
for i in range(width):
rect_start = (coordinates[0][0] - i, coordinates[0][1] - i)
rect_end = (coordinates[1][0] + i, coordinates[1][1] + i)
drawing.rectangle((rect_start, rect_end), outline = color)
del drawing
return im
images = ["images/"+i for i in os.listdir("images") if i.endswith(".png")]
object_names = []
for image_path in images:
if os.path.exists(image_path.replace("images","annotations/xml").replace("png","xml")):
boxes = get_boxes_from_filepath(image_path.replace("images","annotations/xml").replace("png","xml"))
for i in boxes:
if i["name"] not in object_names:
object_names.append(i["name"])
color_map = {}
for i in object_names:
color_map[i] = (r_color(),r_color(),r_color(),100)
for image_path in images:
im = Image.open(image_path)
new_name = image_path.replace("images/","")
if os.path.exists(image_path.replace("images","annotations/xml").replace("png","xml")):
boxes = get_boxes_from_filepath(image_path.replace("images","annotations/xml").replace("png","xml"))
for i in boxes:
im = draw_rectangle(im, [(i["xmin"], i["ymin"]), (i["xmax"], i["ymax"])], color_map[i["name"]], 3)
im.save("annotated2/"+new_name, "PNG")