-1

这些是我的路线和一些功能:

from flask import Flask, render_template, Response
import cv2
import time
from sys import stdout
from flask_socketio import SocketIO
import math
import numpy as np
import logging
import os
from camera import VideoCamera

app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(stdout))

app.config['SECRET_KEY'] = 'b13ce0c6768bb0b280bab13ceb13ce0cde280ba0c676dfde280ba245676dfde280ba0c676dfde280ba245'
app.config['DEBUG'] = True
socketio = SocketIO(app)

@socketio.on('connect', namespace='/test')
def test_connect():
    app.logger.info("client connected")


@app.route('/', methods = ['GET','POST'])
def index():
    """Video streaming home page."""
    return render_template('index.html')

def gen(camera):
    while True:
        data= camera.get_frame()

        frame=data[0]
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/golden_ratio_calculating', methods = ['GET','POST'])
def calculate():
    ratio = main()
    return render_template('golden_calc_page.html' , ratio123 = ratio)


if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    socketio.run(app, port = port)

在下面的代码中,我使用了 WebcamVideoStream(src = 0).start()。在这个 src = 0 在本地服务器上工作正常但是当我将它部署在 heroku 上时它没有打开网络摄像头(即它没有检测到 heroku 服务器上的网络摄像头)。在这里查看(网页):https ://golden-ratio-calculator.herokuapp.com/ 。

import cv2
import pickle
from imutils.video import WebcamVideoStream
# import face_recognition
import time
import math
import random
import numpy as np

class VideoCamera(object):
    
    def __init__(self):
        
        # Using OpenCV to capture from device 0.
        

        self.stream = WebcamVideoStream(src = 0).start()
        

    def __del__(self):
        self.stream.stop()

    def get_frame(self):
        image = self.stream.read()
        startTime = time.time()
        top2chin = []
        left2right = []
        top2pupil = []
        pupil2lip = []
        noseWidth = []
        nose2lips = []
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
        eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
        righteye_cascade = cv2.CascadeClassifier('haarcascade_righteye.xml')
        lefteye_cascade = cv2.CascadeClassifier('haarcascade_leftteye.xml')
        smile_cascade = cv2.CascadeClassifier('haarcascade_mouth.xml')
        nose_cascade = cv2.CascadeClassifier('haarcascade_nose.xml')
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        height, width, channels = image.shape
        for(x, y, w, h) in faces:
            # print("found a face")
            cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
            roi_gray = gray[y:y+h, x:x+h]
            roi_color = image[y:y+h, x:x+h]
            eyes = eye_cascade.detectMultiScale(roi_gray, 2.5, 5)
            smiles = smile_cascade.detectMultiScale(roi_gray, 3.4, 5)
            noses = nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
            right_eyes = righteye_cascade.detectMultiScale(roi_gray, 2.5, 5)
            ex, ey, ew, eh = 0,0,0,0
            sx, sy, sw, sh = 0,0,0,0
            nx, ny, nw, nh = 0,0,0,0
            for (ex, ey, ew, eh) in eyes:
                cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 1)
            for (sx, sy, sw, sh) in smiles:
                cv2.rectangle(roi_color, (sx, sy), (sx+sw, sy+sh), (0, 0, 255), 1)
            for (nx, ny, nw, nh) in noses:
                cv2.rectangle(roi_color, (nx, ny), (nx+nw, ny+nh), (255, 0, 255), 1)
            font = cv2.FONT_HERSHEY_SIMPLEX
            
            

            
            cv2.putText(image, "Hello User", (math.floor(width / 4), math.floor(height / 12)), font, 0.7, (255, 255, 255), 1, cv2.LINE_AA)
            
        
        
        
        
        ret, jpeg = cv2.imencode('.jpg', image)
        data = []
        data.append(jpeg.tobytes())
        # data.append(name)
        return data

请我尝试了几乎所有的东西。请帮我解决它。

4

1 回答 1

0

您是否看过 Heroku 文档中有关相机接口的信息?有一个附加的 CameraTag

Heroku 相机标签

此外,您可能会发现在 apache2 服务器上自行托管您的烧瓶应用程序是一种更成功的解决方案。教程很多,过程也不难。

Heroku 很棒,但是根据我在 face_recognition 方面的经验,如果您不支付升级费用,您将遇到内存超过 <550mb 的问题。

这里的这个链接是自托管烧瓶应用程序的一个很好的教程。

将 Flask 部署到 Apache 服务器

此外,可能是 Heroku 无法访问本地摄像头外围设备或设备不再 [0]

 # Using OpenCV to capture from device 0.
        

        self.stream = WebcamVideoStream(src = 0).start()

你试过修改src吗?

这也可能对您的应用程序有用。

将网络摄像头流式传输到 html OpenCV

于 2020-09-06T22:32:13.663 回答