首页 > 解决方案 > 如何使用烧瓶流式传输桌面

问题描述

我需要使用烧瓶流式传输我的桌面,但我很困惑。我在这里看到 - https://blog.miguelgrinberg.com/post/video-streaming-with-flask我可以“愚弄”烧瓶以为我有一个网络摄像头并流式传输其他图像。我尝试使用 gen(camera) 方法获取桌面图像并对 Camera 类进行了一些更改,但我仍然得到一个空白屏幕并且无法在线找到解决方案。我的代码:

import mss
import numpy

class Camera(object):
    def __init__(self):
        with mss.mss() as sct:
            # Part of the screen to capture
            monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
        self.frames = sct.grab(monitor)

    def get_frame(self):
        return self.frames
from flask import Response
from flask import Flask
from flask import render_template
import threading
import numpy
import time
import cv2
import mss
from camera import Camera


app = Flask(__name__)

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

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')

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

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

标签: pythonflask

解决方案


我认为问题出在您没有将原始数据编码为 JPEG 的事实。

以下实现基于此处代码的摘录。我已经稍微缩短了代码。请查看原件以找出缺失的部分。您应该能够将它组合成一个工作完成的变体。

import cv2
import mss
import numpy

import threading
import time

class Camera(object):
    thread = None
    frame = None
    last_access = 0

    def __init__(self):
        if Camera.thread is None:
            Camera.last_access = time.time()
            Camera.thread = threading.Thread(target=self._thread)
            Camera.thread.start()

            while self.get_frame() is None:
                time.sleep(0)

    def get_frame(self):
        '''Get the current frame.'''
        Camera.last_access = time.time()

        return Camera.frame

    @staticmethod
    def frames():
        '''Create a new frame every 2 seconds.'''
        monitor = {
            'top': 40,
            'left': 0,
            'width': 800,
            'height': 640
        }
        with mss.mss() as sct:
            while True:
                time.sleep(2)
                raw = sct.grab(monitor)
                # Use numpy and opencv to convert the data to JPEG. 
                img = cv2.imencode('.jpg', numpy.array(raw))[1].tobytes()
                yield(img)

    @classmethod
    def _thread(cls):
        '''As long as there is a connection and the thread is running, reassign the current frame.'''
        print('Starting camera thread.')
        frames_iter = cls.frames()
        for frame in frames_iter:
            Camera.frame = frame
            if time.time() - cls.last_access > 10:
                frames_iter.close()
                print('Stopping camera thread due to inactivity.')
                break
        cls.thread = None

在您的示例中,初始化相机的相应实例时仅创建一个图像。
在上述版本中,线程内每 2 秒连续拍摄一张新照片。然后将其设置为当前帧并进行流式传输。如果没有与客户端的连接,则终止线程。内存管理也进行了重大修改。


推荐阅读