forked from aws-samples/groundtruth-object-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
73 lines (60 loc) · 1.98 KB
/
visualize.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.colors as mcolors
import argparse
def visualize_bbox(img_file, yolo_ann_file, label_dict, figure_size=(6, 8)):
"""
Plots bounding boxes on images
Input:
img_file : numpy.array
yolo_ann_file: Text file containing annotations in YOLO format
label_dict: Dictionary of image categories
figure_size: Figure size
"""
img = mpimg.imread(img_file)
fig, ax = plt.subplots(1, 1, figsize=figure_size)
ax.imshow(img)
im_height, im_width, _ = img.shape
palette = mcolors.TABLEAU_COLORS
colors = [c for c in palette.keys()]
with open(yolo_ann_file, "r") as fin:
for line in fin:
cat, center_w, center_h, width, height = line.split()
cat = int(cat)
category_name = label_dict[cat]
left = (float(center_w) - float(width) / 2) * im_width
top = (float(center_h) - float(height) / 2) * im_height
width = float(width) * im_width
height = float(height) * im_height
rect = plt.Rectangle(
(left, top),
width,
height,
fill=False,
linewidth=2,
edgecolor=colors[cat],
)
ax.add_patch(rect)
props = dict(boxstyle="round", facecolor=colors[cat], alpha=0.5)
ax.text(
left,
top,
category_name,
fontsize=14,
verticalalignment="top",
bbox=props,
)
plt.show()
def main():
"""
Plots bounding boxes
"""
labels = {0: "pencil", 1: "pen"}
parser = argparse.ArgumentParser()
parser.add_argument("img", help="image file")
args = parser.parse_args()
img_file = args.img
ann_file = img_file.split(".")[0] + ".txt"
visualize_bbox(img_file, ann_file, labels, figure_size=(6, 8))
if __name__ == "__main__":
main()