尝试使用cheroot flask和opencv向多个客户端广播视频

时间:2019-06-22 21:27:41

标签: python opencv flask raspberry-pi3 cherrypy

我正在尝试创建从Raspberry Pi到多个客户端的视频流。 Flask不支持WSGI服务器,因此我使用cheroot.wsgi服务器。我已经使用noip创建了ddns服务器,以便通过互联网广播视频流。直到现在,即使我使用wsgi服务器,我也只能将视频提供给一个客户端。

这是视频供稿器

from flask import Flask, render_template, Response
from camera import VideoCamera
import RPi.GPIO as gpio


gpio.setmode(gpio.BCM)  
gpio.setup(21, gpio.OUT)


app = Flask(__name__)

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


@app.route('/')
def index():
    return render_template('index.html')



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

@app.route('/background_process_test')
def background_process_test():
    gpio.output(21, gpio.HIGH)

    print ("Hello")
    return ("nothing")

@app.route('/background_process_test2')
def background_process_test2():
    gpio.output(21, gpio.LOW)

    print ("Hello")
    return ("nothing")

if __name__ == '__main__':
    app.run()

这是使用cheroot的wsgi服务器

尝试:     从cheroot.wsgi将服务器导入为WSGIServer,PathInfoDispatcher 除了ImportError:     打印(“确定”)     从cherrypy.wsgiserver导入CherryPyWSGIServer作为WSGIServer,将WSGIPathInfoDispatcher导入到PathInfoDispatcher

from main import app
d = PathInfoDispatcher({'/': app})
server = WSGIServer(('0.0.0.0', 5000), d)
if __name__ == '__main__':
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()

捕获摄像机帧的opencv模块

import cv2

class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture(0)
        self.video.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
        self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, 200)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tobytes()

最后是提供视频供稿的网页

<!doctype html>

<html>
<head>
<!--    <link rel="shortcut icon" href="1.ico" type="image/x-icon" />-->

    <title>jindeath</title>



</head>
<body>



hello
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
        $(function() {
          $('a#test').bind('click', function() {
            $.getJSON('/background_process_test',
                function(data) {
              //do nothing
            });
            return false;
          });
        });
                $(function() {
          $('a#test2').bind('click', function() {
            $.getJSON('/background_process_test2',
                function(data) {
              //do nothing
            });
            return false;
          });
        });
</script>

<div class='container'>
    <h3>Test</h3>
        <form>
            <img id="bg" src="{{ url_for('video_feed') }}">
            <a href=# id=test><button class='btn btn-default'>Test</button></a>
            <a href=# id=test2><button class='btn btn-default'>Test</button></a>

        </form>
</body>
</html>

当不止一个设备连接到页面时,我的rpi会使用其CPU的100%。任何建议

0 个答案:

没有答案