Repository navigation
Expand file tree
/
Copy pathTraffic_Classifier_HSV.py
More file actions
executable file
·166 lines (110 loc) · 5.04 KB
/
Copy pathTraffic_Classifier_HSV.py
File metadata and controls
executable file
·166 lines (110 loc) · 5.04 KB
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
157
158
159
160
161
162
163
164
165
166
from skimage.color import rgb2grey,rgb2hsv
import numpy as np
def crop_roi_image(image_np, sel_box):
cropped_image = image_np[int(sel_box[0]):int(sel_box[2]), int(sel_box[1]):int(sel_box[3]), :]
return cropped_image
def get_masked_hue_image(hsv_test_image):
masked_hue_image = hsv_test_image[:, :, 0]
return masked_hue_image
def high_saturation_region_mask(hsv_image, s_thres=0.6):
if hsv_image.dtype == np.int:
idx = (hsv_image[:, :, 1].astype(np.float) / 255.0) < s_thres
else:
idx = (hsv_image[:, :, 1].astype(np.float)) < s_thres
mask = np.ones_like(hsv_image[:, :, 1])
mask[idx] = 0
return mask
def channel_percentile(single_chan_image, percentile):
sq_image = np.squeeze(single_chan_image)
assert len(sq_image.shape) < 3
thres_value = np.percentile(sq_image.ravel(), percentile)
return float(thres_value) / 255.0
def high_value_region_mask(hsv_image, v_thres=0.6):
if hsv_image.dtype == np.int:
idx = (hsv_image[:, :, 2].astype(np.float) / 255.0) < v_thres
else:
idx = (hsv_image[:, :, 2].astype(np.float)) < v_thres
mask = np.ones_like(hsv_image[:, :, 2])
mask[idx] = 0
return mask
def get_masked_hue_values(rgb_image):
"""
Get the pixels in the RGB image that has high saturation (S) and value (V) in HSV chanels
:param rgb_image: image (height, width, channel)
:return: a 1-d array
"""
hsv_test_image = rgb2hsv(rgb_image)
s_thres_val = channel_percentile(hsv_test_image[:, :, 1], percentile=30)
v_thres_val = channel_percentile(hsv_test_image[:, :, 2], percentile=70)
val_mask = high_value_region_mask(hsv_test_image, v_thres=v_thres_val)
sat_mask = high_saturation_region_mask(hsv_test_image, s_thres=s_thres_val)
masked_hue_image = hsv_test_image[:, :, 0] * 180
# Note that the following statement is not equivalent to
# masked_hue_1d= (maksed_hue_image*np.logical_and(val_mask,sat_mask)).ravel()
# Because zero in hue channel means red, we cannot just set unused pixels to zero.
masked_hue_1d = masked_hue_image[np.logical_and(val_mask, sat_mask)].ravel()
return masked_hue_1d
def convert_to_hue_angle(hue_array):
"""
Convert the hue values from [0,179] to radian degrees [-pi, pi]
:param hue_array: array-like, the hue values in degree [0,179]
:return: the angles of hue values in radians [-pi, pi]
"""
hue_cos = np.cos(hue_array * np.pi / 90)
hue_sine = np.sin(hue_array * np.pi / 90)
hue_angle = np.arctan2(hue_sine, hue_cos)
return hue_angle
def get_rgy_color_mask(hue_value, from_01=False):
"""
return a tuple of np.ndarray that sets the pixels with red, green and yellow matrices to be true
:param hue_value:
:param from_01: True if the hue values is scaled from 0-1 (scikit-image), otherwise is -pi to pi
:return:
"""
if from_01:
n_hue_value = conver_to_hue_angle_from_01(hue_value)
else:
n_hue_value = hue_value
red_index = np.logical_and(n_hue_value < (0.125 * np.pi), n_hue_value > (-0.125 * np.pi))
green_index = np.logical_and(n_hue_value > (0.66 * np.pi), n_hue_value < np.pi)
yellow_index = np.logical_and(n_hue_value > (0.25 * np.pi), n_hue_value < (5.0 / 12.0 * np.pi))
return red_index, green_index, yellow_index
def classify_color_by_range(hue_value):
"""
Determine the color (red, yellow or green) in a hue value array
:param hue_value: hue_value is radians
:return: the color index ['red', 'yellow', 'green', '_', 'unknown']
"""
red_index, green_index, yellow_index = get_rgy_color_mask(hue_value)
color_counts = np.array([np.sum(red_index) / len(hue_value),
np.sum(yellow_index) / len(hue_value),
np.sum(green_index) / len(hue_value)])
color_text = ['red', 'yellow', 'green', '_', 'unknown']
min_index = np.argmax(color_counts)
return min_index, color_text[min_index]
def classify_color_cropped_image(rgb_image):
"""
Full pipeline of classifying the traffic light color from the traffic light image
:param rgb_image: the RGB image array (height,width, RGB channel)
:return: the color index ['red', 'yellow', 'green', '_', 'unknown']
"""
hue_1d_deg = get_masked_hue_values(rgb_image)
if len(hue_1d_deg) == 0:
return 4, 'unknown'
hue_1d_rad = convert_to_hue_angle(hue_1d_deg)
return classify_color_by_range(hue_1d_rad)
def classify_color (image_np,box):
cropped_image = crop_roi_image(image_np, box)
result_color_index, _ = classify_color_cropped_image(cropped_image)
if result_color_index in [3,4]:
index = 10
else :
index = (result_color_index+91)
return index
def classify_all_boxes_in_image(image_np, boxes):
result_index_array = np.zeros(boxes.shape[0], dtype=np.int)
for i, box in enumerate(boxes):
cropped_image = crop_roi_image(image_np, box)
result_color_index, _ = classify_color_cropped_image(cropped_image)
result_index_array[i] = result_color_index
return result_index_array