-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
84 lines (56 loc) · 2.25 KB
/
main.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
import cv2
import matplotlib.pyplot as plt
import numpy as np
from util import get_parking_spots_bboxes, empty_or_not
def calc_diff(im1, im2):
return np.abs(np.mean(im1) - np.mean(im2))
mask = './mask_1920_1080.png'
video_path = './samples/parking_1920_1080_loop.mp4'
mask = cv2.imread(mask, 0)
cap = cv2.VideoCapture(video_path)
connected_components = cv2.connectedComponentsWithStats(mask, 4, cv2.CV_32S)
spots = get_parking_spots_bboxes(connected_components)
spots_status = [None for j in spots]
diffs = [None for j in spots]
previous_frame = None
frame_nmr = 0
ret = True
step = 30
while ret:
ret, frame = cap.read()
if frame_nmr % step == 0 and previous_frame is not None:
for spot_indx, spot in enumerate(spots):
x1, y1, w, h = spot
spot_crop = frame[y1:y1 + h, x1:x1 + w, :]
diffs[spot_indx] = calc_diff(spot_crop, previous_frame[y1:y1 + h, x1:x1 + w, :])
print([diffs[j] for j in np.argsort(diffs)][::-1])
if frame_nmr % step == 0:
if previous_frame is None:
arr_ = range(len(spots))
else:
arr_ = [j for j in np.argsort(diffs) if diffs[j] / np.amax(diffs) > 0.4]
for spot_indx in arr_:
spot = spots[spot_indx]
x1, y1, w, h = spot
spot_crop = frame[y1:y1 + h, x1:x1 + w, :]
spot_status = empty_or_not(spot_crop)
spots_status[spot_indx] = spot_status
if frame_nmr % step == 0:
previous_frame = frame.copy()
for spot_indx, spot in enumerate(spots):
spot_status = spots_status[spot_indx]
x1, y1, w, h = spots[spot_indx]
if spot_status:
frame = cv2.rectangle(frame, (x1, y1), (x1 + w, y1 + h), (0, 255, 0), 2)
else:
frame = cv2.rectangle(frame, (x1, y1), (x1 + w, y1 + h), (0, 0, 255), 2)
cv2.rectangle(frame, (80, 20), (550, 80), (0, 0, 0), -1)
cv2.putText(frame, 'Available spots: {} / {}'.format(str(sum(spots_status)), str(len(spots_status))), (100, 60),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.imshow('frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
frame_nmr += 1
cap.release()
cv2.destroyAllWindows()