Skip to content

Commit 70b816a

Browse files
committed
commit
1 parent 3094936 commit 70b816a

11 files changed

+91
-179
lines changed
16 Bytes
Binary file not shown.

app.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import cv2
22
from flask import Flask, Response, render_template, request, redirect, session, url_for, flash, jsonify
33
from flask_mail import Mail, Message
4+
from numpy import diff
45
from werkzeug.utils import secure_filename
56
from facedetector import detectmask, detectface, faceencodingvalues, predata
67
import db
78
import os
9+
import cv2
10+
import datetime
811

912

1013

@@ -23,6 +26,9 @@
2326
@app.route("/home")
2427
def home():
2528
predata()
29+
global maskdetected
30+
global facedetected
31+
maskdetected,facedetected = False, False
2632
return render_template("index.html")
2733

2834

@@ -32,21 +38,25 @@ def home():
3238
def gen_frames():
3339
global cap,maskdetected,facedetected
3440
while True:
41+
if maskdetected == "wait" and facedetected == "wait":
42+
continue
3543
sucess,img = cap.read()
36-
if maskdetected == False and facedetected == False:
37-
ans = detectmask(img)
38-
if ans == "mask":maskdetected = True
39-
else:
40-
ans = detectface(img)
41-
if ans:facedetected = ans
44+
if facedetected == False:
45+
ans = detectface(img)
46+
if ans: facedetected = ans
47+
ret,buffer=cv2.imencode('.jpg',img)
4248
elif maskdetected == False:
43-
ans = detectmask(img)
49+
ans,box,pred = detectmask(img)
4450
if ans == "mask":maskdetected = True
45-
else:
46-
ans = detectface(img)
47-
if ans:facedetected = ans
48-
yield(b'--frame\r\n'
49-
b'Content-Type: image/jpeg\r\n\r\n' + img + b'\r\n')
51+
(startX, startY, endX, endY) = box
52+
color = (0, 255, 0) if ans == "mask" else (0, 0, 255)
53+
(mask, withoutMask) = pred
54+
cv2.putText(img, ans, (startX, startY - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
55+
cv2.rectangle(img, (startX, startY), (endX, endY), color, 2)
56+
ret,buffer=cv2.imencode('.jpg',img)
57+
58+
frame=buffer.tobytes()
59+
yield(b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
5060

5161
@app.route("/video_feed")
5262
def video_feed():
@@ -59,10 +69,29 @@ def recorded():
5969

6070
@app.route('/recorddone', methods = ['POST', 'GET'])
6171
def recorddone():
62-
global maskdetected,facedetected
63-
cap.release()
64-
maskdetected,facedetected = False, False
65-
return redirect(url_for('home'))
72+
print("recorddone")
73+
global maskdetected
74+
global facedetected
75+
ans = db.select("SELECT inside,fine FROM tarpusers WHERE email = '"+facedetected+"'")
76+
inside,fine = ans[0]
77+
if inside == "YES":
78+
now = datetime.datetime.now()
79+
now = now.strftime("%Y-%m-%d %H:%M:%S")
80+
db.insert("UPDATE tarpusers SET inside = '"+ now +"' WHERE email = '"+facedetected+"'")
81+
else:
82+
diff = datetime.datetime.now() - datetime.datetime.strptime(inside, "%Y-%m-%d %H:%M:%S")
83+
days, seconds = diff.days, diff.seconds
84+
hours = days * 24 + seconds // 3600
85+
if hours > 3:
86+
fine = fine + (hours - 3) * 10
87+
db.insert("UPDATE tarpusers SET fine = '"+ str(fine) +"', inside = 'YES' WHERE email = '"+facedetected+"'")
88+
else:db.insert("UPDATE tarpusers SET inside = 'YES' WHERE email = '"+facedetected+"'")
89+
# cap.release()
90+
cv2.destroyAllWindows()
91+
x = facedetected
92+
maskdetected,facedetected = "wait", "wait"
93+
return render_template("redirect.html", email = x,fine = fine)
94+
# return redirect(url_for('redirect'))
6695

6796
# ============================================================================================================
6897

@@ -121,7 +150,7 @@ def admin():
121150
@app.route("/logout")
122151
def logout():
123152
session.clear()
124-
return redirect(url_for("login"))
153+
return redirect(url_for("home"))
125154

126155

127156
if __name__ == '__main__':

facedetector.py

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010
allmails = []
1111

1212
def faceencodingvalues(img):
13-
print("===============start=====================================================")
1413
imgload = face_recognition.load_image_file(img)
1514
imgload = cv2.cvtColor(imgload,cv2.COLOR_BGR2RGB)
1615
try:
1716
faceloc = face_recognition.face_locations(imgload)[0] # (260, 825, 528, 557)
1817
except:
1918
return [],[]
2019
encodeimg = face_recognition.face_encodings(imgload)[0]
21-
# print("===============faceloc=====================================================")
22-
# print(faceloc)
23-
# print("==================encodeimg==================================================")
24-
# print(encodeimg)
2520

2621
return (encodeimg,faceloc)
2722

@@ -37,79 +32,22 @@ def predata():
3732
knownencodings = encodinglist
3833
allmails = emaillist
3934

40-
4135
def detectmask(img):
4236
(locs,preds) = detect_and_predict_mask(img)
4337
for (box,pred) in zip(locs,preds):
4438
(mask,withoutmask) = pred
45-
if mask > withoutmask:
46-
return "mask"
47-
else:
48-
return "withoutmask"
39+
if mask > withoutmask:return ("mask",box,pred)
40+
else:return ("withoutmask",box,pred)
4941

5042
def detectface(img):
5143
imgS = cv2.resize(img,(0,0),None,0.25,0.25)
5244
imgS = cv2.cvtColor(imgS,cv2.COLOR_BGR2RGB)
5345
facesS = face_recognition.face_locations(imgS)
5446
encodeS = face_recognition.face_encodings(imgS,facesS)
5547

56-
5748
for encodeFace,faceLoc in zip(encodeS,facesS):
5849
matches = face_recognition.compare_faces(knownencodings,encodeFace)
5950
faceDis = face_recognition.face_distance(knownencodings,encodeFace)
6051
matchindex = np.argmin(faceDis)
6152
if faceDis[matchindex]<0.6:
62-
return allmails[matchindex]
63-
# def detect(img,maskdetected,facedetected):
64-
# if maskdetected == False and facedetected == False:
65-
# (locs, preds) = detect_and_predict_mask(img)
66-
# for (box, pred) in zip(locs, preds):
67-
# (startX, startY, endX, endY) = box
68-
# (mask, withoutMask) = pred
69-
# if mask > withoutMask:
70-
# return "mask"
71-
# elif maskdetected == False and facedetected == True:
72-
# (locs, preds) = detect_and_predict_mask(img)
73-
# for (box, pred) in zip(locs, preds):
74-
# (startX, startY, endX, endY) = box
75-
# (mask, withoutMask) = pred
76-
# if mask > withoutMask:
77-
# return "mask"
78-
# else:
79-
80-
# label = "Mask" if mask > withoutMask else "No Mask"
81-
# color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
82-
# label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
83-
# cv2.putText(frame, label, (startX, startY - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
84-
# cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
85-
86-
# cv2.imshow('frame', frame)
87-
# if cv2.waitKey(1) & 0xFF == ord('q'):
88-
# break
89-
90-
91-
92-
# def detect(img):
93-
# imgS = cv2.resize(img,(0,0),None,0.25,0.25)
94-
# imgS = cv2.cvtColor(imgS,cv2.COLOR_BGR2RGB)
95-
# facesS = face_recognition.face_locations(imgS)
96-
# encodeS = face_recognition.face_encodings(imgS,facesS)
97-
98-
# for encodeFace,faceLoc in zip(encodeS,facesS):
99-
# matches = face_recognition.compare_faces(knownencodings,encodeFace)
100-
# faceDis = face_recognition.face_distance(knownencodings,encodeFace)
101-
# matchindex = np.argmin(faceDis)
102-
# # print("=======matchindex========================================")
103-
# print(matchindex)
104-
# # print(count)
105-
# print(matches[matchindex])
106-
# print(faceDis[matchindex])
107-
# # if matches[matchindex] and count == matchindex and faceDis[matchindex]<0.6:
108-
# if matches[matchindex] and faceDis[matchindex]<0.6:
109-
# print("================detect=================================================================")
110-
# ret,buffer = cv2.imencode('.jpg',img)
111-
# frame = buffer.tobytes()
112-
# return (frame,"YES")
113-
# ret,buffer = cv2.imencode('.jpg',img)
114-
# frame = buffer.tobytes()
115-
# return (frame,"NO")
53+
return allmails[matchindex]

facedetectors.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

maskdetect.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77

88
def detect_and_predict_mask(frame):
9-
maskNet = load_model(r"mask_detector.model")
10-
prototxtPath = r"deploy.prototxt"
11-
weightsPath = r"res10_300x300_ssd_iter_140000.caffemodel"
9+
maskNet = load_model(r"mask/mask_detector.model")
10+
prototxtPath = r"mask/deploy.prototxt"
11+
weightsPath = r"mask/res10_300x300_ssd_iter_140000.caffemodel"
1212
faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
1313

1414
(h, w) = frame.shape[:2]
@@ -40,23 +40,4 @@ def detect_and_predict_mask(frame):
4040
faces = np.array(faces, dtype="float32")
4141
preds = maskNet.predict(faces, batch_size=32)
4242

43-
return (locs, preds)
44-
45-
# while True:
46-
# ret, frame = cap.read()
47-
# frame = imutils.resize(frame, width=800)
48-
# frame = cv2.flip(frame, 1)
49-
# (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
50-
51-
# for (box, pred) in zip(locs, preds):
52-
# (startX, startY, endX, endY) = box
53-
# (mask, withoutMask) = pred
54-
# label = "Mask" if mask > withoutMask else "No Mask"
55-
# color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
56-
# label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
57-
# cv2.putText(frame, label, (startX, startY - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
58-
# cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
59-
60-
# cv2.imshow('frame', frame)
61-
# if cv2.waitKey(1) & 0xFF == ord('q'):
62-
# break
43+
return (locs, preds)

static/uploads/19BIT0134.jpg

130 KB
Loading

static/uploads/19BIT0321.jpg

92.3 KB
Loading

static/uploads/19BIT0338.jpeg

177 KB
Loading

static/uploads/19BIT0348.jpg

167 KB
Loading

templates/index.html

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010

1111
<body>
1212
<h1>face and mask detection</h1>
13+
<h3 id="facename"></h3>
1314

1415
<img src="{{ url_for('video_feed') }}" height="60%">
1516

17+
18+
<label for="">face detected</label>
19+
<input type="checkbox" id="face">
20+
<label for="">mask detected</label>
21+
<input type="checkbox" id="mask">
22+
23+
1624
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
1725
<script>
1826
$(document).ready(function () {
@@ -22,11 +30,17 @@ <h1>face and mask detection</h1>
2230
function refresh() {
2331
setTimeout(function () {
2432
$.getJSON('/recorded', function (data) {
25-
console.log(data);
26-
// if (data == "YES") {
27-
// window.location.href = "http://127.0.0.1:7890/recorddone";
28-
// }
29-
//$('#test').text(data);
33+
console.log(data.face,data.mask);
34+
document.getElementById('facename').innerHTML = data.face;
35+
if (data.face) $('#face').prop('checked', true);
36+
else $('#face').prop('checked', false);
37+
38+
if (data.mask) $('#mask').prop('checked', true);
39+
else $('#mask').prop('checked', false);
40+
if (data.face != false & data.mask == true) {
41+
//console.log('face and mask');
42+
window.location.href = "http://127.0.0.1:8000/recorddone";
43+
}
3044
});
3145
refresh();
3246
}, 1000);

templates/redirect.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<h1>{{ email }} recorded</h1>
11+
<h2>Fine {{ fine }} rs</h2>
12+
13+
<script>
14+
setTimeout(function () {
15+
window.location.href = "http://127.0.0.1:8000";
16+
}, 5000);
17+
</script>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)