File size: 3,147 Bytes
0d9713a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86ec2f8
0d9713a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f60c09
0d9713a
 
 
 
86ec2f8
 
9f60c09
0d9713a
 
 
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
from flask import Flask, render_template, request, jsonify
import cv2
from mtcnn import MTCNN
import time
import concurrent.futures
import base64
import numpy as np

app = Flask(__name__)

def load_and_detect_haar(gray_img):
    ff = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    ff_alt = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml')
    ff_alt2 = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml')
    pf = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_profileface.xml')

    ff_faces = ff.detectMultiScale(gray_img, scaleFactor=1.2, minNeighbors=10, minSize=(25, 25))
    ff_alt2_faces = ff_alt2.detectMultiScale(gray_img, scaleFactor=1.05, minNeighbors=10, minSize=(20, 20))
    pf_faces = pf.detectMultiScale(gray_img, scaleFactor=1.05, minNeighbors=5, minSize=(20, 20))

    return ff_faces, ff_alt2_faces, pf_faces

def load_and_detect_mtcnn(image):
    mtcnn = MTCNN()
    faces = mtcnn.detect_faces(image)
    mt_faces = [face['box'] for face in faces]
    return mt_faces

def get_unique_face_locations(all_face_locations):
    unique_detected_faces = []
    for (x1, y1, w1, h1) in all_face_locations:
        unique = True
        for (x2, y2, w2, h2) in unique_detected_faces:
            if abs(x1 - x2) < 50 and abs(y1 - y2) < 50:
                unique = False
                break
        if unique:
            unique_detected_faces.append((x1, y1, w1, h1))
    
    return unique_detected_faces

@app.route('/')
def index():
    return render_template('pg1.html')

@app.route('/camera')
def camera():
    return render_template("pg2.html")

@app.route('/report')
def report():
    return render_template("pg4.html")

@app.route('/detect_faces', methods=['POST'])
def detect_faces():
    start = time.perf_counter()
    filestr = request.files["img_upload"].read()
    file_bytes = np.fromstring(filestr, np.uint8) 
    img = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    with concurrent.futures.ThreadPoolExecutor() as executor:
        haar_detections = executor.submit(load_and_detect_haar, gray)
        mtcnn_detections = executor.submit(load_and_detect_mtcnn, img)
        
        ff_faces, ff_alt2_faces, pf_faces = haar_detections.result()
        mt_faces = mtcnn_detections.result()

        all_faces = [*ff_faces, *ff_alt2_faces, *pf_faces, *mt_faces]
        unique_detected_faces = get_unique_face_locations(all_faces)

        for (x, y, w, h) in unique_detected_faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)

        # img = cv2.putText(img, f"{len(unique_detected_faces)} Faces", (100, 300), cv2.FONT_HERSHEY_SIMPLEX, 6, (0, 0, 0), 10)

    _, buffer = cv2.imencode(".jpg", cv2.resize(img, (0, 0), fx=0.5, fy=0.5))
    b64 = base64.b64encode(buffer)

    print(f"\n\n Request Took {time.perf_counter() - start:.2f} seconds to complete \n\n")

    return render_template("pg1.html", img_data = b64.decode('utf-8'), faces=len(unique_detected_faces))

if __name__ == '__main__':
    app.run(debug=False)