-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (61 loc) · 2.36 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
import os
import cv2
import numpy as np
from keras.models import load_model
from flask import Flask, render_template, Response
app = Flask(__name__)
# camera = cv2.VideoCapture(0)
if os.environ.get("WERKZEUG_RUN_MAIN") or Flask.debug is False:
camera = cv2.VideoCapture(0)
labels_dict = {0: 'MASK', 1: 'NO MASK'}
color_dict = {0: (0, 255, 0), 1: (0, 0, 255)}
classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
model = load_model('model-090.model')
def genFrames():
while True:
success, frame = camera.read()
frame = cv2.resize(frame, None, fx=0.6, fy=0.6, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_rects = classifier.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in face_rects:
face_img = gray[y+(abs(w - h)):y+h, x:x+w]
resized = cv2.add(cv2.resize(face_img, (100, 100)), 60)
normalized = resized/255.0
reshaped = np.reshape(normalized, (1, 100, 100, 1))
result = model.predict(reshaped)
label = np.argmax(result, axis=1)[0]
cv2.rectangle(frame, (x, y), (x+w, y+h), color_dict[label], 4)
cv2.rectangle(frame, (x, y-40), (x+w, y), color_dict[label], 4)
cv2.putText(frame, labels_dict[label], (x, y-10),
cv2.FONT_ITALIC, 1, (255, 255, 255), 4)
break
ret, buffer = cv2.imencode(".jpg", frame)
frame = buffer.tobytes()
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n")
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/blog")
def blog():
return render_template("blog.html")
@app.route("/service")
def service():
return render_template("service.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/trynow")
def trynow():
return render_template("TryNow.html")
def gen(camera):
print("gen")
frame = camera.get_frame()
yield (b"--frame\r\n" b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n\r\n")
@app.route("/video_feed")
def video_feed():
return Response(genFrames(), mimetype="multipart/x-mixed-replace; boundary=frame")
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)