【问题标题】:How to open camera on client side with OpenCV, Face Recognition, Flask, Python 3 and Heroku [duplicate]如何使用 OpenCV、人脸识别、Flask、Python 3 和 Heroku 在客户端打开相机 [重复]
【发布时间】:2018-10-05 08:14:40
【问题描述】:

我正在尝试使用托管在 Heroku 上的 OpenCV、Flask、人脸识别来制作网络应用程序。在 localhost 上,一切正常,但在 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 很简单。使用两个调用函数 login 和 register 的按钮注册或登录的页面

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

【问题讨论】:

  • 您可以查看上面的 github 链接,因为我使用该链接为自己创建了一个示例 poc。 [使用 Facenet 及其预训练模型在 python 中进行人脸匹配]:github.com/arunmandal53/facematch
  • 谢谢,但我不明白这如何帮助我使用 OpenCV 和 Heroku 访问客户端摄像头。这将在 localhost 上运行。

标签: python opencv flask


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的回答。注册用户没问题,因为我可以打开相机并保存图像。但是当用户想要登录时,我需要打开相机并运行 python 函数进行人脸识别,以比较来自网络摄像头的图像和保存的图像。玩具是否认为我需要重写所有内容才能做到这一点?
  • 是的。您无法访问相机服务器端。
  • 谢谢。好吧,我想我除了重新开始之外别无他法。再次感谢您
猜你喜欢
  • 2021-09-18
  • 2019-08-10
  • 2017-02-26
  • 2017-02-03
  • 1970-01-01
  • 2017-07-08
  • 2018-06-14
  • 1970-01-01
  • 2019-05-10
相关资源
最近更新 更多