- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 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/
我正在遵循Flask-potion教程并尝试保护我的api。我一直试图找出如何在User端点上设置权限,以使一个User无法查看另一个User的记录。这是我的代码: class UserResourc
项目设置 Python 3.5.3 flask 0.12.2 目录 . ├── Core │ ├── BackgroundProcessManager.py │ ├── FirebaseDat
我正在构建 flask 应用程序并决定这次尝试应用程序工厂方法,但是遇到了 flask 迁移的麻烦并且无法找出简单的解决方案。 请注意,我想将配置位置作为选项传递给脚本 管理.py: manager
我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/ex
我一周前刚刚了解了 Flask 和 JWT,所以我真的需要一些帮助。有人可以解释一下flask-jwt和flask-jwt-extended之间的区别吗? 最佳答案 Flask-JWT 没有那么多功能
我是 Flask 的新手,我只是想用它创建一个简单的市场。我的网站上有三种类型的用户 [卖家、买家、管理员]。我认为每种类型的用户都应该有自己的注册和登录,因为他们的角色应该提供不同类型的信息。另一方
基本上,我想做一些非常简单的事情:我想创建一个用于删除数据库中条目的表单。 模板正在创建一个包含所有条目的html表,没有任何麻烦。我现在的问题是:如何将其转换为每行都有链接的表单。 当然,我可以通过
我对Flask来说非常陌生,其中一个基本要求是我需要SEO友好的URL。 我说一条路线 @app.route('/sales/') @app.route(/sales/) def get_sales(
一直在为用户管理系统开发Flask-user,但是我看不到自定义登录页面和注册页面的方法。我使用pip安装了flask-user pip install flask-user 任何帮助,将不胜感激 最
我有一个奇怪的情况,不知道如何找到问题。 我已经创建了一个这样的蓝图 reportjobsmod = Blueprint('jobreports', __name__, url_prefix='/jo
我认为在 Flask 中拥有实例变量的正确方法是添加用户和 session ,但我正在尝试测试一个概念,我还不想经历所有这些。我试图让一个网络应用程序将图像加载到一个变量中,然后可以对其执行不同的图像
select = SelectField("City") form.select.choices = [(places['name'], places['name']) for places in p
这个问题在这里已经有了答案: Provide extra information to Flask's app.logger (2 个回答) 3年前关闭。 我正在运行一个 flask 应用程序。我想写
我当时正在编程一个非常不错的api,使用json从游戏服务器到我的网站空间获取一些json数据, 但是每次我使用angular发送请求时,我都会得到以下信息:127.0.0.1--[20 / Mar
我已经开发了一个上传表单,以将特定的.xlsx文件作为上传。要求是要处理非xlsx上载的任何异常(例如zip,exe文件)。我正在使用pyexcel库读取上传内容。我尝试创建以下代码来处理此异常: e
我使用此功能在网站上捕获404错误: @app.errorhandler(404) def page_not_found(e): logger.warning('User raised an
我正在尝试同时使用 Flask Restful 和 Flask SocketIO。我已经制作了一个 Flask Restful 应用程序,但现在我想使用 Flask SocketIO 在客户端和我的服
我有一些 Flask 应用程序,我通过设置 FLASK_APP 来运行它们。环境变量和调用 flask run .自 FLASK_DEBUG也设置了,每当我更改一些代码时,应用程序都可以方便地重新加载
我目前正在使用 Flask 构建一个应用程序。我正在努力访问 Flask 实例“app”以及 Flask-mail 实例“mail”。 下面是我的项目的样子: └── my-project ├
Remember-me是 parameter flask_login.login_user(user, remember=False, duration=None, force=False, fres
我是一名优秀的程序员,十分优秀!