gpt4 book ai didi

python - 如何使用 flask 流式传输桌面

转载 作者:行者123 更新时间:2023-12-05 04:53:55 24 4
gpt4 key购买 nike

我需要使用 Flask 流式传输我的桌面,但我很困惑。我在这里看到 - https://blog.miguelgrinberg.com/post/video-streaming-with-flask我可以“愚弄” 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)

最佳答案

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

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

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 秒连续拍摄一张新照片。然后将其设置为当前帧并进行流式传输。如果没有连接到客户端,则线程终止。内存管理也进行了重大修改。

关于python - 如何使用 flask 流式传输桌面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65919614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com