-
Notifications
You must be signed in to change notification settings - Fork 15
/
visualizer.py
156 lines (135 loc) · 4.28 KB
/
visualizer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import numpy as np
import cv2
import matplotlib.pyplot as plt
from bounding_box import BoundingBox
color = {
0: [255, 0, 0],
1: [0, 255, 0],
2: [0, 0, 255],
3: [255, 255, 0],
4: [255, 0, 255],
5: [0, 255, 255],
6: [255, 0, 125],
7: [255, 125, 0],
8: [125, 255, 0],
9: [0, 255, 125],
10: [0, 125, 255],
11: [125, 0, 255],
}
def draw_spatially_binned_features(feature_vec):
"""
Draws spatially binned feature vector
:param feature_vec: Feature vector
:return: None
"""
# Draw feature vector
plt.plot(feature_vec)
plt.title('Spatially Binned Features')
def rescale(image):
"""
Rescales image on 32x32 size
:param image: Input image
:return: Returns 32x32 pixel version of image
"""
# Return resized image
return cv2.resize(image,(32,32))
def draw_histograms(r_hist, g_hist, b_hist, bin_centers):
"""
Draws the histograms of the RGB channels
:param r_hist: Red Channel Histogram
:param g_hist: Grenn Channel Histogram
:param b_hist: Blue Channel Histogram
:param bin_centers: Bin centers
:return: None
"""
# Draw histograms
fig = plt.figure(figsize=(12,3))
plt.subplot(131)
plt.bar(bin_centers, r_hist[0])
plt.xlim(0, 256)
plt.title('R Histogram')
plt.subplot(132)
plt.bar(bin_centers, g_hist[0])
plt.xlim(0, 256)
plt.title('G Histogram')
plt.subplot(133)
plt.bar(bin_centers, b_hist[0])
plt.xlim(0, 256)
plt.title('B Histogram')
fig.tight_layout()
plt.show()
def draw_boxes(img, bboxes, color=(0, 0, 255), thick=6):
"""
Returns a copy of the image with drawn bounding boxes
:param img: Image without bounding box information
:param bboxes: Bounding boxes
:param color: Color code of the bounding boxes
:param thick: Thickness of lines from the bounding boxes
:return: Image with bounding box information
"""
# Make a copy of the image
draw_img = np.copy(img)
# Loop through bounding boxes and add them
for bbox in bboxes:
# print(bbox)
cv2.rectangle(draw_img,bbox[0],bbox[1],color=color,thickness=thick)
# Returns image with bounding boxes
return draw_img
def draw_image(img, title='', save=False):
"""
Draws image
:param img: Image to draw
:return: None
"""
# Draw image
f = plt.gcf()
plt.imshow(img, cmap='gray')
plt.title(title)
if save:
f.savefig('output_images/' + title + '.png')
else:
plt.show()
def draw_two_images(img1, img2, title = '', save=False):
# Draw image
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(img1, cmap='gray')
ax2.imshow(img2, cmap='gray')
plt.show()
if save:
f.savefig('output_images/' + title + '.png')
def draw_labeled_bboxes(img, bboxes):
# Iterate through all detected cars
for bbox in bboxes:
# Draw the box on the image
cv2.rectangle(img, bbox.p1, bbox.p2, (0,0,255), 6)
# Return the image
return img
def draw_tracking(image,tracker):
draw_img = np.copy(image)
for track in tracker.list_of_tracks:
if track.age > 3:
col = color[track.id % len(color)]
p1_x = np.int(track.box.x_center - track.box.width/2)
p2_x = np.int(track.box.x_center + track.box.width / 2)
p1_y = np.int(track.box.y_center - track.box.height / 2)
p2_y = np.int(track.box.y_center + track.box.height / 2)
cv2.rectangle(draw_img, (p1_x,p2_y), (p2_x,p1_y), color=col, thickness=3)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(draw_img, 'ID ' + str(track.id) + ' AGE ' + str(track.age),
(p1_x, p1_y - 10), font, 1, col, 2, cv2.LINE_AA)
cv2.putText(draw_img, 'B ' + str('%.2f' % track.belief),
(p1_x, p1_y - 40), font, 1, col, 2, cv2.LINE_AA)
# draw_image(draw_img)
return draw_img
def read_and_draw_image(image_name,title):
image = cv2.imread(image_name)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
draw_image(image,title,True)
return image
def draw_roi(image):
bboxes = []
roi = BoundingBox([640,528,1280,256])
bboxes.append(roi)
roi_image = draw_labeled_bboxes(image,bboxes)
draw_image(roi_image,'ROI','True')