Flask 应用程序在本地服务器上工作,但不在全球范围内工作

Flask app working on local server but not globally

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

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

拜托,我几乎尝试了一切。请帮我解决一下。

您是否看过有关相机接口的 Heroku 文档?有一个 add-on CameraTag

Heroku Cameratag

您可能还会发现 self-hosting 您在 apache2 服务器上的烧瓶应用程序是更成功的解决方案。教程很多,过程也不难

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

这个 link 这是 self-hosting flask 应用程序的一个很好的教程。

Deploy Flask to Apache Server

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

 # Using OpenCV to capture from device 0.
        

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

你试过修改src吗?

这对您的申请也可能有用。

Stream webcam to html OpenCV