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

add webcam_height and webcam_width to specify the resolution of the Webcam #10032

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gradio/components/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def __init__(
show_share_button: bool | None = None,
placeholder: str | None = None,
show_fullscreen_button: bool = True,
webcam_height: int = 1440,
Copy link
Collaborator

@freddyaboulton freddyaboulton Nov 25, 2024

Choose a reason for hiding this comment

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

Thanks @yinsumirage for the nice contribution!

I prefer we instead just let developers specify the media constraints as a python dictionary and we pass that into the get_stream function. It will be more flexible that way. We can add docstring examples for controlling the height and width specifically. I think we can call this parameter webcam_constraints.

BTW, you also need to specify the new props in image/Index.svelte and image/ImageUploader.svelte and pass them into the Webcam component.

BTW can you do this for video as well now that you're at it? 😊

webcam_width: int = 1920,
):
"""
Parameters:
Expand Down Expand Up @@ -170,6 +172,9 @@ def __init__(
)
self.show_fullscreen_button = show_fullscreen_button
self.placeholder = placeholder
self.webcam_height = webcam_height
self.webcam_width = webcam_width

super().__init__(
label=label,
every=every,
Expand Down
7 changes: 5 additions & 2 deletions js/image/shared/Webcam.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
set_available_devices
} from "./stream_utils";
import type { Base64File } from "./types";
import type { int } from "babylonjs";

let video_source: HTMLVideoElement;
let available_video_devices: MediaDeviceInfo[] = [];
Expand Down Expand Up @@ -52,6 +53,8 @@
export let mode: "image" | "video" = "image";
export let mirror_webcam: boolean;
export let include_audio: boolean;
export let webcam_height:int;
export let webcam_width: int;
export let i18n: I18nFormatter;
export let upload: Client["upload"];
export let value: FileData | null | Base64File = null;
Expand Down Expand Up @@ -80,7 +83,7 @@
const target = event.target as HTMLInputElement;
const device_id = target.value;

await get_video_stream(include_audio, video_source, device_id).then(
await get_video_stream(include_audio, video_source, webcam_height, webcam_width, device_id).then(
async (local_stream) => {
stream = local_stream;
selected_device =
Expand All @@ -94,7 +97,7 @@

async function access_webcam(): Promise<void> {
try {
get_video_stream(include_audio, video_source)
get_video_stream(include_audio, video_source, webcam_height, webcam_width)
.then(async (local_stream) => {
webcam_accessed = true;
available_video_devices = await get_devices();
Expand Down
10 changes: 6 additions & 4 deletions js/image/shared/stream_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export function set_local_stream(
export async function get_video_stream(
include_audio: boolean,
video_source: HTMLVideoElement,
device_id?: string
width?: number,
height?: number,
device_id?: string,
): Promise<MediaStream> {
const size = {
width: { ideal: 1920 },
height: { ideal: 1440 }
};
width: { ideal: width || 1920 },
height: { ideal: height || 1440 }
};

const constraints = {
video: device_id ? { deviceId: { exact: device_id }, ...size } : size,
Expand Down