Hello,
I am trying to set up a flow with a download information or flag at the end. I split the app into three tabs which are process images, videos and realtime (with streamlit-webrtc).
Pipeline looks like this;
- Upload video with
st.file_uploader
- Click
st.button
to start process - In the received video, he guesses the genders and makes the visualizations.
- The information in the frame is saved in a dictionary so that it can be downloaded.
- When the process is finished, the video is shown with
st.video
6. I should have two options here. One is to download the outputs. The other should be a button that you flag predicted wrong gender.
As a result, I want to take these two for store and review. But when I click the download button, the page refreshes. I have shared the code below.
def inferenceVideo(PROCESSOR):
inputVideoFile = st.file_uploader("Choose a video")
if (inputVideoFile is not None) and (st.button("Process")):
st.text("Video processing...")
bytesVideo = inputVideoFile.read()
frames = iio.v3.imread(bytesVideo, format_hint=".mp4")
frameWidth = frames.shape[2]
frameHeight = frames.shape[1]
videoName = os.path.join("Requests", "%s.webm" % datetime.now())
out = cv2.VideoWriter(
videoName,
cv2.VideoWriter_fourcc(*'vp80'),
30,
(frameWidth, frameHeight),
)
progress = st.progress(0)
outputInformations = {}
for c, frame in tqdm(enumerate(frames[:30])):
if c == int(len(frames) - 1):
progress.progress(101)
else:
progress.progress(int(c+1/len(frames[:30])*100))
outputImage, outputInformation = PROCESSOR.inference(frame)
outputInformations["Frame%s" % c] = outputInformation
out.write(cv2.cvtColor(outputImage, cv2.COLOR_RGB2BGR).astype(np.uint8))
outputVideo = io.open(videoName, "rb")
with open(videoName.replace("webm", "json"), 'w') as f:
json.dump(outputInformations, f, indent=4)
zipName = videoName.replace("webm", "zip")
zipObj = ZipFile(zipName, "w")
zipObj.write(videoName)
zipObj.write(videoName.replace("webm", "json"))
zipObj.close()
st.video(outputVideo)
st.json(outputInformations, expanded=False)
os.remove(videoName)
os.remove(videoName.replace("webm", "json"))
#TODO Download or Flag button to feedback.
with open(zipName, "rb") as f:
st.download_button(label="Download outputs",
data=f,
file_name=zipName,
mime="application/zip")