File size: 3,416 Bytes
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
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
# from flask import Flask, render_template, Response
# import cv2
# from mtcnn import MTCNN
# import base64
# import numpy as np
# import concurrent.futures

# app = Flask(__name__)

# class FaceDetector:
#     def __init__(self):
#         self.detector = MTCNN()
#         self.face_count = 0

#     def detect_faces(self, frame):
#         faces = self.detector.detect_faces(frame)
#         self.face_count = len(faces)
#         return faces

# face_detector = FaceDetector()

# def generate_frames():
#     camera = cv2.VideoCapture(0)  # 0 indicates the default camera (you can change it to the camera index you want to use)

#     while True:
#         success, frame = camera.read()
#         if not success:
#             break

#         gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#         # Perform face detection
#         faces = face_detector.detect_faces(frame)

#         for face in faces:
#             x, y, w, h = face['box']
#             cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

#         # Display face count on each frame
#         font = cv2.FONT_HERSHEY_SIMPLEX
#         cv2.putText(frame, f'Faces: {face_detector.face_count}', (10, 30), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

#         _, buffer = cv2.imencode(".jpg", cv2.resize(frame, (0, 0), fx=0.5, fy=0.5))
#         img_data = buffer.tobytes()

#         yield (b'--frame\r\n'
#                b'Content-Type: image/jpeg\r\n\r\n' + img_data + b'\r\n\r\n')

#     camera.release()

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

# @app.route('/detect_faces')
# def detect_faces():
#     return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

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


from flask import Flask, render_template, Response, request
import cv2
from mtcnn import MTCNN
import numpy as np
import concurrent.futures

app = Flask(__name__)

class FaceDetector:
    def __init__(self):
        self.detector = MTCNN()
        self.face_count = 0

    def detect_faces(self, frame):
        faces = self.detector.detect_faces(frame)
        self.face_count = len(faces)
        return faces

face_detector = FaceDetector()

def generate_frames(camera_index):
    camera = cv2.VideoCapture(camera_index)

    while True:
        success, frame = camera.read()
        if not success:
            break

        # Perform face detection
        faces = face_detector.detect_faces(frame)

        for face in faces:
            x, y, w, h = face['box']
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

        # Display face count on each frame
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(frame, f'Faces: {face_detector.face_count}', (10, 30), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

        _, buffer = cv2.imencode(".jpg", cv2.resize(frame, (0, 0), fx=0.5, fy=0.5))
        img_data = buffer.tobytes()

        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + img_data + b'\r\n\r\n')

    camera.release()

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

@app.route('/detect_faces')
def detect_faces():
    camera_index = int(request.args.get('camera_index', 1))
    return Response(generate_frames(camera_index), mimetype='multipart/x-mixed-replace; boundary=frame')

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