Hi!
Im relatively new to plotly dash and could use some guidance. I’m trying to make a multi-page app that has one of the pages showing the output from cv2.VideoCapture. I’ve researched and found a couple different posts regarding this issue although it doesnt appear to have been solved for multi-page apps.
Following the guidance of the documentation for multi-page urls and a previous post which works when its a single page app here i’ve created an initial app that looks like this:
app.py
import dash
from dash import Dash, html, dcc
import dash_bootstrap_components as dbc
from pages.pose import video_feed
app = Dash(__name__, use_pages=True, external_stylesheets=[dbc.themes.SPACELAB])
app.layout = html.Div([
html.H1('Multi-page app with Dash Pages'),
html.Div([
html.Div(
dcc.Link(f"{page['name']} - {page['path']}", href=page["relative_path"])
) for page in dash.page_registry.values()
]),
dash.page_container
])
if __name__ == '__main__':
app.run(debug=True)
cam.py
import dash
import dash_core_components as dcc
import dash_html_components as html
from flask import Flask, Response
import cv2
dash.register_page(__name__)
server = Flask(__name__)
app = dash.Dash(__name__, server=server)
layout = html.Div([
html.H1("Webcam Test"),
html.Img(src="/video_feed")
])
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
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.route('/video_feed')
def video_feed():
return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run_server(debug=True)
calling python cam.py
works as expected after changing layout=
to app.layout=
however the calling python app.py
returns a screen with the empty html components and no video capture in the img component. Thanks in advance for your help.