-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexit_detection.py
184 lines (162 loc) · 8.43 KB
/
exit_detection.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
from utility import *
import pymysql
from datetime import datetime
car_threshold_value = 1000
cap = cv2.VideoCapture(0)
temp_car_plate_number = ""
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
canny = auto_canny(blurred)
canny_value = cv2.countNonZero(canny)
# print(canny_value)
# For debugging purpose. Checks onto cloud vision api
# spacebar to take photo
# k = cv2.waitKey(1)
# if k%256 == 32:
# cv2.imwrite("exit_car.jpg", frame)
# car_plate_number = detect_text("exit_car.jpg")
# print("Exit Car Plate Number : " , car_plate_number)
#
# # if is not null
# if car_plate_number is not None:
#
# # check car plate number is same with previous
# if temp_car_plate_number != car_plate_number:
#
# conn = pymysql.connect(host=host, user=user, password=pw, db=db)
# cursor = conn.cursor()
#
# select_sql = "SELECT records_ID, start_time FROM parking_records WHERE plate_number = (%s) AND end_time IS NULL"
# cursor.execute(select_sql, car_plate_number)
# print("Found records : ", cursor.rowcount)
#
# # check got matching result or not in parking_records
# # plate_number must match end_time is null
# if cursor.rowcount is not 0:
# results = cursor.fetchone()
# records_ID = results[0]
# start_time = results[1]
# end_time = datetime.time(datetime.now().replace(microsecond=0)).isoformat()
#
# print("records_ID: ", records_ID)
# print("start_time: ", start_time)
# print("end_time: ", end_time)
#
# # get time interval (duration)
# FMT = '%H:%M:%S'
# duration = datetime.strptime(end_time, FMT) - datetime.strptime(start_time, FMT)
# print("Duration: ", duration)
#
# # get individual duration. hour minutes seconds
# string_duration = list(map(int, f'{duration}'.split(":")))
# hour = string_duration[0]
# minutes = string_duration[1]
# seconds = string_duration[2]
#
# # retrieve active(1) parking rates
# rates_sql = "SELECT active, first_hour, following_two_hour, following_fourth_hour FROM parking_rates WHERE active = 1"
# cursor.execute(rates_sql)
#
# # if got result from query retrieve active parking rates
# if cursor.rowcount is not 0:
# results = cursor.fetchone()
# first_hour = results[1]
# following_two_hour = results[2]
# following_fourth_hour = results[3]
#
# # calculate parking fees
# parking_fees = 0
# if hour < 1:
# parking_fees = first_hour
# elif hour <= 2:
# parking_fees = first_hour + following_two_hour
# elif hour >= 3:
# parking_fees = first_hour + following_two_hour
# for parking_fees in range(hour):
# parking_fees += following_fourth_hour
#
# # update the parking record with end_time, duration, and parking_fees
# update_sql = "UPDATE parking_records SET end_time = (%s), duration = (%s), paid_parking_fee = (%s) WHERE records_ID = (%s)"
# cursor.execute(update_sql,
# (end_time, f'{duration}', parking_fees, records_ID))
# conn.commit()
# print("Updated record")
# print("Open Barrier")
# # TODO Auto pay at mobile app then only update records
# print("Close Barrier")
# temp_car_plate_number = car_plate_number
# The real detection starts here
# Check got car exit or not
if canny_value > car_threshold_value:
# print("got exit car")
cv2.imwrite("exit_car.jpg", frame)
car_plate_number = detect_text("enter_car.jpg")
print("Exit Car Plate Number : " , car_plate_number)
# if is not null
if car_plate_number is not None:
# check car plate number is same with previous
if temp_car_plate_number != car_plate_number:
conn = pymysql.connect(host=host, user=user, password=pw, db=db)
if conn.open:
cursor = conn.cursor()
select_sql = "SELECT records_ID, start_time FROM parking_records WHERE plate_number = (%s) AND end_time IS NULL"
cursor.execute(select_sql, car_plate_number)
print("Found records : ", cursor.rowcount)
# check got matching result or not in parking_records
# plate_number must match end_time is null
if cursor.rowcount is not 0:
results = cursor.fetchone()
records_ID = results[0]
start_time = results[1]
end_time = datetime.time(datetime.now().replace(microsecond=0)).isoformat()
print("records_ID: ", records_ID)
print("start_time: ", start_time)
print("end_time: ", end_time)
# get time interval (duration)
FMT = '%H:%M:%S'
duration = datetime.strptime(end_time, FMT) - datetime.strptime(start_time, FMT)
print("Duration: ", duration)
# get individual duration. hour minutes seconds
string_duration = list(map(int, f'{duration}'.split(":")))
hour = string_duration[0]
minutes = string_duration[1]
seconds = string_duration[2]
# retrieve active(1) parking rates
rates_sql = "SELECT active, first_hour, following_two_hour, following_fourth_hour FROM parking_rates WHERE active = 1"
cursor.execute(rates_sql)
# if got result from query retrieve active parking rates
if cursor.rowcount is not 0:
results = cursor.fetchone()
first_hour = results[1]
following_two_hour = results[2]
following_fourth_hour = results[3]
# calculate parking fees
parking_fees = 0
if hour < 1:
parking_fees = first_hour
elif hour <= 2:
parking_fees = first_hour + following_two_hour
elif hour >= 3:
parking_fees = first_hour + following_two_hour
for parking_fees in range(hour):
parking_fees += following_fourth_hour
# update the parking record with end_time, duration, and parking_fees
update_sql = "UPDATE parking_records SET end_time = (%s), duration = (%s), paid_parking_fee = (%s) WHERE records_ID = (%s)"
cursor.execute(update_sql,
(end_time, f'{duration}', parking_fees, records_ID))
conn.commit()
print("Updated record")
print("Open Barrier")
# TODO Auto pay at mobile app then only update records
print("Close Barrier")
temp_car_plate_number = car_plate_number
else:
print("Connection with db is not open")
cv2.imshow('Final Outcome', frame)
# cv2.imshow('canny', canny)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()