-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
148 lines (109 loc) · 5.32 KB
/
app.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
import os
from werkzeug.utils import secure_filename
from urllib.request import Request
from flask import Flask, render_template, Response, request, redirect, flash
from Myfunctions import *
import urllib
import secrets
import cv2
secret = secrets.token_urlsafe(32)
app = Flask(__name__)
app.secret_key = secret
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/')
def index():
"""my main page"""
return render_template('index.html')
@app.route('/ImageStream')
def ImageStream():
"""the live page"""
return render_template('RealtimeImage.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/takeimage', methods=['POST'])
def takeimage():
""" Captures Images from WebCam, saves them and check face mask detection analysis """
_, frame = camera.read()
filename = "capture.png"
cv2.imwrite(f"static\{filename}", frame)
camera.release()
results = image_preprocessing(filename)
if results is None:
return render_template('Error.html')
else:
img_preds = results[0]
frame = results[1]
faces_detected = results[2]
results2 = predictions_results(img_preds, frame, faces_detected, filename)
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return render_template('PictureResult.html', user_image=full_filename,
number_of_face="Number of faces detected: {}".format(results2[0]),
no_mask_face="No face mask count: {}".format(results2[1]),
correct_mask_face="Correct face mask count: {}".format(results2[2]),
incorrect_mask_face="Incorrect face mask count: {}".format(results2[3]))
@app.route('/UploadImage', methods=['POST'])
def UploadImage():
"""the upload image page"""
return render_template('UploadPicture.html')
@app.route('/UploadImageFunction', methods=['POST'])
def UploadImageFunction():
if request.method == 'POST':
# Check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# If user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
# If user uploads the correct Image File
if file and allowed_file(file.filename):
# Pass it a filename and it will return a secure version of it.
# The filename returned is an ASCII only string for maximum portability.
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
results = image_preprocessing(filename)
if results is None:
return render_template('Error.html')
else:
img_preds = results[0]
frame = results[1]
faces_detected = results[2]
results2 = predictions_results(img_preds, frame, faces_detected, filename)
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return render_template('UploadPicture.html', user_image=full_filename,
number_of_face="Number of faces detected: {}".format(results2[0]),
no_mask_face="No face mask count: {}".format(results2[1]),
correct_mask_face="Correct face mask count: {}".format(results2[2]),
incorrect_mask_face="Incorrect face mask count: {}".format(results2[3]))
@app.route('/UploadURLImage', methods=['POST'])
def UploadURLImage():
"""the upload an image URL page"""
return render_template('UploadURLImage.html')
@app.route('/ImageUrl', methods=['POST'])
def ImageUrl():
"""the upload an image URL page"""
# Fetch the Image from the Provided URL
url = request.form['url']
filename = url.split('/')[-1]
if allowed_file(filename):
urllib.request.urlretrieve(url, f"static/{filename}")
results = image_preprocessing(filename)
if results is None:
return render_template('Error.html')
else:
img_preds = results[0]
frame = results[1]
faces_detected = results[2]
results2 = predictions_results(img_preds, frame, faces_detected, filename)
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return render_template('UploadURLImage.html', user_image=full_filename,
number_of_face="Number of faces detected: {}".format(results2[0]),
no_mask_face="No face mask count: {}".format(results2[1]),
correct_mask_face="Correct face mask count: {}".format(results2[2]),
incorrect_mask_face="Incorrect face mask count: {}".format(results2[3]))
if __name__ == '__main__':
app.run(debug=True)