-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
46 lines (35 loc) · 1.27 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
#!/usr/bin/env python
import cv2
from flask import Flask, render_template, Response
import base64
vc = cv2.VideoCapture(0)
# vc.isOpened()
face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')
detected_faces = [] # List to store detected faces
def detect_faces(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
return faces
def gen_frames():
global detected_faces
while True:
ret, frame = vc.read()
if not ret:
break
# Detect faces
# faces = detect_faces(frame)
detected_faces.append(frame) # All frames without detecting faces
# Encode frame as JPEG
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', detected_faces=detected_faces)
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)