Flask 应用程序在本地服务器上工作,但不在全球范围内工作 [英] Flask app working on local server but not globally

查看:79
本文介绍了Flask 应用程序在本地服务器上工作,但不在全球范围内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

These are my routes and some functions:

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/.

In the code below i have used WebcamVideoStream(src = 0).start(). In this src = 0 works fine on local server but when i deployed it on heroku it not opening the webcam( i.e. it is not detecting the webcam on heroku server). Check here(the webpage): 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

请我几乎尝试了所有方法.请帮我解决它.

Please i tried almost everything. Please help me out to solve it.

推荐答案

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

Have you had a look at the Heroku docs regarding camera interfaces? There is an add-on CameraTag

Heroku Cameratag

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

Also you might find self-hosting your flask app on an apache2 server a more successful solution. There are many tutorials and the process is not difficult.

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

Heroku is great, but with my experience with face_recognition specifically, if you don't pay for upgrades you will run into issues exceeding memory <550mb.

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

This link here is a great tutorial for self-hosting flask app.

将 Flask 部署到 Apache 服务器

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

Furthermore, it may be that Heroku cannot access the local camera peripheral or the device is no longer [0]

 # Using OpenCV to capture from device 0.
        

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

您是否尝试过修改 src?

Have you tried modifying the src?

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

This may also be useful for your application.

流网络摄像头到 html OpenCV

这篇关于Flask 应用程序在本地服务器上工作,但不在全球范围内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆