python - 如何使用 OpenCV、人脸识别、Flask、Python 3 和 Heroku 在客户端打开摄像头

标签 python opencv flask

<分区>

我正在尝试使用 Heroku 上托管的 OpenCV、Flask 和人脸识别来制作网络应用程序。在本地主机上,一切正常,但在 Heroku 上,我无法打开客户端摄像头。我知道我需要 javascript 或 WebSocket 来实现这一点。 我只能用 javascript 找到如何从浏览器上的摄像头流式传输,这对我不利,因为我需要打开 OpenCV 框架进行人脸检测和识别。另外,我一直在寻找 WebSocket 和 flask-socket,但它们只发送消息,不发送视频。

这是我的代码: 应用.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 很简单。页面显示要使用两个调用功能登录和注册的按钮进行注册或登录

如有任何帮助,我们将不胜感激。 谢谢

最佳答案

这无法按照您构建它的方式工作。
OpenCV 将尝试在服务器当前运行的同一台机器上打开摄像头。在本地主机上,那是你自己的笔记本电脑。在 heroku 上,这是 AWS 数据中心某处的服务器(它没有网络摄像头)。

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

关于python - 如何使用 OpenCV、人脸识别、Flask、Python 3 和 Heroku 在客户端打开摄像头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661209/

相关文章:

python - 优化 Python 代码以提高效率

python - 我不明白这个 Django 文档。我该如何使用这个模块?

python - 无法找到 matplotlib 配置文件 .mplstyle 的正确文档

c++ - 无法在 Windows 7 机器上的 Microsoft Visual C++ 2010 中运行 OpenCV

python-3.x - 出现错误 : 'No module named flask' in VSCode even when I have installed flask

python - 使用惯用的 Python 删除 pandas 列中的空格和换行符?

opencv - 我们如何从图像中删除一种纯色?

python - 在 Flask 中引用父目录上方的静态文件

python - 设置 Flask 站点时出现问题

java - pythonic OpenCV中java图像的使用