我正在使用flask和python开发基于人脸识别的考勤系统。我的要求是,我需要一个在网页上显示的脸周围带有边框的实时网络摄像头,通过点击按钮,它应该拍摄照片并将其保存到指定目录。我尝试使用mtcnn,但出现以下错误:
app.py
from flask import Flask, render_template, Response
from mtcnn.mtcnn import MTCNN
import cv2
import os
import numpy as np
from datetime import datetime
from src.preprocess import face_preprocess
app = Flask(__name__)
detector = MTCNN()
def generate_frame():
# initialize video stream
cap = cv2.VideoCapture(0)
# Setup some useful var
faces = 0
frames = 0
max_faces = 50
max_bbox = np.zeros(4)
os.makedirs(os.path.join('images', 'person_name'), exist_ok=True)
file = os.path.join('images', 'person_name')
while faces < max_faces:
ret, frame = cap.read()
frames += 1
dtString = str(datetime.now().microsecond)
# Get all faces on current frame
bboxes = detector.detect_faces(frame)
if len(bboxes) != 0:
# Get only the biggest face
max_area = 0
for bboxe in bboxes:
bbox = bboxe["box"]
bbox = np.array([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])
keypoints = bboxe["keypoints"]
area = (bbox[2] - bbox[0]) * (bbox[3] - bbox[1])
if area > max_area:
max_bbox = bbox
landmarks = keypoints
max_area = area
max_bbox = max_bbox[0:4]
# get each of 3 frames
if frames % 3 == 0:
# convert to face_preprocess.preprocess input
landmarks = np.array([landmarks["left_eye"][0], landmarks["right_eye"][0], landmarks["nose"][0],
landmarks["mouth_left"][0], landmarks["mouth_right"][0],
landmarks["left_eye"][1], landmarks["right_eye"][1], landmarks["nose"][1],
landmarks["mouth_left"][1], landmarks["mouth_right"][1]])
landmarks = landmarks.reshape((2, 5)).T
nimg = face_preprocess.preprocess(frame, max_bbox, landmarks, image_size='112,112')
cv2.imwrite(os.path.join(file, "{}.jpg".format(dtString)), nimg)
cv2.rectangle(frame, (max_bbox[0], max_bbox[1]), (max_bbox[2], max_bbox[3]), (255, 0, 0), 2)
print("[INFO] {} Image Captured".format(faces + 1))
faces += 1
rete, buff = cv2.imencode('.jpg', frame)
fr = buff.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + fr + b'\r\n\r\n')
@app.route('/')
def index():
return render_template('test.html')
@app.route('/video')
def video():
return Response(generate_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>STREAMING</h1>
<img src=" {{ url_for('video') }} " width="40%" height="450px"/>
</body>
</html>
暂无答案!
目前还没有任何答案,快来回答吧!