1
- from flask import Flask , request , render_template , flash , redirect , url_for , send_file
1
+ from flask import Flask , request , render_template , flash , redirect , url_for , send_file , Response
2
2
from werkzeug .utils import secure_filename
3
3
from datetime import datetime
4
4
from tensorflow .keras .applications .mobilenet_v2 import preprocess_input
16
16
net = cv2 .dnn .readNet (prototxtPath , weightsPath )
17
17
model = load_model ('mask_detector.model' )
18
18
19
+ def detect_and_predict_mask (frame ,faceNet ,maskModel ):
20
+ (h ,w )= frame .shape [:2 ]
21
+ blob = cv2 .dnn .blobFromImage (frame ,1.0 ,(300 ,300 ),(104.0 ,177.0 ,123.0 ))#normalization
22
+ faceNet .setInput (blob )
23
+ detections = faceNet .forward ()
24
+
25
+ faces = []
26
+ locs = []
27
+ preds = []
28
+
29
+ for i in range (0 ,detections .shape [2 ]):
30
+ confidence = detections [0 ,0 ,i ,2 ]
31
+ if confidence > 0.5 :
32
+ box = detections [0 ,0 ,i ,3 :7 ]* np .array ([w ,h ,w ,h ])
33
+ (start_X ,start_Y , end_X , end_Y )= box .astype ('int' )
34
+ (start_X ,start_Y )= (max (0 ,start_X ),max (0 ,start_Y ))
35
+ (end_X ,end_Y )= (min (w - 1 ,end_X ),min (h - 1 ,end_Y ))
36
+ face = frame [start_Y :end_Y , start_X :end_X ]
37
+ face = cv2 .cvtColor (face ,cv2 .COLOR_BGR2RGB )
38
+ face = cv2 .resize (face , (224 ,224 ))
39
+ face = img_to_array (face )
40
+ face = preprocess_input (face )
41
+ faces .append (face )
42
+ locs .append ((start_X ,start_Y ,end_X ,end_Y ))
43
+
44
+ if len (faces )> 0 :
45
+ faces = np .array (faces , dtype = "float32" )
46
+ preds = maskModel .predict (faces )
47
+
48
+ return (locs ,preds )
49
+
19
50
def allowed_file (filename ):
20
51
return '.' in filename and \
21
52
filename .rsplit ('.' , 1 )[1 ].lower () in ['png' , 'jpg' , 'jpeg' ]
@@ -80,5 +111,51 @@ def download(filename):
80
111
# Returning file from appended path
81
112
return send_file ( os .path .join ('static/online_detector_results' ,filename ), as_attachment = True )
82
113
114
+ @app .route ('/streaming' )
115
+ def streaming ():
116
+ return render_template ('streaming.html' )
117
+
118
+
119
+ #video streaming
120
+
121
+ video = cv2 .VideoCapture (0 )
122
+
123
+ def gen_frames ():
124
+ while True :
125
+ success , image = video .read ()
126
+ if not success :
127
+ break
128
+ else :
129
+ (oheight , owidth ) = image .shape [:2 ]
130
+ adjusted_width = 400
131
+ adjusted_height = int (adjusted_width * oheight / owidth )
132
+ cv2 .imshow ("image" , image )
133
+ image = cv2 .resize (image , (adjusted_width , adjusted_height ), interpolation = cv2 .INTER_AREA )
134
+ (locs ,preds )= detect_and_predict_mask (image ,net ,model )
135
+
136
+ for (loc ,pred ) in zip (locs ,preds ):
137
+ (start_X ,start_Y ,end_X ,end_Y )= loc
138
+ (with_mask ,without_mask )= pred
139
+ if with_mask > without_mask :
140
+ label = "Mask"
141
+ color = (0 ,255 ,0 )
142
+ else :
143
+ label = "No Mask"
144
+ color = (0 ,0 ,255 )
145
+
146
+ label = "{}:{:.2f}%" .format (label , max (with_mask ,without_mask )* 100 )
147
+ cv2 .putText (image ,label ,(start_X ,start_Y - 10 ),cv2 .FONT_HERSHEY_SIMPLEX ,0.45 ,color ,2 )
148
+ cv2 .rectangle (image , (start_X ,start_Y ),(end_X ,end_Y ),color ,2 )
149
+
150
+ ret , jpeg = cv2 .imencode ('.jpg' , image )
151
+ frame = jpeg .tobytes ()
152
+
153
+ yield (b'--frame\r \n '
154
+ b'Content-Type: image/jpeg\r \n \r \n ' + frame + b'\r \n \r \n ' )
155
+
156
+ @app .route ('/video_feed' )
157
+ def video_feed ():
158
+ return Response (gen_frames (), mimetype = 'multipart/x-mixed-replace; boundary=frame' )
159
+
83
160
if __name__ == '__main__' :
84
161
app .run (debug = True )
0 commit comments