-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.py
202 lines (173 loc) · 6.74 KB
/
gui.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import uuid
import cv2
import models
from glob import glob
class ParkingSpotSelector:
'''
The Parking Spot Selector provides a simply gui for selecting parking spaces
While running, use the mouse to drag over parking spots
Hit the 'r' key to refresh the spots
Hit the 'c' key to close
Parking spots are automatically persisted
Usage: ParkingSpotSelector('images/G0031388.JPG').start_loop()
'''
def __init__(self, filename):
'''
:param filename: string of an image path relative to the working directory
'''
self.image = cv2.imread(filename)
self.clone = self.image.copy()
self.parking_spots = models.ParkingSpotRoi()
self.parking_spots.load()
self.roi = None
self.window_name = uuid.uuid4().hex
def _select_roi(self, event, x, y, flags, param):
'''
Select Roi is used as a callback for selecting regions of interest (parking spots)
:param event: a cv2 event
:param x: the x coordinate of the mouse
:param y: the y coordinate of the mouse
:param flags: cv2 flags
:param param: cv2 param
'''
if event == cv2.EVENT_LBUTTONDOWN:
self.roi = [(x, y)]
elif event == cv2.EVENT_LBUTTONUP:
self.roi.append((x, y))
self.parking_spots.append(self.roi)
cv2.rectangle(self.image, self.roi[0], self.roi[1], (0, 255, 0), 4)
cv2.imshow(self.window_name, self.image)
def _reset_parking_spots(self):
'''
Reset our parking spots
'''
self.parking_spots.data = []
self.image = self.clone.copy()
def _setup_windows(self):
'''
Runs our initial set up, open the window show, show the image and draw the parking spots
'''
cv2.namedWindow(self.window_name, cv2.WINDOW_NORMAL)
cv2.imshow(self.window_name, self.image)
cv2.setMouseCallback(self.window_name, self._select_roi)
for spot in self.parking_spots:
cv2.rectangle(self.image, spot[0], spot[1], (0, 255, 0), 4)
def start_loop(self):
'''
Loop until user hits "c"
Reset parking spots on "r"
'''
self._setup_windows()
while True:
cv2.imshow(self.window_name, self.image)
key = cv2.waitKey(0) & 0xFF
if key == ord("r") or key == ord("R"):
self._reset_parking_spots()
elif key == ord("c") or key == ord("C"):
break
self.parking_spots.save()
cv2.destroyAllWindows()
class ParkingSpotClassifier:
'''
The Parking Spot Selector provides a simple gui for selecting parking spaces
While running, use the mouse to drag over parking spots
Hit the 'r' key to refresh the spots
Hit the 'c' key to close
Parking spots are automatically persisted
Usage: ParkingSpotClassifier('images/G0031388.JPG').start_loop()
'''
def __init__(self, filename):
'''
:param filename: string of an image path relative to the working directory
'''
self.image = cv2.imread(filename)
self.filename = filename
self.clone = self.image.copy()
self.parking_spots = models.ParkingSpotRoi()
self.parking_spots.load()
self.parking_spot_classification = models.ParkingSpotClassifications(filename)
self.parking_spot_classification.load()
self.window_name = uuid.uuid4().hex
def _toggle_spot(self, event, x, y, flags, param):
'''
Select Roi is used as a callback for selecting regions of interest (parking spots)
:param event: a cv2 event
:param x: the x coordinate of the mouse
:param y: the y coordinate of the mouse
:param flags: cv2 flags
:param param: cv2 param
'''
refresh = False
if event == cv2.EVENT_LBUTTONDOWN:
for i, roi in enumerate(self.parking_spots):
x_bounds = sorted([roi[0][0], roi[1][0]])
y_bounds = sorted([roi[0][1], roi[1][1]])
if x_bounds[0] <= x <= x_bounds[1] and y_bounds[0] <= y <= y_bounds[1]:
self.parking_spot_classification[i] = not self.parking_spot_classification[i]
refresh = True
if refresh:
self._draw_classifications()
def _draw_classifications(self):
'''
Draws the classications starting fresh
'''
self.image = self.clone.copy()
for i, spot in enumerate(self.parking_spots):
try:
self.parking_spot_classification[i]
except IndexError:
self.parking_spot_classification.append(False)
if self.parking_spot_classification[i]:
color = (0, 255, 0)
else:
color = (0, 0, 255)
cv2.rectangle(self.image, spot[0], spot[1], color, 4)
cv2.imshow(self.window_name, self.image)
def _setup_windows(self):
'''
Runs our initial set up, open the window show, show the image and draw the parking spots
'''
cv2.namedWindow(self.window_name, cv2.WINDOW_NORMAL)
cv2.imshow(self.window_name, self.image)
cv2.setMouseCallback(self.window_name, self._toggle_spot)
self._draw_classifications()
def start_loop(self):
'''
Loop until user hits "c"
Reset parking spots on "r"
'''
self._setup_windows()
while True:
key = cv2.waitKey(0) & 0xFF
if key == ord("r") or key == ord("R"):
self._reset_parking_spots()
elif key == ord("c") or key == ord("C"):
break
self.parking_spot_classification.save()
cv2.destroyAllWindows()
def classify_folder(folder, select_first=False):
image_files = glob(folder + '/*.jpg')
if select_first:
ParkingSpotSelector(image_files[0]).start_loop()
for f in image_files:
ParkingSpotClassifier(f).start_loop()
class ParkingSpotPrediction(ParkingSpotClassifier):
def _setup_windows(self):
'''
Runs our initial set up, open the window show, show the image and draw the parking spots
'''
cv2.namedWindow(self.window_name, cv2.WINDOW_NORMAL)
self._draw_classifications()
def _draw_classifications(self):
'''
Draws the classications starting
'''
self.image = self.clone.copy()
for i, spot in enumerate(self.parking_spots):
is_spot = models.ParkingSpotPredictor().predict(self.filename, spot, i)[0]
if is_spot:
color = (0, 255, 0)
else:
color = (0, 0, 255)
cv2.rectangle(self.image, spot[0], spot[1], color, 4)
cv2.imshow(self.window_name, self.image)