如何使用OpenCV,面部识别,Flask,Python 3和Heroku在客户端打开相机

时间:2018-10-05 08:14:40

标签: python opencv flask

我正在尝试使用Heroku上托管的OpenCV,Flask,面部识别制作Web应用程序。在localhost上,一切都按预期运行,但是在Heroku上,我无法打开客户端摄像头。我知道我需要javascript或WebSocket来实现这一目标。 我可以使用javascript找到的所有内容是如何从浏览器上的相机中流式传输,这对我来说并不好,因为我需要打开OpenCV框架进行面部检测和识别。另外,我一直在寻找WebSocket和flask-socket,但它们只发送消息,而不发送视频。

这是我的代码: app.py

'''Face Detection Login App '''

import random, string
import face_recognition
import cv2
import glob
from flask import Flask, render_template, redirect, url_for
app = Flask(__name__)

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


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


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


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

@app.route('/login', methods=["GET", "POST"])
def login():

    page_name = 'reject'

    video_capture = cv2.VideoCapture(0)
# Load faces
    faces = 'faces/*.jpg*'
    face = glob.glob(faces)
    for fn in face:
        try_image = face_recognition.load_image_file(f'{fn}')
        print(f'{fn}')
        try_face_encoding = face_recognition.face_encodings(try_image)

    if not try_face_encoding:
        print("No face found on the image")
        return redirect(url_for(page_name))

    try_face_encoding = try_face_encoding[0]

# Array of faces
    known_face_encodings = [
        try_face_encoding,
    ]

    face_locations = []
    face_encodings = []
    process_this_frame = True

    ret, frame = video_capture.read()

# Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(
        rgb_small_frame, face_locations)

        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(
                known_face_encodings, face_encoding)

            if True in matches:
                first_match_index = matches.index(True)
                page_name = 'article'
                break

# if user is NOT found release the capture and redirect
    video_capture.release()
    cv2.destroyAllWindows()

    return redirect(url_for(page_name))


# Register
@app.route('/register', methods=["GET", "POST"])
def register():
    video_capture = cv2.VideoCapture(0)
    faceCascade = 
    cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

while(True):
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        if w <= 200:
            x = 0
            y = 20
            text_color = (0, 255, 0)
            cv2.putText(
                frame, "Please get closer", (x, y),
                cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1
            )
        else:
            x = 0
            y = 20
            text_color = (0, 255, 0)
            cv2.putText(
                frame, "Press q to take image", (x, y),
                cv2.FONT_HERSHEY_PLAIN, 1.0, text_color, thickness=1
            )

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        image_name = str(random.randint(1, 100))
        cv2.imwrite(f'faces/{image_name}.jpg', frame)
        # cv2.imwrite('faces/try.jpg', frame)
        break

video_capture.release()
cv2.destroyAllWindows()

return redirect(url_for('registered'))


if __name__ == '__main__':
    app.secret_key = 'secret123'
    app.config['SESSION_TYPE'] = 'filesystem'
    app.run()

,我的HTML很简单。用两个按钮说要注册或登录的页面,这两个按钮调用函数登录和注册

任何帮助将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:2)

这无法按照您的方式进行构建。
OpenCV将尝试在与服务器当前正在运行的同一台计算机上打开摄像机。在本地主机上,这是您自己的笔记本电脑。在heroku上,那是一台位于AWS数据中心中的服务器(并且没有网络摄像头)。

您需要用javascript / html捕获视频,将数据流传输到服务器,然后在服务器端进行分析。
有关如何在浏览器中访问网络摄像头的信息,请参见本教程:https://www.kirupa.com/html5/accessing_your_webcam_in_html5.htm