-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (74 loc) · 2.96 KB
/
main.py
File metadata and controls
95 lines (74 loc) · 2.96 KB
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
import os
from flask import Flask, render_template, request
import cv2
import numpy as np
import base64
app = Flask(__name__)
UPLOAD_FOLDER = os.path.basename('uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route("/")
def start_page():
print("Start")
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['image']
# Save file
#filename = 'static/' + file.filename
#file.save(filename)
# Read image
image = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_UNCHANGED)
# Detect faces
faces = detect_faces(image)
if len(faces) == 0:
faceDetected = False
num_faces = 0
to_send = ''
else:
faceDetected = True
num_faces = len(faces)
# Draw a rectangle
for item in faces:
draw_rectangle(image, item['rect'])
# Save
#cv2.imwrite(filename, image)
# In memory
image_content = cv2.imencode('.jpg', image)[1].tostring()
encoded_image = base64.encodestring(image_content)
to_send = 'data:image/jpg;base64, ' + str(encoded_image, 'utf-8')
return render_template('index.html', faceDetected=faceDetected, num_faces=num_faces, image_to_show=to_send, init=True)
# ----------------------------------------------------------------------------------
# Detect faces using OpenCV
# ----------------------------------------------------------------------------------
def detect_faces(img):
'''Detect face in an image'''
faces_list = []
# Convert the test image to gray scale (opencv face detector expects gray images)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Load OpenCV face detector (LBP is faster)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
# Detect multiscale images (some images may be closer to camera than others)
# result is a list of faces
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
# If not face detected, return empty list
if len(faces) == 0:
return faces_list
for i in range(0, len(faces)):
(x, y, w, h) = faces[i]
face_dict = {}
face_dict['face'] = gray[y:y + w, x:x + h]
face_dict['rect'] = faces[i]
faces_list.append(face_dict)
# Return the face image area and the face rectangle
return faces_list
# ----------------------------------------------------------------------------------
# Draw rectangle on image
# according to given (x, y) coordinates and given width and heigh
# ----------------------------------------------------------------------------------
def draw_rectangle(img, rect):
'''Draw a rectangle on the image'''
(x, y, w, h) = rect
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
if __name__ == "__main__":
# Only for debugging while developing
app.run(host='0.0.0.0', debug=True, port=80)