-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (125 loc) · 4.92 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
#Modified by smartbuilds.io
#Date: 27.09.20
#Desc: This web application serves a motion JPEG stream
# main.py
# import the necessary packages
from flask import Flask, render_template, Response, request
# from camera import VideoCamera
import time
import threading
import os
import cv2
from flask import jsonify
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
import numpy as np
import pandas as pd
import time
from PIL import Image
# import serial
from datetime import datetime
# pi_camera = VideoCamera(flip=False) # flip pi camera if upside down.
# camera = cv2.VideoCapture('http://192.168.100.3:4747/video')
df = pd.DataFrame(columns=['mask','no_mask','time', 'image', 'prediction_confidence'])
cascPath = "models/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
model = load_model("models/mask_recog_ver2.h5")
# video_capture = cv2.VideoCapture('http://192.168.100.3:4747/video')
# App Globals (do not edit)
app = Flask(__name__)
@app.route('/')
def index():
return "HELLO WORLD!" #you can customze index.html here
# def gen(camera):
# #get camera frame
# while True:
# ret, frame = camera.read()
# frame = mask_detection(frame, True)
# ret, img = cv2.imencode('.jpg', frame)
# img = img.tobytes()
# yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + img + b'\r\n\r\n')
def genRequest(frame):
#get camera frame
prediction = mask_detection(frame, False)
return prediction
# @app.route('/video_feed')
# def video_feed():
# return Response(gen(camera),
# mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/prediction', methods = ['POST'])
def prediction():
if request.method == 'POST':
if 'file' not in request.files:
return {'error': 'No image submited'}
try:
f = request.files['file'].read()
npimg = np.fromstring(f, np.uint8)
frame = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
result = genRequest(frame)
return result
except Exception as e:
return e
def add_mask():
global df
now=datetime.now()
current_time = now.strftime("%H:%M:%S")
img_path = '/with_mask/hasmask{}.jpg'.format(str(datetime.now()))
pred = "{:.2f}%".format(max(mask, withoutMask) * 100)
new_row={'mask':1, 'no_mask':0, 'time':current_time, 'image':img_path, 'prediction_confidence':pred}
# cv2.imwrite(img_path,frame)
# df=df.append(new_row, ignore_index=True)
# df.to_csv('maskdata.csv', index = True)
def add_nomask():
global df
now=datetime.now()
current_time = now.strftime("%H:%M:%S")
img_path = '/without_mask/hasmask{}.jpg'.format(str(datetime.now()))
pred = "{:.2f}%".format(max(mask, withoutMask) * 100)
new_row={'mask':0, 'no_mask':1, 'time':current_time, 'image':img_path, 'prediction_confidence':pred}
# cv2.imwrite(img_path,frame)
# df=df.append(new_row, ignore_index=True)
# df.to_csv('maskdata.csv', index = True)
def mask_detection(frame, image_flag):
# Capture frame-by-frame
# ret, frame = video_capture.read()
label = ''
try:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
faces_list=[]
preds=[]
for (x, y, w, h) in faces:
face_frame = frame[y:y+h,x:x+w]
face_frame = cv2.cvtColor(face_frame, cv2.COLOR_BGR2RGB)
face_frame = cv2.resize(face_frame, (224, 224))
face_frame = img_to_array(face_frame)
face_frame = np.expand_dims(face_frame, axis=0)
face_frame = preprocess_input(face_frame)
faces_list.append(face_frame)
if len(faces_list)>0:
preds = model.predict(face_frame)
else:
return "NO FACES DETECTED"
for pred in preds:
(mask, withoutMask) = pred
label = "Mask" if mask > withoutMask else "No Mask"
# color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
print(label)
# cv2.putText(frame, label, (x, y- 10),
# cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
# cv2.rectangle(frame, (x, y), (x + w, y + h),color, 2)
if image_flag:
return frame
else:
return jsonify({"result": label})
except Exception as e:
return e
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)