-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
285 lines (255 loc) · 10.9 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import os
import smtplib
import imghdr
import cv2
import numpy as np
import time
import darknet
import sys
from email.message import EmailMessage
from email.mime.text import MIMEText
from fpdf import FPDF
from datetime import datetime
# Early exit
if len(sys.argv)<2:
printf("Enter a video input path!")
exit()
videoInput=sys.argv[1]
#determine time
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
#email initialization
EMAIL_USER = "[email protected]"
EMAIL_PASSWORD = "Cis3296!"
msg = EmailMessage()
msg['Subject'] = 'Delivery detected'
msg['From'] = EMAIL_USER
with open('userEmail.txt','r') as userEmail:
emailAddress = userEmail.read()
msg['To'] = emailAddress
#Section to convert a text file to a pdf
#======================================================================#
# def txtToPDF(filePath):
# pdf = FPDF()
# pdf.add_page()
# pdf.set_font("Arial", size = 10)
# f = open(filePath, "r")
# for i in f:
# pdf.cell(0,txt = i, ln = 2)
# return pdf.output("./results/output.pdf")
#=======================================================================#
#Section to prepare sending an email
#=======================================================================#
def sendIMG(filePath):
attachImage(filePath)
def attachImage(filePath):
with open(filePath,'rb') as f:
file_data=f.read()
file_type=imghdr.what(f.name)
file_name=f.name
msg.add_attachment(file_data,maintype='image',subtype=file_type,filename=file_name)
def sendPDF(filePath):
attachPDF(filePath)
def attachPDF(filePath):
with open(filePath,'rb') as f:
file_data=f.read()
file_name=f.name
msg.add_attachment(file_data,maintype='application',subtype='octet-stream',filename=file_name)
#Method to send email with attachments as parameters
def sendAttachments(imgPath, logFilePath,label,confidence):
if confidence>float(89.9):
msg.set_content('A '+label+' was detected at ' + current_time+ '. Please check attached text file for more information!')
else:
msg.set_content('Looks like a '+label+' stopped by your door at ' + current_time+ '. Please check attached text file for more information!')
sendIMG(imgPath)
sendPDF(logFilePath)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_USER, EMAIL_PASSWORD)
smtp.send_message(msg)
#sendAttachments('test.jpg', txtFile)
"""# Create an object of sendpdf function
k = sendpdf(sender_email_address,
receiver_email_address,
sender_email_password,
subject_of_email,
body_of_email,
filename,
location_of_file)
# sending an email
k.email_send()"""
#=======================================================================#
############################
#write detections to log file
############################
def logging(label, confidence):
logfile = "detection_logs/"
logfile += datetime.today().strftime('%Y-%m-%d')+".txt"
txtFile = logfile
f = open(logfile, "a")
output = datetime.now().strftime('%H:%M')
if confidence >= float(90):
output += "\tDelivery driver detected!\n"
else:
output += "\tDelivery driver may have been detected!\n"
output += "\t{}: {}% confident".format(label, confidence) + "\n\n"
f.write(output)
f.close
return logfile
# sendAttachments(imgPath, file,label,confidence)
#################################################
# Drawing box on img according to the detection
#################################################
def convert_to_coordinate(x, y, w, h):
left = int(round(x - (w / 2)))
right = int(round(x + (w / 2)))
top = int(round(y - (h / 2)))
bot = int(round(y + (h / 2)))
return left,right,bot,top
# def cvDrawBoxes(detections, img):
# # Colored labels dictionary
# color_dict = {
# 'amazonCourier' : [0, 255, 255], 'fedexCourier': [238, 123, 158], 'ups_courier' : [24, 245, 217], 'uspsCourier' : [224, 119, 227]
# }
# for detection in detections:
# # print("detections[0]: " + str(detection[0]))
# # print("detections[1]: " + str(detection[1]))
# x, y, w, h = detection[2][0],\
# detection[2][1],\
# detection[2][2],\
# detection[2][3]
# name_tag = str(detection[0])#.decode())
# for name_key, color_val in color_dict.items():
# if name_key == name_tag:
# color = color_val
# xmin, ymin, xmax, ymax = convertBack(
# float(x), float(y), float(w), float(h))
# pt1 = (xmin, ymin)
# pt2 = (xmax, ymax)
# cv2.rectangle(img, pt1, pt2, color, 1)
# cv2.putText(img,
# detection[0] +
# " [" + str(detection[1]) + "]",
# (pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
# color, 2)
# return img
def drawBoxes(detections,img):
box_color = {
'amazonCourier':[0,168,225],'fedexCourier':[75,19,136],'ups_courier':[46,20,6],'uspsCourier':[36,60,141]
}
text_color = {
'amazonCourier':[255,255,255],'fedexCourier':[240,80,15],'ups_courier':[242,178,10],'uspsCourier':[255,255,255]
}
formal_label = {
'amazonCourier':'Amazon courier','fedexCourier':'Fedex courier','ups_courier':'UPS driver','uspsCourier':'USPS Driver'
}
for detection in detections:
label=str(detection[0])
frame_color=box_color[label]
msg_color=text_color[label]
tag = formal_label[label]
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3]
left,right,bot,top = convert_to_coordinate(float(x), float(y), float(w), float(h))
pt1=(left,bot)
pt2=(right,top)
msg = tag+' ' + str(detection[1])+ '%'
(text_width, text_height) = cv2.getTextSize(msg, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.9,thickness=2)[0]
cv2.rectangle(img,
pt1,(left+text_width,bot-text_height-10),
frame_color,
cv2.FILLED)
cv2.rectangle(img,
pt1,pt2,
frame_color,
2)
cv2.putText(img,
msg,
(left,bot - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.9,
msg_color,2)
return img
##################################
#The main part
##################################
def main():
# YOLO and darknetconfiguration
configPath = "./cfg/knockknock_cfg.cfg" # Path to cfg
weightPath = "./knockknock_cfg_best.weights" # Path to weights
metaPath = "./data/obj.data" # Path to meta data
if not os.path.exists(configPath): # Checks whether file exists otherwise return ValueError
raise ValueError("Invalid config path `" +
os.path.abspath(configPath)+"`")
if not os.path.exists(weightPath):
raise ValueError("Invalid weight path `" +
os.path.abspath(weightPath)+"`")
if not os.path.exists(metaPath):
raise ValueError("Invalid data file path `" +
os.path.abspath(metaPath)+"`")
network, class_names, class_colors = darknet.load_network(
configPath,
metaPath,
weightPath,
batch_size=1
)
# rtsp = rtsp://admin:[email protected]:554
#cap = cv2.VideoCapture(rtsp) To run on camera
#cap = cv2.VideoCapture(0) # Uncomment to use Webcam
cap = cv2.VideoCapture(videoInput) # Local Stored video detection - Set input video
frame_width = int(cap.get(3)) # Returns the width and height of capture video
frame_height = int(cap.get(4))
# Set out for video writer
fileName = datetime.now().strftime("%B-%d-%y_%H:%M:%S")
outputPath="./results/"+fileName+".avi"
out = cv2.VideoWriter( # Set the Output path for video writer
outputPath, cv2.VideoWriter_fourcc(*"MJPG"), 10.0,
(frame_width, frame_height))
print("Analyze starts..")
darknet_image = darknet.make_image(frame_width, frame_height, 3) # Create image according darknet for compatibility of network
x=float(0)
while True: # Load the input frame and write output frame.
prev_time = time.time()
# print("line 107 pass")
ret, frame_read = cap.read() # Capture frame and return true if frame present
# print("line 109 pass")
# For Assertion Failed Error in OpenCV
if not ret: # Check if frame present otherwise he break the while loop
break
frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # Convert frame into RGB from BGR and resize accordingly
#print("line 116 pass")
frame_resized = cv2.resize(frame_rgb,
(frame_width, frame_height),
interpolation=cv2.INTER_LINEAR)
#print("line 117 pass")
darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) # Copy that frame bytes to darknet_image
#print("line 121 pass")
# detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.5) # Detection occurs at this line and return detections, for customize we can change the threshold.
detections = darknet.detect_image(network, class_names, darknet_image, thresh=0.8)
#print("line 124 pass")
image = drawBoxes(detections, frame_resized) # Call the function cvDrawBoxes() for colored bounding box per class
#print("line 126 pass")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if detections:
if float(detections[0][1])>x:
snap=image
detection=detections
x=float(detections[0][1])
# print(1/(time.time()-prev_time))
#cv2.imshow('Demo', image) # Display Image window
cv2.waitKey(3)
out.write(image) # Write that frame into output video
# print("detections[0]: " + str(detections[0][0]))
cap.release() # For releasing cap and out.
out.release()
if x>float(80):
snapPath="./results/"+fileName+".jpg"
cv2.imwrite(snapPath,snap)
label=str(detection[0][0])
confidence=float(detection[0][1])
logFilePath=logging(label,confidence)
sendAttachments(snapPath, logFilePath,label,confidence)
print("Analyze Completed")
if __name__ == "__main__":
main()