-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
107 lines (90 loc) · 3.38 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
import numpy as np
import cv2, time, yaml
import tkinter as tk
from PIL import Image, ImageTk
from evaluation import get_prediction
from drawRect import drawRect
from send_sms import send_message
'''
evalTimer - used to measure elapsed time since last sent message
lastCapture - used to measure elapsed time since last frame evaluation
message_sent - tracks if a text message has been sent in the last 15 seconds
'''
evalTimer = time.time()
lastCapture = time.time()
message_sent = False
#Set up GUI
window = tk.Tk() #Makes main window
window.wm_title("Mask-Detect")
window.config(background="#312a2a")
#Graphics window
imageFrame = tk.Frame(window, width=600, height=500)
imageFrame.grid(row=0, column=0, padx=10, pady=2)
'''
checkEvalTime - checks if 15 seconds have elapsed from last text sent, if yes,
sets global message_sent to false and resets timer. If no message sent in last 15 seconds, breaks
'''
def checkEvalTime():
global evalTimer, message_sent
curTime = time.time()
if(message_sent and curTime - evalTimer > 15):
message_sent = False
evalTimer = time.time()
'''
checkEval - image (OpenCV) - opens credentials with phone number destination and twilio auth tokens.
Passes openCv image, writes it to local dir, and passes it through AutoML trained for mask states.
If conditions are met, will text alert to user based on the detected mask state.
'''
def checkEval(image):
creds = yaml.safe_load(open("creds.yaml", "r"))
toNum = creds['to']
#overwrites the previous image captured
cv2.imwrite('Active_Captures/frame.jpg', image)
label, x1, y1, x2, y2 = get_prediction('Active_Captures/frame.jpg')
global message_sent
if label != None and label != 'mask':
if (message_sent == False):
img = drawRect('Active_Captures/frame.jpg', label, x1, y1, x2, y2)
if label == 'improper_mask':
msg = 'Improper Mask Detected: Better get that mask over your nose!'
send_message(msg, toNum=toNum, image=img)
message_sent = True
evalTimer = time.time()
else:
msg = 'No Mask Detected: STOP! You need your mask!'
send_message(msg, toNum=toNum, image=img)
message_sent = True
evalTimer = time.time()
#Capture video frames
lmain = tk.Label(imageFrame)
lmain.grid(row=0, column=0)
#initializes video feed w/ OpenCV
cap = cv2.VideoCapture(0)
'''
show_frame - displays frame to the Tkinter gui window. If 2 seconds have passed,
passes current frame to be evaluated by AutoML.
'''
def show_frame():
global lastCapture, evalTimer
_, frame = cap.read()
cv2image = cv2.flip(frame, 1)
checkEvalTime()
if(message_sent == False):
evalTimer = time.time()
#cv2 image is the frame we want to analyze
#only saves img and analyzes every 2 seconds
if time.time() - lastCapture > 2:
checkEval(cv2image)
lastCapture = time.time()
img = Image.fromarray(cv2image)
r, g, b = img.split()
img = Image.merge('RGB', (b, g, r))
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(10, show_frame)
#Slider window (slider controls stage position)
#sliderFrame = tk.Frame(window, width=600, height=100)
#sliderFrame.grid(row = 600, column=0, padx=10, pady=2)
show_frame() #Display 2
window.mainloop() #Starts GUI