Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow start/pause of streaming image input. Only access the webcam while it's needed #7228

Merged
merged 9 commits into from Feb 2, 2024

Conversation

freddyaboulton
Copy link
Collaborator

@freddyaboulton freddyaboulton commented Jan 31, 2024

Description

Closes: #3642
Closes: #7230

If the webcam is an allowed image source, gradio will access the webcam as soon as the component is mounted on the page. There is also no way to stop gradio from accessing the webcam. This is #3642

When the webcam is an allowed source, users have to click into the image component to grant webcam access. This is what it looks like in that case:

image

After the webcam stops recording or taking a picture, gradio will stop using the webcam and revert back to the ^previous state.

Additionally, when gr.Image(streaming=true), the webcam component will behave similarly to video recording. There will be a record and stop button at the bottom that users can use to stop the stream of images.

Webcam Demo no streaming

webcam_demo

Webcam Demo with streaming

stream_frames

Test streaming with this demo

import gradio as gr
import numpy as np

def flip(im):
    return np.flipud(im)

with gr.Blocks() as demo:
    with gr.Row():
        input_img = gr.Image(sources=["webcam"])
        output_img = gr.Image()
        input_img.stream(flip, input_img, output_img)
demo.launch(share=True)

if __name__ == "__main__":
    demo.launch()
    

EDIT:

Also fixes Interface streaming blocking the output, fixes: #7021

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 31, 2024

🪼 branch checks and previews

Name Status URL
Spaces building...
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests building...
🦄 Changes detected! Details

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jan 31, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/image minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Allow start/pause of streaming image input. Only access the webcam while it's needed

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@@ -180,19 +203,34 @@
event.stopPropagation();
options_open = false;
}

$: console.log("webcam_accessed", webcam_accessed);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will delete 🤦

<WebcamIcon />
</span>
<button on:click={async () => await access_webcam()}
>{"Click to access webcam"}</button
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be camel case for consistency

@aliabid94
Copy link
Collaborator

I could've sworn I made the change to set the progress tracker visibility to False when streaming from a webcam

@abidlabs
Copy link
Member

I could've sworn I made the change to set the progress tracker visibility to False when streaming from a webcam

I'm pretty sure its a regression. We should do this for any streaming event, e.g. #7230 as well

@abidlabs
Copy link
Member

This looks great to me @freddyaboulton! My only feedback is that the clickable area to give access to the webcam is very small. I kept clicking the icon and was confused why it wasn't working. I'd suggest making this whole area clickable:

image

@freddyaboulton
Copy link
Collaborator Author

@aliabid94 Yea I remember too. I think it was this PR - #6709

Maybe we accidentally merged a regression.

@aliabid94
Copy link
Collaborator

Is the demo supposed to be using an audio component? Btw when I was running locally I did not see the progress bar but also I got this error after a while when streaming a webcam:
ValueError: Cannot process this value as an Image, it is of type: <class 'tuple'>

@aliabid94
Copy link
Collaborator

Btw when I was running locally I did not see the progress bar

Actually there was a visible progress bar only when using streaming from Interface, fixed in this PR in this commit: db019e4

@freddyaboulton freddyaboulton force-pushed the record-button-for-image-streaming branch from db019e4 to 1395b21 Compare January 31, 2024 22:12
@freddyaboulton
Copy link
Collaborator Author

Actually there was a visible progress bar only when using streaming from Interface, fixed in this PR in this commit: db019e4

Nice - confirmed it works! 🔥

Is the demo supposed to be using an audio component?

Yea that's my bad - copy/pasted the wrong demo. I updated the PR description to include the correct one but also including it here

import gradio as gr
import numpy as np

def flip(im):
    return np.flipud(im)

with gr.Blocks() as demo:
    with gr.Row():
        input_img = gr.Image(sources=["webcam"])
        output_img = gr.Image()
        input_img.stream(flip, input_img, output_img)
demo.launch(share=True)

if __name__ == "__main__":
    demo.launch()

My only feedback is that the clickable area to give access to the webcam is very small.

Agreed - just pushed up a fix! Will wait for CI to pass before opening it up for broader review.

Comment on lines 151 to 152
const func = mode === "image" ? take_picture : take_recording;
func();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid abbreviations like func. Personally think this is more readable tbh

Suggested change
const func = mode === "image" ? take_picture : take_recording;
func();
if (mode === "image") {
take_picture();
} else {
take_recording();
}

@@ -253,6 +282,10 @@
height: var(--size-full);
}

.invisible {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, but we use the class name hide throughout the codebase for display: none;, for consistency would be nice

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM great work

@dawoodkhan82
Copy link
Collaborator

Looks great @freddyaboulton. Small nit, I notice after taking a picture the input component quickly flashes back to the "click to access webcam" state before showing the taken picture.

@abidlabs
Copy link
Member

abidlabs commented Feb 2, 2024

Maybe also fixes: #7230?

@freddyaboulton freddyaboulton force-pushed the record-button-for-image-streaming branch from b26b2c5 to 9255ecd Compare February 2, 2024 16:22
@freddyaboulton
Copy link
Collaborator Author

Thanks for the reviews everyone! Will merge the comments and flicker have been addressed.

@freddyaboulton freddyaboulton enabled auto-merge (squash) February 2, 2024 17:59
@freddyaboulton freddyaboulton merged commit 2e6672c into main Feb 2, 2024
8 checks passed
@freddyaboulton freddyaboulton deleted the record-button-for-image-streaming branch February 2, 2024 18:26
@pngwn pngwn mentioned this pull request Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants