python - 如何在 Dash (Plotly) 中使用本地网络摄像头在登录页面中显示直播?
问题描述
我想使用本地网络摄像头在 html 页面上实时显示 cam 输出。如果可用,网络浏览器最好使用登陆用户的默认网络摄像头。这是否有可能在 Python 中实现?我考虑过使用 Dash 并尝试了以下代码段:
import dash
import dash_core_components as dcc
import dash_html_components as html
from flask import Flask, Response
import cv2
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
cv2.destroyAllWindows()
def get_frame(self):
success, image = self.video.read()
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
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')
server = Flask(__name__)
app = dash.Dash(__name__, server=server)
@server.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
app.layout = html.Div([
html.H1("Webcam Test"),
html.Img(src="/video_feed")
])
if __name__ == '__main__':
app.run_server(debug=True)
不幸的是,当服务器运行时,它没有在页面上显示所需的本地网络摄像头输出。我该如何解决这个问题?
解决方案
推荐阅读
- javascript - Firestore 和 Vue.js 多个 Where 子句
- javascript - 是什么导致我的 jQuery 调用出现这种递归?
- python - 如何使这个循环每次迭代增加 1.8?
- raspberry-pi - 节点红树莓派多输入
- javascript - Chrome 中的嵌套 Web Worker
- ruby-on-rails - 为什么没有从视图中调用我的控制器
- python - Python根据条件创建ID组合
- html - 使用 FontAwesome 图标居中文本
- firebase - 如何在 firebase localhost:5000 中切换项目
- javascript - 结合两个 JSON 对象 javaScript