Hello , I am new to dash, I am currently stuck at a point. I need to stream the video on dashboard and then pause the video with the help of stop button and then wants to save that paused frame of video in my local directory. I am able to stream the video but I am unable to put a stop button in ordert to pause the video and simultaneously save this frame to my local directory. Below is my code. It will be a great help if any help is provided.
import dash
from dash.dependencies import Output, Input, State
import dash_core_components as dcc
import dash_html_components as html
import cv2
import numpy as np
from dash.exceptions import PreventUpdate
import plotly.express as px
from PIL import Image
app = dash.Dash()
server = app.server
app.layout = html.Div(children = [
html.Div([
html.Div([
dcc.Upload(id='upload_file_1', children=[html.Button('Upload File 1', id='file_upload_btn_1', n_clicks=0, style={'width': '240px', 'height':'40px'})]
)], style={'display': 'inline-block'}),
html.Div(id='file_upload_success_1', style={'display': 'inline-block'}),
]),
html.Button('Submit', id='submit_btn', n_clicks=0,
style={'width': '300px', 'height':'60px'}),
html.Button('Save', id='submit', n_clicks=0,
style={'width': '300px', 'height':'60px'}),
html.Video(
controls=True,
id='video_player',
src={},
autoPlay=False,
style={'width': "70%"}
),
html.Div(id='display_output')
])
#@app.callback(Output(‘file_upload_success_1’, ‘children’),
#Input(‘file_upload_btn_1’, ‘n_clicks’),
#Input(‘upload_file_1’, ‘contents’),
#State(‘upload_file_1’, ‘filename’))
#def update_success_message(n_clicks, contents, filename):
#if n_clicks and contents:
#return “File ‘{}’ uploaded”.format(filename)
@app.callback(Output(‘video_player’, ‘src’),
Input(‘submit_btn’, ‘n_clicks’),
#Input(‘upload_file_1’, ‘contents’),
)
def update_output(n_clicks):
if n_clicks:
#if video_1 :
video_src=‘http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4’
else:
dash.no_update
return video_src
@app.callback(Output(‘display_output’, ‘children’),
Input(‘submit’, ‘n_clicks’),
#Input(‘upload_file_1’, ‘contents’),
)
def output(n_clicks):
video_src=‘http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4’
if n_clicks:
cap = cv2.VideoCapture(video_src)
i = 0 # frame index to save frames
while cap.isOpened():
ret, frame = cap.read()
if ret:
cv2.imwrite(r’C:\Users\FQTQ3VZ\DL_Visualization_Dsahboard\Local Version\testimage\ref’+str(i)+’.jpg’, frame)
else:
break
try:
img = np.array(Image.open(r'testimage\ref.jpg'))
except OSError:
raise PreventUpdate
fig = px.imshow(img, color_continuous_scale="gray")
fig.update_layout(coloraxis_showscale=False)
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(showticklabels=False)
return dcc.Graph(figure=fig)
if name == ‘main’:
app.run_server()