使用 mtcnn 进行人脸识别的网络摄像头

时间:2021-07-08 17:46:39

标签: python flask computer-vision face-recognition

我正在使用flask和python开发基于面部识别的考勤系统。我的要求是,我需要一个在网页上显示的面部周围带有边界框的实时网络摄像头,通过单击按钮,它应该拍摄图片并将其保存到我尝试使用 MTCNN 但出现以下错误的指定目录中:

enter image description here enter image description here

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>

0 个答案:

没有答案