Upload folder using huggingface_hub
Browse files- 6.1.0/image/Example.svelte +49 -0
- 6.1.0/image/Index.svelte +220 -0
- 6.1.0/image/package.json +56 -0
- 6.1.0/image/shared/Image.svelte +28 -0
- 6.1.0/image/shared/ImagePreview.svelte +147 -0
- 6.1.0/image/shared/ImageUploader.svelte +305 -0
- 6.1.0/image/shared/Webcam.svelte +486 -0
- 6.1.0/image/shared/WebcamPermissions.svelte +46 -0
- 6.1.0/image/shared/index.ts +2 -0
- 6.1.0/image/shared/stream_utils.ts +50 -0
- 6.1.0/image/shared/types.ts +42 -0
- 6.1.0/image/shared/utils.ts +29 -0
6.1.0/image/Example.svelte
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import Image from "./shared/Image.svelte";
|
| 3 |
+
import type { FileData } from "@gradio/client";
|
| 4 |
+
|
| 5 |
+
export let value: null | FileData;
|
| 6 |
+
export let type: "gallery" | "table";
|
| 7 |
+
export let selected = false;
|
| 8 |
+
</script>
|
| 9 |
+
|
| 10 |
+
<div
|
| 11 |
+
class="container"
|
| 12 |
+
class:table={type === "table"}
|
| 13 |
+
class:gallery={type === "gallery"}
|
| 14 |
+
class:selected
|
| 15 |
+
class:border={value}
|
| 16 |
+
>
|
| 17 |
+
{#if value}
|
| 18 |
+
<Image src={value.url} alt="" />
|
| 19 |
+
{/if}
|
| 20 |
+
</div>
|
| 21 |
+
|
| 22 |
+
<style>
|
| 23 |
+
.container :global(img) {
|
| 24 |
+
width: 100%;
|
| 25 |
+
height: 100%;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
.container.selected {
|
| 29 |
+
border-color: var(--border-color-accent);
|
| 30 |
+
}
|
| 31 |
+
.border.table {
|
| 32 |
+
border: 2px solid var(--border-color-primary);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.container.table {
|
| 36 |
+
margin: 0 auto;
|
| 37 |
+
border-radius: var(--radius-lg);
|
| 38 |
+
overflow: hidden;
|
| 39 |
+
width: var(--size-20);
|
| 40 |
+
height: var(--size-20);
|
| 41 |
+
object-fit: cover;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.container.gallery {
|
| 45 |
+
width: var(--size-20);
|
| 46 |
+
max-width: var(--size-20);
|
| 47 |
+
object-fit: cover;
|
| 48 |
+
}
|
| 49 |
+
</style>
|
6.1.0/image/Index.svelte
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script context="module" lang="ts">
|
| 4 |
+
export { default as Webcam } from "./shared/Webcam.svelte";
|
| 5 |
+
export { default as BaseImageUploader } from "./shared/ImageUploader.svelte";
|
| 6 |
+
export { default as BaseStaticImage } from "./shared/ImagePreview.svelte";
|
| 7 |
+
export { default as BaseExample } from "./Example.svelte";
|
| 8 |
+
export { default as BaseImage } from "./shared/Image.svelte";
|
| 9 |
+
</script>
|
| 10 |
+
|
| 11 |
+
<script lang="ts">
|
| 12 |
+
import { tick } from "svelte";
|
| 13 |
+
import { Gradio } from "@gradio/utils";
|
| 14 |
+
import StaticImage from "./shared/ImagePreview.svelte";
|
| 15 |
+
import ImageUploader from "./shared/ImageUploader.svelte";
|
| 16 |
+
import { Block, Empty, UploadText } from "@gradio/atoms";
|
| 17 |
+
import { Image } from "@gradio/icons";
|
| 18 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 19 |
+
import type { ImageProps, ImageEvents } from "./shared/types";
|
| 20 |
+
|
| 21 |
+
let stream_data = { value: null };
|
| 22 |
+
let upload_promise = $state<Promise<any>>();
|
| 23 |
+
class ImageGradio extends Gradio<ImageEvents, ImageProps> {
|
| 24 |
+
async get_data() {
|
| 25 |
+
if (upload_promise) {
|
| 26 |
+
await upload_promise;
|
| 27 |
+
await tick();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const data = await super.get_data();
|
| 31 |
+
if (props.props.streaming) {
|
| 32 |
+
data.value = stream_data.value;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
return data;
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
const props = $props();
|
| 40 |
+
const gradio = new ImageGradio(props);
|
| 41 |
+
|
| 42 |
+
let fullscreen = $state(false);
|
| 43 |
+
let dragging = $state(false);
|
| 44 |
+
let active_source = $derived.by(() =>
|
| 45 |
+
gradio.props.sources ? gradio.props.sources[0] : null
|
| 46 |
+
);
|
| 47 |
+
|
| 48 |
+
let upload_component: ImageUploader;
|
| 49 |
+
const handle_drag_event = (event: Event): void => {
|
| 50 |
+
const drag_event = event as DragEvent;
|
| 51 |
+
drag_event.preventDefault();
|
| 52 |
+
drag_event.stopPropagation();
|
| 53 |
+
if (drag_event.type === "dragenter" || drag_event.type === "dragover") {
|
| 54 |
+
dragging = true;
|
| 55 |
+
} else if (drag_event.type === "dragleave") {
|
| 56 |
+
dragging = false;
|
| 57 |
+
}
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
const handle_drop = (event: Event): void => {
|
| 61 |
+
if (gradio.shared.interactive) {
|
| 62 |
+
const drop_event = event as DragEvent;
|
| 63 |
+
drop_event.preventDefault();
|
| 64 |
+
drop_event.stopPropagation();
|
| 65 |
+
dragging = false;
|
| 66 |
+
|
| 67 |
+
if (upload_component) {
|
| 68 |
+
upload_component.loadFilesFromDrop(drop_event);
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
let old_value = $state(gradio.props.value);
|
| 74 |
+
|
| 75 |
+
$effect(() => {
|
| 76 |
+
if (old_value != gradio.props.value) {
|
| 77 |
+
old_value = gradio.props.value;
|
| 78 |
+
gradio.dispatch("change");
|
| 79 |
+
}
|
| 80 |
+
});
|
| 81 |
+
|
| 82 |
+
let status = $derived(gradio?.shared?.loading_status.stream_state);
|
| 83 |
+
</script>
|
| 84 |
+
|
| 85 |
+
{#if !gradio.shared.interactive}
|
| 86 |
+
<Block
|
| 87 |
+
visible={gradio.shared.visible}
|
| 88 |
+
variant={"solid"}
|
| 89 |
+
border_mode={dragging ? "focus" : "base"}
|
| 90 |
+
padding={false}
|
| 91 |
+
elem_id={gradio.shared.elem_id}
|
| 92 |
+
elem_classes={gradio.shared.elem_classes}
|
| 93 |
+
height={gradio.props.height || undefined}
|
| 94 |
+
width={gradio.props.width}
|
| 95 |
+
allow_overflow={false}
|
| 96 |
+
container={gradio.shared.container}
|
| 97 |
+
scale{gradio.shared.scale}
|
| 98 |
+
min_width={gradio.shared.min_width}
|
| 99 |
+
bind:fullscreen
|
| 100 |
+
>
|
| 101 |
+
<StatusTracker
|
| 102 |
+
autoscroll={gradio.shared.autoscroll}
|
| 103 |
+
i18n={gradio.i18n}
|
| 104 |
+
{...gradio.shared.loading_status}
|
| 105 |
+
on_clear_status={() =>
|
| 106 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 107 |
+
/>
|
| 108 |
+
<StaticImage
|
| 109 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
| 110 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 111 |
+
on:error={({ detail }) => gradio.dispatch("error", detail)}
|
| 112 |
+
on:fullscreen={({ detail }) => {
|
| 113 |
+
fullscreen = detail;
|
| 114 |
+
}}
|
| 115 |
+
{fullscreen}
|
| 116 |
+
value={gradio.props.value}
|
| 117 |
+
label={gradio.shared.label}
|
| 118 |
+
show_label={gradio.shared.show_label}
|
| 119 |
+
selectable={gradio.props._selectable}
|
| 120 |
+
i18n={gradio.i18n}
|
| 121 |
+
buttons={gradio.props.buttons}
|
| 122 |
+
/>
|
| 123 |
+
</Block>
|
| 124 |
+
{:else}
|
| 125 |
+
<Block
|
| 126 |
+
visible={gradio.shared.visible}
|
| 127 |
+
variant={gradio.props.value === null ? "dashed" : "solid"}
|
| 128 |
+
border_mode={dragging ? "focus" : "base"}
|
| 129 |
+
padding={false}
|
| 130 |
+
elem_id={gradio.shared.elem_id}
|
| 131 |
+
elem_classes={gradio.shared.elem_classes}
|
| 132 |
+
height={gradio.props.height || undefined}
|
| 133 |
+
width={gradio.props.width}
|
| 134 |
+
allow_overflow={false}
|
| 135 |
+
container={gradio.shared.container}
|
| 136 |
+
scale={gradio.shared.scale}
|
| 137 |
+
min_width={gradio.shared.min_width}
|
| 138 |
+
bind:fullscreen
|
| 139 |
+
on:dragenter={handle_drag_event}
|
| 140 |
+
on:dragleave={handle_drag_event}
|
| 141 |
+
on:dragover={handle_drag_event}
|
| 142 |
+
on:drop={handle_drop}
|
| 143 |
+
>
|
| 144 |
+
{#if gradio.shared.loading_status.type === "output"}
|
| 145 |
+
<StatusTracker
|
| 146 |
+
autoscroll={gradio.shared.autoscroll}
|
| 147 |
+
i18n={gradio.i18n}
|
| 148 |
+
{...gradio.shared.loading_status}
|
| 149 |
+
on_clear_status={() =>
|
| 150 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 151 |
+
/>
|
| 152 |
+
{/if}
|
| 153 |
+
<ImageUploader
|
| 154 |
+
bind:upload_promise
|
| 155 |
+
bind:this={upload_component}
|
| 156 |
+
bind:active_source
|
| 157 |
+
bind:value={gradio.props.value}
|
| 158 |
+
bind:dragging
|
| 159 |
+
selectable={gradio.props._selectable}
|
| 160 |
+
root={gradio.shared.root}
|
| 161 |
+
sources={gradio.props.sources}
|
| 162 |
+
{fullscreen}
|
| 163 |
+
show_fullscreen_button={gradio.props.buttons === null
|
| 164 |
+
? true
|
| 165 |
+
: gradio.props.buttons.includes("fullscreen")}
|
| 166 |
+
on:edit={() => gradio.dispatch("edit")}
|
| 167 |
+
on:clear={() => {
|
| 168 |
+
fullscreen = false;
|
| 169 |
+
gradio.dispatch("clear");
|
| 170 |
+
gradio.dispatch("input");
|
| 171 |
+
}}
|
| 172 |
+
on:stream={({ detail }) => {
|
| 173 |
+
stream_data = detail;
|
| 174 |
+
gradio.dispatch("stream", detail);
|
| 175 |
+
}}
|
| 176 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 177 |
+
on:upload={() => {
|
| 178 |
+
gradio.dispatch("upload");
|
| 179 |
+
gradio.dispatch("input");
|
| 180 |
+
}}
|
| 181 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
| 182 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 183 |
+
on:error={({ detail }) => {
|
| 184 |
+
gradio.shared.loading_status.status = "error";
|
| 185 |
+
gradio.dispatch("error", detail);
|
| 186 |
+
}}
|
| 187 |
+
on:close_stream={() => {
|
| 188 |
+
gradio.dispatch("close_stream");
|
| 189 |
+
}}
|
| 190 |
+
on:fullscreen={({ detail }) => {
|
| 191 |
+
fullscreen = detail;
|
| 192 |
+
}}
|
| 193 |
+
label={gradio.shared.label}
|
| 194 |
+
show_label={gradio.shared.show_label}
|
| 195 |
+
pending={gradio.shared.loading_status?.status === "pending" ||
|
| 196 |
+
gradio.shared.loading_status?.status === "streaming"}
|
| 197 |
+
streaming={gradio.props.streaming}
|
| 198 |
+
webcam_options={gradio.props.webcam_options}
|
| 199 |
+
stream_every={gradio.props.stream_every}
|
| 200 |
+
time_limit={gradio.shared.loading_status?.time_limit}
|
| 201 |
+
max_file_size={gradio.shared.max_file_size}
|
| 202 |
+
i18n={gradio.i18n}
|
| 203 |
+
upload={(...args) => gradio.shared.client.upload(...args)}
|
| 204 |
+
stream_handler={gradio.shared.client?.stream}
|
| 205 |
+
stream_state={status}
|
| 206 |
+
>
|
| 207 |
+
{#if active_source === "upload" || !active_source}
|
| 208 |
+
<UploadText
|
| 209 |
+
i18n={gradio.i18n}
|
| 210 |
+
type="image"
|
| 211 |
+
placeholder={gradio.props.placeholder}
|
| 212 |
+
/>
|
| 213 |
+
{:else if active_source === "clipboard"}
|
| 214 |
+
<UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
|
| 215 |
+
{:else}
|
| 216 |
+
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
| 217 |
+
{/if}
|
| 218 |
+
</ImageUploader>
|
| 219 |
+
</Block>
|
| 220 |
+
{/if}
|
6.1.0/image/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/image",
|
| 3 |
+
"version": "0.24.0",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"@gradio/atoms": "workspace:^",
|
| 11 |
+
"@gradio/client": "workspace:^",
|
| 12 |
+
"@gradio/icons": "workspace:^",
|
| 13 |
+
"@gradio/statustracker": "workspace:^",
|
| 14 |
+
"@gradio/upload": "workspace:^",
|
| 15 |
+
"@gradio/utils": "workspace:^",
|
| 16 |
+
"cropperjs": "^2.0.1",
|
| 17 |
+
"lazy-brush": "^2.0.2",
|
| 18 |
+
"resize-observer-polyfill": "^1.5.1"
|
| 19 |
+
},
|
| 20 |
+
"devDependencies": {
|
| 21 |
+
"@gradio/preview": "workspace:^"
|
| 22 |
+
},
|
| 23 |
+
"main_changeset": true,
|
| 24 |
+
"main": "./Index.svelte",
|
| 25 |
+
"exports": {
|
| 26 |
+
"./package.json": "./package.json",
|
| 27 |
+
".": {
|
| 28 |
+
"gradio": "./Index.svelte",
|
| 29 |
+
"svelte": "./dist/Index.svelte",
|
| 30 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 31 |
+
},
|
| 32 |
+
"./example": {
|
| 33 |
+
"gradio": "./Example.svelte",
|
| 34 |
+
"svelte": "./dist/Example.svelte",
|
| 35 |
+
"types": "./dist/Example.svelte.d.ts"
|
| 36 |
+
},
|
| 37 |
+
"./base": {
|
| 38 |
+
"gradio": "./shared/ImagePreview.svelte",
|
| 39 |
+
"svelte": "./dist/shared/ImagePreview.svelte",
|
| 40 |
+
"types": "./dist/shared/ImagePreview.svelte.d.ts"
|
| 41 |
+
},
|
| 42 |
+
"./shared": {
|
| 43 |
+
"gradio": "./shared/index.ts",
|
| 44 |
+
"svelte": "./dist/shared/index.js",
|
| 45 |
+
"types": "./dist/shared/index.d.ts"
|
| 46 |
+
}
|
| 47 |
+
},
|
| 48 |
+
"peerDependencies": {
|
| 49 |
+
"svelte": "^5.43.4"
|
| 50 |
+
},
|
| 51 |
+
"repository": {
|
| 52 |
+
"type": "git",
|
| 53 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 54 |
+
"directory": "js/image"
|
| 55 |
+
}
|
| 56 |
+
}
|
6.1.0/image/shared/Image.svelte
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
let {
|
| 3 |
+
src,
|
| 4 |
+
restProps,
|
| 5 |
+
data_testid,
|
| 6 |
+
class_names
|
| 7 |
+
}: {
|
| 8 |
+
src: string;
|
| 9 |
+
restProps: object;
|
| 10 |
+
data_testid: string;
|
| 11 |
+
class_names: string[];
|
| 12 |
+
} = $props();
|
| 13 |
+
</script>
|
| 14 |
+
|
| 15 |
+
<!-- svelte-ignore a11y-missing-attribute -->
|
| 16 |
+
<img
|
| 17 |
+
{src}
|
| 18 |
+
class={(class_names || []).join(" ")}
|
| 19 |
+
data-testid={data_testid}
|
| 20 |
+
{...restProps}
|
| 21 |
+
on:load
|
| 22 |
+
/>
|
| 23 |
+
|
| 24 |
+
<style>
|
| 25 |
+
img {
|
| 26 |
+
object-fit: cover;
|
| 27 |
+
}
|
| 28 |
+
</style>
|
6.1.0/image/shared/ImagePreview.svelte
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher, onMount } from "svelte";
|
| 3 |
+
import type { SelectData } from "@gradio/utils";
|
| 4 |
+
import { uploadToHuggingFace } from "@gradio/utils";
|
| 5 |
+
import {
|
| 6 |
+
BlockLabel,
|
| 7 |
+
Empty,
|
| 8 |
+
IconButton,
|
| 9 |
+
ShareButton,
|
| 10 |
+
IconButtonWrapper,
|
| 11 |
+
FullscreenButton,
|
| 12 |
+
DownloadLink
|
| 13 |
+
} from "@gradio/atoms";
|
| 14 |
+
import { Download, Image as ImageIcon } from "@gradio/icons";
|
| 15 |
+
import { get_coordinates_of_clicked_image } from "./utils";
|
| 16 |
+
import Image from "./Image.svelte";
|
| 17 |
+
|
| 18 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 19 |
+
import type { FileData } from "@gradio/client";
|
| 20 |
+
|
| 21 |
+
export let value: null | FileData;
|
| 22 |
+
export let label: string | undefined = undefined;
|
| 23 |
+
export let show_label: boolean;
|
| 24 |
+
export let buttons: string[] | null = null;
|
| 25 |
+
export let selectable = false;
|
| 26 |
+
export let i18n: I18nFormatter;
|
| 27 |
+
export let display_icon_button_wrapper_top_corner = false;
|
| 28 |
+
export let fullscreen = false;
|
| 29 |
+
export let show_button_background = true;
|
| 30 |
+
|
| 31 |
+
const dispatch = createEventDispatcher<{
|
| 32 |
+
change: string;
|
| 33 |
+
select: SelectData;
|
| 34 |
+
fullscreen: boolean;
|
| 35 |
+
}>();
|
| 36 |
+
|
| 37 |
+
const handle_click = (evt: MouseEvent): void => {
|
| 38 |
+
let coordinates = get_coordinates_of_clicked_image(evt);
|
| 39 |
+
if (coordinates) {
|
| 40 |
+
dispatch("select", { index: coordinates, value: null });
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
let image_container: HTMLElement;
|
| 45 |
+
</script>
|
| 46 |
+
|
| 47 |
+
<BlockLabel
|
| 48 |
+
{show_label}
|
| 49 |
+
Icon={ImageIcon}
|
| 50 |
+
label={!show_label ? "" : label || i18n("image.image")}
|
| 51 |
+
/>
|
| 52 |
+
{#if value == null || !value?.url}
|
| 53 |
+
<Empty unpadded_box={true} size="large"><ImageIcon /></Empty>
|
| 54 |
+
{:else}
|
| 55 |
+
<div class="image-container" bind:this={image_container}>
|
| 56 |
+
<IconButtonWrapper
|
| 57 |
+
display_top_corner={display_icon_button_wrapper_top_corner}
|
| 58 |
+
show_background={show_button_background}
|
| 59 |
+
>
|
| 60 |
+
{#if buttons === null ? true : buttons.includes("fullscreen")}
|
| 61 |
+
<FullscreenButton {fullscreen} on:fullscreen />
|
| 62 |
+
{/if}
|
| 63 |
+
|
| 64 |
+
{#if buttons === null ? true : buttons.includes("download")}
|
| 65 |
+
<DownloadLink href={value.url} download={value.orig_name || "image"}>
|
| 66 |
+
<IconButton Icon={Download} label={i18n("common.download")} />
|
| 67 |
+
</DownloadLink>
|
| 68 |
+
{/if}
|
| 69 |
+
{#if buttons === null ? true : buttons.includes("share")}
|
| 70 |
+
<ShareButton
|
| 71 |
+
{i18n}
|
| 72 |
+
on:share
|
| 73 |
+
on:error
|
| 74 |
+
formatter={async (value) => {
|
| 75 |
+
if (!value) return "";
|
| 76 |
+
let url = await uploadToHuggingFace(value, "url");
|
| 77 |
+
return `<img src="${url}" />`;
|
| 78 |
+
}}
|
| 79 |
+
{value}
|
| 80 |
+
/>
|
| 81 |
+
{/if}
|
| 82 |
+
</IconButtonWrapper>
|
| 83 |
+
<button on:click={handle_click}>
|
| 84 |
+
<div class:selectable class="image-frame">
|
| 85 |
+
<Image
|
| 86 |
+
src={value.url}
|
| 87 |
+
restProps={{ loading: "lazy", alt: "" }}
|
| 88 |
+
on:load
|
| 89 |
+
/>
|
| 90 |
+
</div>
|
| 91 |
+
</button>
|
| 92 |
+
</div>
|
| 93 |
+
{/if}
|
| 94 |
+
|
| 95 |
+
<style>
|
| 96 |
+
.image-container {
|
| 97 |
+
height: 100%;
|
| 98 |
+
position: relative;
|
| 99 |
+
min-width: var(--size-20);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.image-container button {
|
| 103 |
+
width: var(--size-full);
|
| 104 |
+
height: var(--size-full);
|
| 105 |
+
border-radius: var(--radius-lg);
|
| 106 |
+
|
| 107 |
+
display: flex;
|
| 108 |
+
align-items: center;
|
| 109 |
+
justify-content: center;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
.image-frame :global(img) {
|
| 113 |
+
width: var(--size-full);
|
| 114 |
+
height: var(--size-full);
|
| 115 |
+
object-fit: scale-down;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
.selectable {
|
| 119 |
+
cursor: crosshair;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
:global(.fullscreen-controls svg) {
|
| 123 |
+
position: relative;
|
| 124 |
+
top: 0px;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
:global(.image-container:fullscreen) {
|
| 128 |
+
background-color: black;
|
| 129 |
+
display: flex;
|
| 130 |
+
justify-content: center;
|
| 131 |
+
align-items: center;
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
:global(.image-container:fullscreen img) {
|
| 135 |
+
max-width: 90vw;
|
| 136 |
+
max-height: 90vh;
|
| 137 |
+
object-fit: scale-down;
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
.image-frame {
|
| 141 |
+
width: auto;
|
| 142 |
+
height: 100%;
|
| 143 |
+
display: flex;
|
| 144 |
+
align-items: center;
|
| 145 |
+
justify-content: center;
|
| 146 |
+
}
|
| 147 |
+
</style>
|
6.1.0/image/shared/ImageUploader.svelte
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher, tick } from "svelte";
|
| 3 |
+
import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
|
| 4 |
+
import { Clear, Image as ImageIcon } from "@gradio/icons";
|
| 5 |
+
import { FullscreenButton } from "@gradio/atoms";
|
| 6 |
+
import {
|
| 7 |
+
type SelectData,
|
| 8 |
+
type I18nFormatter,
|
| 9 |
+
type ValueData
|
| 10 |
+
} from "@gradio/utils";
|
| 11 |
+
import { get_coordinates_of_clicked_image } from "./utils";
|
| 12 |
+
import Webcam from "./Webcam.svelte";
|
| 13 |
+
|
| 14 |
+
import { Upload, UploadProgress } from "@gradio/upload";
|
| 15 |
+
import { FileData, type Client } from "@gradio/client";
|
| 16 |
+
import { SelectSource } from "@gradio/atoms";
|
| 17 |
+
import Image from "./Image.svelte";
|
| 18 |
+
import type { Base64File, WebcamOptions } from "./types";
|
| 19 |
+
|
| 20 |
+
export let value: null | FileData | Base64File = null;
|
| 21 |
+
export let label: string | undefined = undefined;
|
| 22 |
+
export let show_label: boolean;
|
| 23 |
+
|
| 24 |
+
type source_type = "upload" | "webcam" | "clipboard" | "microphone" | null;
|
| 25 |
+
|
| 26 |
+
export let sources: source_type[] = ["upload", "clipboard", "webcam"];
|
| 27 |
+
export let streaming = false;
|
| 28 |
+
export let pending = false;
|
| 29 |
+
export let webcam_options: WebcamOptions;
|
| 30 |
+
export let selectable = false;
|
| 31 |
+
export let root: string;
|
| 32 |
+
export let i18n: I18nFormatter;
|
| 33 |
+
export let max_file_size: number | null = null;
|
| 34 |
+
export let upload: Client["upload"];
|
| 35 |
+
export let stream_handler: Client["stream"];
|
| 36 |
+
export let stream_every: number;
|
| 37 |
+
export let time_limit: number;
|
| 38 |
+
export let show_fullscreen_button = true;
|
| 39 |
+
export let stream_state: "open" | "waiting" | "closed" = "closed";
|
| 40 |
+
export let upload_promise: Promise<any> | null = null;
|
| 41 |
+
|
| 42 |
+
let upload_input: Upload;
|
| 43 |
+
export let uploading = false;
|
| 44 |
+
export let active_source: source_type = null;
|
| 45 |
+
export let fullscreen = false;
|
| 46 |
+
|
| 47 |
+
let files: FileData[] = [];
|
| 48 |
+
let upload_id: string;
|
| 49 |
+
|
| 50 |
+
async function handle_upload({
|
| 51 |
+
detail
|
| 52 |
+
}: CustomEvent<FileData>): Promise<void> {
|
| 53 |
+
if (!streaming) {
|
| 54 |
+
if (detail.path?.toLowerCase().endsWith(".svg") && detail.url) {
|
| 55 |
+
const response = await fetch(detail.url);
|
| 56 |
+
const svgContent = await response.text();
|
| 57 |
+
value = {
|
| 58 |
+
...detail,
|
| 59 |
+
url: `data:image/svg+xml,${encodeURIComponent(svgContent)}`
|
| 60 |
+
};
|
| 61 |
+
} else {
|
| 62 |
+
value = detail;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
await tick();
|
| 66 |
+
dispatch("upload");
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
function handle_clear(): void {
|
| 71 |
+
value = null;
|
| 72 |
+
dispatch("clear");
|
| 73 |
+
dispatch("change", null);
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function handle_remove_image_click(event: MouseEvent): void {
|
| 77 |
+
handle_clear();
|
| 78 |
+
event.stopPropagation();
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
async function handle_save(
|
| 82 |
+
img_blob: Blob | any,
|
| 83 |
+
event: "change" | "stream" | "upload"
|
| 84 |
+
): Promise<void> {
|
| 85 |
+
if (event === "stream") {
|
| 86 |
+
dispatch("stream", {
|
| 87 |
+
value: { url: img_blob } as Base64File,
|
| 88 |
+
is_value_data: true
|
| 89 |
+
});
|
| 90 |
+
return;
|
| 91 |
+
}
|
| 92 |
+
upload_id = Math.random().toString(36).substring(2, 15);
|
| 93 |
+
const f_ = new File([img_blob], `image.${streaming ? "jpeg" : "png"}`);
|
| 94 |
+
files = [
|
| 95 |
+
new FileData({
|
| 96 |
+
path: f_.name,
|
| 97 |
+
orig_name: f_.name,
|
| 98 |
+
blob: f_,
|
| 99 |
+
size: f_.size,
|
| 100 |
+
mime_type: f_.type,
|
| 101 |
+
is_stream: false
|
| 102 |
+
})
|
| 103 |
+
];
|
| 104 |
+
pending = true;
|
| 105 |
+
const f = await upload_input.load_files([f_], upload_id);
|
| 106 |
+
if (event === "change" || event === "upload") {
|
| 107 |
+
value = f?.[0] || null;
|
| 108 |
+
await tick();
|
| 109 |
+
dispatch("change");
|
| 110 |
+
}
|
| 111 |
+
pending = false;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
$: active_streaming = streaming && active_source === "webcam";
|
| 115 |
+
$: if (uploading && !active_streaming) value = null;
|
| 116 |
+
|
| 117 |
+
const dispatch = createEventDispatcher<{
|
| 118 |
+
change?: never;
|
| 119 |
+
stream: ValueData;
|
| 120 |
+
clear?: never;
|
| 121 |
+
drag: boolean;
|
| 122 |
+
upload?: never;
|
| 123 |
+
select: SelectData;
|
| 124 |
+
end_stream: never;
|
| 125 |
+
}>();
|
| 126 |
+
|
| 127 |
+
export let dragging = false;
|
| 128 |
+
|
| 129 |
+
$: dispatch("drag", dragging);
|
| 130 |
+
|
| 131 |
+
function handle_click(evt: MouseEvent): void {
|
| 132 |
+
let coordinates = get_coordinates_of_clicked_image(evt);
|
| 133 |
+
if (coordinates) {
|
| 134 |
+
dispatch("select", { index: coordinates, value: null });
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
$: if (!active_source && sources) {
|
| 139 |
+
active_source = sources[0];
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
async function handle_select_source(
|
| 143 |
+
source: (typeof sources)[number]
|
| 144 |
+
): Promise<void> {
|
| 145 |
+
switch (source) {
|
| 146 |
+
case "clipboard":
|
| 147 |
+
upload_input.paste_clipboard();
|
| 148 |
+
break;
|
| 149 |
+
default:
|
| 150 |
+
break;
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
let image_container: HTMLElement;
|
| 155 |
+
|
| 156 |
+
function on_drag_over(evt: DragEvent): void {
|
| 157 |
+
evt.preventDefault();
|
| 158 |
+
evt.stopPropagation();
|
| 159 |
+
if (evt.dataTransfer) {
|
| 160 |
+
evt.dataTransfer.dropEffect = "copy";
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
dragging = true;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
async function on_drop(evt: DragEvent): Promise<void> {
|
| 167 |
+
evt.preventDefault();
|
| 168 |
+
evt.stopPropagation();
|
| 169 |
+
dragging = false;
|
| 170 |
+
|
| 171 |
+
if (value) {
|
| 172 |
+
handle_clear();
|
| 173 |
+
await tick();
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
active_source = "upload";
|
| 177 |
+
await tick();
|
| 178 |
+
upload_input.load_files_from_drop(evt);
|
| 179 |
+
}
|
| 180 |
+
</script>
|
| 181 |
+
|
| 182 |
+
<BlockLabel {show_label} Icon={ImageIcon} label={label || "Image"} />
|
| 183 |
+
|
| 184 |
+
<div data-testid="image" class="image-container" bind:this={image_container}>
|
| 185 |
+
<IconButtonWrapper>
|
| 186 |
+
{#if value?.url && !active_streaming}
|
| 187 |
+
{#if show_fullscreen_button}
|
| 188 |
+
<FullscreenButton {fullscreen} on:fullscreen />
|
| 189 |
+
{/if}
|
| 190 |
+
<IconButton
|
| 191 |
+
Icon={Clear}
|
| 192 |
+
label="Remove Image"
|
| 193 |
+
on:click={handle_remove_image_click}
|
| 194 |
+
/>
|
| 195 |
+
{/if}
|
| 196 |
+
</IconButtonWrapper>
|
| 197 |
+
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
| 198 |
+
<div
|
| 199 |
+
class="upload-container"
|
| 200 |
+
class:reduced-height={sources.length > 1}
|
| 201 |
+
style:width={value ? "auto" : "100%"}
|
| 202 |
+
on:dragover={on_drag_over}
|
| 203 |
+
on:drop={on_drop}
|
| 204 |
+
>
|
| 205 |
+
<Upload
|
| 206 |
+
bind:upload_promise
|
| 207 |
+
hidden={value !== null || active_source === "webcam"}
|
| 208 |
+
bind:this={upload_input}
|
| 209 |
+
bind:uploading
|
| 210 |
+
bind:dragging
|
| 211 |
+
filetype={active_source === "clipboard" ? "clipboard" : "image/*"}
|
| 212 |
+
on:load={handle_upload}
|
| 213 |
+
on:error
|
| 214 |
+
{root}
|
| 215 |
+
{max_file_size}
|
| 216 |
+
disable_click={!sources.includes("upload") || value !== null}
|
| 217 |
+
{upload}
|
| 218 |
+
{stream_handler}
|
| 219 |
+
aria_label={i18n("image.drop_to_upload")}
|
| 220 |
+
>
|
| 221 |
+
{#if value === null}
|
| 222 |
+
<slot />
|
| 223 |
+
{/if}
|
| 224 |
+
</Upload>
|
| 225 |
+
{#if active_source === "webcam" && !streaming && pending}
|
| 226 |
+
<UploadProgress {root} {upload_id} {stream_handler} {files} />
|
| 227 |
+
{:else if active_source === "webcam" && (streaming || (!streaming && !value))}
|
| 228 |
+
<Webcam
|
| 229 |
+
{root}
|
| 230 |
+
{value}
|
| 231 |
+
on:capture={(e) => handle_save(e.detail, "change")}
|
| 232 |
+
on:stream={(e) => handle_save(e.detail, "stream")}
|
| 233 |
+
on:error
|
| 234 |
+
on:drag
|
| 235 |
+
on:upload={(e) => handle_save(e.detail, "upload")}
|
| 236 |
+
on:close_stream
|
| 237 |
+
{stream_state}
|
| 238 |
+
mirror_webcam={webcam_options.mirror}
|
| 239 |
+
{stream_every}
|
| 240 |
+
{streaming}
|
| 241 |
+
mode="image"
|
| 242 |
+
include_audio={false}
|
| 243 |
+
{i18n}
|
| 244 |
+
{upload}
|
| 245 |
+
{time_limit}
|
| 246 |
+
webcam_constraints={webcam_options.constraints}
|
| 247 |
+
/>
|
| 248 |
+
{:else if value !== null && !streaming}
|
| 249 |
+
<!-- svelte-ignore a11y-click-events-have-key-events-->
|
| 250 |
+
<!-- svelte-ignore a11y-no-static-element-interactions-->
|
| 251 |
+
<div class:selectable class="image-frame" on:click={handle_click}>
|
| 252 |
+
<Image src={value.url} restProps={{ alt: value.alt_text }} />
|
| 253 |
+
</div>
|
| 254 |
+
{/if}
|
| 255 |
+
</div>
|
| 256 |
+
{#if sources.length > 1 || sources.includes("clipboard")}
|
| 257 |
+
<SelectSource
|
| 258 |
+
{sources}
|
| 259 |
+
bind:active_source
|
| 260 |
+
{handle_clear}
|
| 261 |
+
handle_select={handle_select_source}
|
| 262 |
+
/>
|
| 263 |
+
{/if}
|
| 264 |
+
</div>
|
| 265 |
+
|
| 266 |
+
<style>
|
| 267 |
+
.image-frame :global(img) {
|
| 268 |
+
width: var(--size-full);
|
| 269 |
+
height: var(--size-full);
|
| 270 |
+
object-fit: scale-down;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
.upload-container {
|
| 274 |
+
display: flex;
|
| 275 |
+
align-items: center;
|
| 276 |
+
justify-content: center;
|
| 277 |
+
|
| 278 |
+
height: 100%;
|
| 279 |
+
flex-shrink: 1;
|
| 280 |
+
max-height: 100%;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
.reduced-height {
|
| 284 |
+
height: calc(100% - var(--size-10));
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
.image-container {
|
| 288 |
+
display: flex;
|
| 289 |
+
height: 100%;
|
| 290 |
+
flex-direction: column;
|
| 291 |
+
justify-content: center;
|
| 292 |
+
align-items: center;
|
| 293 |
+
max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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher, onDestroy, onMount } from "svelte";
|
| 3 |
+
import {
|
| 4 |
+
Camera,
|
| 5 |
+
Circle,
|
| 6 |
+
Square,
|
| 7 |
+
DropdownArrow,
|
| 8 |
+
Spinner
|
| 9 |
+
} from "@gradio/icons";
|
| 10 |
+
import type { I18nFormatter } from "@gradio/utils";
|
| 11 |
+
import { StreamingBar } from "@gradio/statustracker";
|
| 12 |
+
import { type FileData, type Client, prepare_files } from "@gradio/client";
|
| 13 |
+
import WebcamPermissions from "./WebcamPermissions.svelte";
|
| 14 |
+
import { fade } from "svelte/transition";
|
| 15 |
+
import {
|
| 16 |
+
get_devices,
|
| 17 |
+
get_video_stream,
|
| 18 |
+
set_available_devices
|
| 19 |
+
} from "./stream_utils";
|
| 20 |
+
import type { Base64File } from "./types";
|
| 21 |
+
|
| 22 |
+
let video_source: HTMLVideoElement;
|
| 23 |
+
let available_video_devices: MediaDeviceInfo[] = [];
|
| 24 |
+
let selected_device: MediaDeviceInfo | null = null;
|
| 25 |
+
|
| 26 |
+
export let stream_state: "open" | "waiting" | "closed" = "closed";
|
| 27 |
+
|
| 28 |
+
let canvas: HTMLCanvasElement;
|
| 29 |
+
export let streaming = false;
|
| 30 |
+
export let pending = false;
|
| 31 |
+
export let root = "";
|
| 32 |
+
export let stream_every = 1;
|
| 33 |
+
|
| 34 |
+
export let mode: "image" | "video" = "image";
|
| 35 |
+
export let mirror_webcam: boolean;
|
| 36 |
+
export let include_audio: boolean;
|
| 37 |
+
export let webcam_constraints: { [key: string]: any } | null = null;
|
| 38 |
+
export let i18n: I18nFormatter;
|
| 39 |
+
export let upload: Client["upload"];
|
| 40 |
+
export let value: FileData | null | Base64File = null;
|
| 41 |
+
export let time_limit: number | null = null;
|
| 42 |
+
const dispatch = createEventDispatcher<{
|
| 43 |
+
stream: Blob | string;
|
| 44 |
+
capture: FileData | Blob | null;
|
| 45 |
+
error: string;
|
| 46 |
+
start_recording: undefined;
|
| 47 |
+
stop_recording: undefined;
|
| 48 |
+
close_stream: undefined;
|
| 49 |
+
}>();
|
| 50 |
+
|
| 51 |
+
onMount(() => {
|
| 52 |
+
canvas = document.createElement("canvas");
|
| 53 |
+
if (streaming && mode === "image") {
|
| 54 |
+
window.setInterval(() => {
|
| 55 |
+
if (video_source && !pending) {
|
| 56 |
+
take_picture();
|
| 57 |
+
}
|
| 58 |
+
}, stream_every * 1000);
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
const handle_device_change = async (event: InputEvent): Promise<void> => {
|
| 63 |
+
const target = event.target as HTMLInputElement;
|
| 64 |
+
const device_id = target.value;
|
| 65 |
+
|
| 66 |
+
await get_video_stream(
|
| 67 |
+
include_audio,
|
| 68 |
+
video_source,
|
| 69 |
+
webcam_constraints,
|
| 70 |
+
device_id
|
| 71 |
+
).then(async (local_stream) => {
|
| 72 |
+
stream = local_stream;
|
| 73 |
+
selected_device =
|
| 74 |
+
available_video_devices.find(
|
| 75 |
+
(device) => device.deviceId === device_id
|
| 76 |
+
) || null;
|
| 77 |
+
options_open = false;
|
| 78 |
+
});
|
| 79 |
+
};
|
| 80 |
+
|
| 81 |
+
async function access_webcam(): Promise<void> {
|
| 82 |
+
try {
|
| 83 |
+
get_video_stream(include_audio, video_source, webcam_constraints)
|
| 84 |
+
.then(async (local_stream) => {
|
| 85 |
+
webcam_accessed = true;
|
| 86 |
+
available_video_devices = await get_devices();
|
| 87 |
+
stream = local_stream;
|
| 88 |
+
})
|
| 89 |
+
.then(() => set_available_devices(available_video_devices))
|
| 90 |
+
.then((devices) => {
|
| 91 |
+
available_video_devices = devices;
|
| 92 |
+
|
| 93 |
+
const used_devices = stream
|
| 94 |
+
.getTracks()
|
| 95 |
+
.map((track) => track.getSettings()?.deviceId)[0];
|
| 96 |
+
|
| 97 |
+
selected_device = used_devices
|
| 98 |
+
? devices.find((device) => device.deviceId === used_devices) ||
|
| 99 |
+
available_video_devices[0]
|
| 100 |
+
: available_video_devices[0];
|
| 101 |
+
});
|
| 102 |
+
|
| 103 |
+
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
| 104 |
+
dispatch("error", i18n("image.no_webcam_support"));
|
| 105 |
+
}
|
| 106 |
+
} catch (err) {
|
| 107 |
+
if (err instanceof DOMException && err.name == "NotAllowedError") {
|
| 108 |
+
dispatch("error", i18n("image.allow_webcam_access"));
|
| 109 |
+
} else {
|
| 110 |
+
throw err;
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
function take_picture(): void {
|
| 116 |
+
if (
|
| 117 |
+
(!streaming || (streaming && recording)) &&
|
| 118 |
+
video_source.videoWidth &&
|
| 119 |
+
video_source.videoHeight
|
| 120 |
+
) {
|
| 121 |
+
var context = canvas.getContext("2d")!;
|
| 122 |
+
canvas.width = video_source.videoWidth;
|
| 123 |
+
canvas.height = video_source.videoHeight;
|
| 124 |
+
context.drawImage(
|
| 125 |
+
video_source,
|
| 126 |
+
0,
|
| 127 |
+
0,
|
| 128 |
+
video_source.videoWidth,
|
| 129 |
+
video_source.videoHeight
|
| 130 |
+
);
|
| 131 |
+
|
| 132 |
+
if (mirror_webcam) {
|
| 133 |
+
context.scale(-1, 1);
|
| 134 |
+
context.drawImage(video_source, -video_source.videoWidth, 0);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
if (streaming && (!recording || stream_state === "waiting")) {
|
| 138 |
+
return;
|
| 139 |
+
}
|
| 140 |
+
if (streaming) {
|
| 141 |
+
const image_data = canvas.toDataURL("image/jpeg");
|
| 142 |
+
dispatch("stream", image_data);
|
| 143 |
+
return;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
canvas.toBlob(
|
| 147 |
+
(blob) => {
|
| 148 |
+
dispatch(streaming ? "stream" : "capture", blob);
|
| 149 |
+
},
|
| 150 |
+
`image/${streaming ? "jpeg" : "png"}`,
|
| 151 |
+
0.8
|
| 152 |
+
);
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
let recording = false;
|
| 157 |
+
let recorded_blobs: BlobPart[] = [];
|
| 158 |
+
let stream: MediaStream;
|
| 159 |
+
let mimeType: string;
|
| 160 |
+
let media_recorder: MediaRecorder;
|
| 161 |
+
|
| 162 |
+
function take_recording(): void {
|
| 163 |
+
if (recording) {
|
| 164 |
+
media_recorder.stop();
|
| 165 |
+
let video_blob = new Blob(recorded_blobs, { type: mimeType });
|
| 166 |
+
let ReaderObj = new FileReader();
|
| 167 |
+
ReaderObj.onload = async function (e): Promise<void> {
|
| 168 |
+
if (e.target) {
|
| 169 |
+
let _video_blob = new File(
|
| 170 |
+
[video_blob],
|
| 171 |
+
"sample." + mimeType.substring(6)
|
| 172 |
+
);
|
| 173 |
+
const val = await prepare_files([_video_blob]);
|
| 174 |
+
let val_ = (
|
| 175 |
+
(await upload(val, root))?.filter(Boolean) as FileData[]
|
| 176 |
+
)[0];
|
| 177 |
+
dispatch("capture", val_);
|
| 178 |
+
dispatch("stop_recording");
|
| 179 |
+
}
|
| 180 |
+
};
|
| 181 |
+
ReaderObj.readAsDataURL(video_blob);
|
| 182 |
+
} else if (typeof MediaRecorder !== "undefined") {
|
| 183 |
+
dispatch("start_recording");
|
| 184 |
+
recorded_blobs = [];
|
| 185 |
+
let validMimeTypes = ["video/webm", "video/mp4"];
|
| 186 |
+
for (let validMimeType of validMimeTypes) {
|
| 187 |
+
if (MediaRecorder.isTypeSupported(validMimeType)) {
|
| 188 |
+
mimeType = validMimeType;
|
| 189 |
+
break;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
if (mimeType === null) {
|
| 193 |
+
console.error("No supported MediaRecorder mimeType");
|
| 194 |
+
return;
|
| 195 |
+
}
|
| 196 |
+
media_recorder = new MediaRecorder(stream, {
|
| 197 |
+
mimeType: mimeType
|
| 198 |
+
});
|
| 199 |
+
media_recorder.addEventListener("dataavailable", function (e) {
|
| 200 |
+
recorded_blobs.push(e.data);
|
| 201 |
+
});
|
| 202 |
+
media_recorder.start(200);
|
| 203 |
+
}
|
| 204 |
+
recording = !recording;
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
let webcam_accessed = false;
|
| 208 |
+
|
| 209 |
+
function record_video_or_photo({
|
| 210 |
+
destroy
|
| 211 |
+
}: { destroy?: boolean } = {}): void {
|
| 212 |
+
if (mode === "image" && streaming) {
|
| 213 |
+
recording = !recording;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
if (!destroy) {
|
| 217 |
+
if (mode === "image") {
|
| 218 |
+
take_picture();
|
| 219 |
+
} else {
|
| 220 |
+
take_recording();
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
if (!recording && stream) {
|
| 225 |
+
dispatch("close_stream");
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
let options_open = false;
|
| 230 |
+
|
| 231 |
+
export function click_outside(node: Node, cb: any): any {
|
| 232 |
+
const handle_click = (event: MouseEvent): void => {
|
| 233 |
+
if (
|
| 234 |
+
node &&
|
| 235 |
+
!node.contains(event.target as Node) &&
|
| 236 |
+
!event.defaultPrevented
|
| 237 |
+
) {
|
| 238 |
+
cb(event);
|
| 239 |
+
}
|
| 240 |
+
};
|
| 241 |
+
|
| 242 |
+
document.addEventListener("click", handle_click, true);
|
| 243 |
+
|
| 244 |
+
return {
|
| 245 |
+
destroy() {
|
| 246 |
+
document.removeEventListener("click", handle_click, true);
|
| 247 |
+
}
|
| 248 |
+
};
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
function handle_click_outside(event: MouseEvent): void {
|
| 252 |
+
event.preventDefault();
|
| 253 |
+
event.stopPropagation();
|
| 254 |
+
options_open = false;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
onDestroy(() => {
|
| 258 |
+
if (typeof window === "undefined") return;
|
| 259 |
+
record_video_or_photo({ destroy: true });
|
| 260 |
+
stream?.getTracks().forEach((track) => track.stop());
|
| 261 |
+
});
|
| 262 |
+
</script>
|
| 263 |
+
|
| 264 |
+
<div class="wrap">
|
| 265 |
+
<StreamingBar {time_limit} />
|
| 266 |
+
<!-- svelte-ignore a11y-media-has-caption -->
|
| 267 |
+
<!-- need to suppress for video streaming https://github.com/sveltejs/svelte/issues/5967 -->
|
| 268 |
+
<video
|
| 269 |
+
bind:this={video_source}
|
| 270 |
+
class:flip={mirror_webcam}
|
| 271 |
+
class:hide={!webcam_accessed || (webcam_accessed && !!value)}
|
| 272 |
+
/>
|
| 273 |
+
<!-- svelte-ignore a11y-missing-attribute -->
|
| 274 |
+
<img
|
| 275 |
+
src={value?.url}
|
| 276 |
+
class:hide={!webcam_accessed || (webcam_accessed && !value)}
|
| 277 |
+
/>
|
| 278 |
+
{#if !webcam_accessed}
|
| 279 |
+
<div
|
| 280 |
+
in:fade={{ delay: 100, duration: 200 }}
|
| 281 |
+
title="grant webcam access"
|
| 282 |
+
style="height: 100%"
|
| 283 |
+
>
|
| 284 |
+
<WebcamPermissions on:click={async () => access_webcam()} />
|
| 285 |
+
</div>
|
| 286 |
+
{:else}
|
| 287 |
+
<div class="button-wrap">
|
| 288 |
+
<button
|
| 289 |
+
on:click={() => record_video_or_photo()}
|
| 290 |
+
aria-label={mode === "image" ? "capture photo" : "start recording"}
|
| 291 |
+
>
|
| 292 |
+
{#if mode === "video" || streaming}
|
| 293 |
+
{#if streaming && stream_state === "waiting"}
|
| 294 |
+
<div class="icon-with-text" style="width:var(--size-24);">
|
| 295 |
+
<div class="icon color-primary" title="spinner">
|
| 296 |
+
<Spinner />
|
| 297 |
+
</div>
|
| 298 |
+
{i18n("audio.waiting")}
|
| 299 |
+
</div>
|
| 300 |
+
{:else if (streaming && stream_state === "open") || (!streaming && recording)}
|
| 301 |
+
<div class="icon-with-text">
|
| 302 |
+
<div class="icon color-primary" title="stop recording">
|
| 303 |
+
<Square />
|
| 304 |
+
</div>
|
| 305 |
+
{i18n("audio.stop")}
|
| 306 |
+
</div>
|
| 307 |
+
{:else}
|
| 308 |
+
<div class="icon-with-text">
|
| 309 |
+
<div class="icon color-primary" title="start recording">
|
| 310 |
+
<Circle />
|
| 311 |
+
</div>
|
| 312 |
+
{i18n("audio.record")}
|
| 313 |
+
</div>
|
| 314 |
+
{/if}
|
| 315 |
+
{:else}
|
| 316 |
+
<div class="icon" title="capture photo">
|
| 317 |
+
<Camera />
|
| 318 |
+
</div>
|
| 319 |
+
{/if}
|
| 320 |
+
</button>
|
| 321 |
+
{#if !recording}
|
| 322 |
+
<button
|
| 323 |
+
class="icon"
|
| 324 |
+
on:click={() => (options_open = true)}
|
| 325 |
+
aria-label="select input source"
|
| 326 |
+
>
|
| 327 |
+
<DropdownArrow />
|
| 328 |
+
</button>
|
| 329 |
+
{/if}
|
| 330 |
+
</div>
|
| 331 |
+
{#if options_open && selected_device}
|
| 332 |
+
<select
|
| 333 |
+
class="select-wrap"
|
| 334 |
+
aria-label="select source"
|
| 335 |
+
use:click_outside={handle_click_outside}
|
| 336 |
+
on:change={handle_device_change}
|
| 337 |
+
>
|
| 338 |
+
<!-- <button
|
| 339 |
+
class="inset-icon"
|
| 340 |
+
on:click|stopPropagation={() => (options_open = false)}
|
| 341 |
+
>
|
| 342 |
+
<DropdownArrow />
|
| 343 |
+
</button> -->
|
| 344 |
+
{#if available_video_devices.length === 0}
|
| 345 |
+
<option value="">{i18n("common.no_devices")}</option>
|
| 346 |
+
{:else}
|
| 347 |
+
{#each available_video_devices as device}
|
| 348 |
+
<option
|
| 349 |
+
value={device.deviceId}
|
| 350 |
+
selected={selected_device.deviceId === device.deviceId}
|
| 351 |
+
>
|
| 352 |
+
{device.label}
|
| 353 |
+
</option>
|
| 354 |
+
{/each}
|
| 355 |
+
{/if}
|
| 356 |
+
</select>
|
| 357 |
+
{/if}
|
| 358 |
+
{/if}
|
| 359 |
+
</div>
|
| 360 |
+
|
| 361 |
+
<style>
|
| 362 |
+
.wrap {
|
| 363 |
+
position: relative;
|
| 364 |
+
width: var(--size-full);
|
| 365 |
+
height: var(--size-full);
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
.hide {
|
| 369 |
+
display: none;
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
video {
|
| 373 |
+
width: var(--size-full);
|
| 374 |
+
height: var(--size-full);
|
| 375 |
+
object-fit: contain;
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
.button-wrap {
|
| 379 |
+
position: absolute;
|
| 380 |
+
background-color: var(--block-background-fill);
|
| 381 |
+
border: 1px solid var(--border-color-primary);
|
| 382 |
+
border-radius: var(--radius-xl);
|
| 383 |
+
padding: var(--size-1-5);
|
| 384 |
+
display: flex;
|
| 385 |
+
bottom: var(--size-2);
|
| 386 |
+
left: 50%;
|
| 387 |
+
transform: translate(-50%, 0);
|
| 388 |
+
box-shadow: var(--shadow-drop-lg);
|
| 389 |
+
border-radius: var(--radius-xl);
|
| 390 |
+
line-height: var(--size-3);
|
| 391 |
+
color: var(--button-secondary-text-color);
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
.icon-with-text {
|
| 395 |
+
width: var(--size-20);
|
| 396 |
+
align-items: center;
|
| 397 |
+
margin: 0 var(--spacing-xl);
|
| 398 |
+
display: flex;
|
| 399 |
+
justify-content: space-evenly;
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
@media (--screen-md) {
|
| 403 |
+
button {
|
| 404 |
+
bottom: var(--size-4);
|
| 405 |
+
}
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
@media (--screen-xl) {
|
| 409 |
+
button {
|
| 410 |
+
bottom: var(--size-8);
|
| 411 |
+
}
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
.icon {
|
| 415 |
+
width: 18px;
|
| 416 |
+
height: 18px;
|
| 417 |
+
display: flex;
|
| 418 |
+
justify-content: space-between;
|
| 419 |
+
align-items: center;
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
.color-primary {
|
| 423 |
+
fill: var(--primary-600);
|
| 424 |
+
stroke: var(--primary-600);
|
| 425 |
+
color: var(--primary-600);
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
.flip {
|
| 429 |
+
transform: scaleX(-1);
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
.select-wrap {
|
| 433 |
+
-webkit-appearance: none;
|
| 434 |
+
-moz-appearance: none;
|
| 435 |
+
appearance: none;
|
| 436 |
+
color: var(--button-secondary-text-color);
|
| 437 |
+
background-color: transparent;
|
| 438 |
+
width: 95%;
|
| 439 |
+
font-size: var(--text-md);
|
| 440 |
+
position: absolute;
|
| 441 |
+
bottom: var(--size-2);
|
| 442 |
+
background-color: var(--block-background-fill);
|
| 443 |
+
box-shadow: var(--shadow-drop-lg);
|
| 444 |
+
border-radius: var(--radius-xl);
|
| 445 |
+
z-index: var(--layer-top);
|
| 446 |
+
border: 1px solid var(--border-color-primary);
|
| 447 |
+
text-align: left;
|
| 448 |
+
line-height: var(--size-4);
|
| 449 |
+
white-space: nowrap;
|
| 450 |
+
text-overflow: ellipsis;
|
| 451 |
+
left: 50%;
|
| 452 |
+
transform: translate(-50%, 0);
|
| 453 |
+
max-width: var(--size-52);
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
.select-wrap > option {
|
| 457 |
+
padding: 0.25rem 0.5rem;
|
| 458 |
+
border-bottom: 1px solid var(--border-color-accent);
|
| 459 |
+
padding-right: var(--size-8);
|
| 460 |
+
text-overflow: ellipsis;
|
| 461 |
+
overflow: hidden;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
.select-wrap > option:hover {
|
| 465 |
+
background-color: var(--color-accent);
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
.select-wrap > option:last-child {
|
| 469 |
+
border: none;
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
.inset-icon {
|
| 473 |
+
position: absolute;
|
| 474 |
+
top: 5px;
|
| 475 |
+
right: -6.5px;
|
| 476 |
+
width: var(--size-10);
|
| 477 |
+
height: var(--size-5);
|
| 478 |
+
opacity: 0.8;
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
@media (--screen-md) {
|
| 482 |
+
.wrap {
|
| 483 |
+
font-size: var(--text-lg);
|
| 484 |
+
}
|
| 485 |
+
}
|
| 486 |
+
</style>
|
6.1.0/image/shared/WebcamPermissions.svelte
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { Webcam } from "@gradio/icons";
|
| 3 |
+
import { createEventDispatcher } from "svelte";
|
| 4 |
+
|
| 5 |
+
const dispatch = createEventDispatcher<{
|
| 6 |
+
click: undefined;
|
| 7 |
+
}>();
|
| 8 |
+
</script>
|
| 9 |
+
|
| 10 |
+
<button style:height="100%" on:click={() => dispatch("click")}>
|
| 11 |
+
<div class="wrap">
|
| 12 |
+
<span class="icon-wrap">
|
| 13 |
+
<Webcam />
|
| 14 |
+
</span>
|
| 15 |
+
{"Click to Access Webcam"}
|
| 16 |
+
</div>
|
| 17 |
+
</button>
|
| 18 |
+
|
| 19 |
+
<style>
|
| 20 |
+
button {
|
| 21 |
+
cursor: pointer;
|
| 22 |
+
width: var(--size-full);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.wrap {
|
| 26 |
+
display: flex;
|
| 27 |
+
flex-direction: column;
|
| 28 |
+
justify-content: center;
|
| 29 |
+
align-items: center;
|
| 30 |
+
min-height: var(--size-60);
|
| 31 |
+
color: var(--block-label-text-color);
|
| 32 |
+
height: 100%;
|
| 33 |
+
padding-top: var(--size-3);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
.icon-wrap {
|
| 37 |
+
width: 30px;
|
| 38 |
+
margin-bottom: var(--spacing-lg);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
@media (--screen-md) {
|
| 42 |
+
.wrap {
|
| 43 |
+
font-size: var(--text-lg);
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
</style>
|
6.1.0/image/shared/index.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { default as Image } from "./Image.svelte";
|
| 2 |
+
export { default as StaticImage } from "./ImagePreview.svelte";
|
6.1.0/image/shared/stream_utils.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function get_devices(): Promise<MediaDeviceInfo[]> {
|
| 2 |
+
return navigator.mediaDevices.enumerateDevices();
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
export function handle_error(error: string): void {
|
| 6 |
+
throw new Error(error);
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export function set_local_stream(
|
| 10 |
+
local_stream: MediaStream | null,
|
| 11 |
+
video_source: HTMLVideoElement
|
| 12 |
+
): void {
|
| 13 |
+
video_source.srcObject = local_stream;
|
| 14 |
+
video_source.muted = true;
|
| 15 |
+
video_source.play();
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
export async function get_video_stream(
|
| 19 |
+
include_audio: boolean,
|
| 20 |
+
video_source: HTMLVideoElement,
|
| 21 |
+
webcam_constraints: { [key: string]: any } | null,
|
| 22 |
+
device_id?: string
|
| 23 |
+
): Promise<MediaStream> {
|
| 24 |
+
const constraints: MediaStreamConstraints = {
|
| 25 |
+
video: device_id
|
| 26 |
+
? { deviceId: { exact: device_id }, ...webcam_constraints?.video }
|
| 27 |
+
: webcam_constraints?.video || {
|
| 28 |
+
width: { ideal: 1920 },
|
| 29 |
+
height: { ideal: 1440 }
|
| 30 |
+
},
|
| 31 |
+
audio: include_audio && (webcam_constraints?.audio ?? true) // Defaults to true if not specified
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
return navigator.mediaDevices
|
| 35 |
+
.getUserMedia(constraints)
|
| 36 |
+
.then((local_stream: MediaStream) => {
|
| 37 |
+
set_local_stream(local_stream, video_source);
|
| 38 |
+
return local_stream;
|
| 39 |
+
});
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
export function set_available_devices(
|
| 43 |
+
devices: MediaDeviceInfo[]
|
| 44 |
+
): MediaDeviceInfo[] {
|
| 45 |
+
const cameras = devices.filter(
|
| 46 |
+
(device: MediaDeviceInfo) => device.kind === "videoinput"
|
| 47 |
+
);
|
| 48 |
+
|
| 49 |
+
return cameras;
|
| 50 |
+
}
|
6.1.0/image/shared/types.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
| 2 |
+
import type { FileData } from "@gradio/client";
|
| 3 |
+
|
| 4 |
+
export interface Base64File {
|
| 5 |
+
url: string;
|
| 6 |
+
alt_text: string;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export interface WebcamOptions {
|
| 10 |
+
mirror: boolean;
|
| 11 |
+
constraints: MediaStreamConstraints;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export interface ImageProps {
|
| 15 |
+
_selectable: boolean;
|
| 16 |
+
sources: ("clipboard" | "webcam" | "upload")[];
|
| 17 |
+
height: number;
|
| 18 |
+
width: number;
|
| 19 |
+
webcam_options: WebcamOptions;
|
| 20 |
+
value: FileData | null;
|
| 21 |
+
buttons: string[];
|
| 22 |
+
pending: boolean;
|
| 23 |
+
streaming: boolean;
|
| 24 |
+
stream_every: number;
|
| 25 |
+
input_ready: boolean;
|
| 26 |
+
placeholder: string;
|
| 27 |
+
watermark: FileData | null;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
export interface ImageEvents {
|
| 31 |
+
clear: void;
|
| 32 |
+
change: any;
|
| 33 |
+
stream: any;
|
| 34 |
+
select: any;
|
| 35 |
+
upload: any;
|
| 36 |
+
input: any;
|
| 37 |
+
clear_status: LoadingStatus;
|
| 38 |
+
share: any;
|
| 39 |
+
error: any;
|
| 40 |
+
close_stream: void;
|
| 41 |
+
edit: void;
|
| 42 |
+
}
|
6.1.0/image/shared/utils.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const get_coordinates_of_clicked_image = (
|
| 2 |
+
evt: MouseEvent
|
| 3 |
+
): [number, number] | null => {
|
| 4 |
+
let image;
|
| 5 |
+
if (evt.currentTarget instanceof Element) {
|
| 6 |
+
image = evt.currentTarget.querySelector("img") as HTMLImageElement;
|
| 7 |
+
} else {
|
| 8 |
+
return [NaN, NaN];
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const imageRect = image.getBoundingClientRect();
|
| 12 |
+
const xScale = image.naturalWidth / imageRect.width;
|
| 13 |
+
const yScale = image.naturalHeight / imageRect.height;
|
| 14 |
+
if (xScale > yScale) {
|
| 15 |
+
const displayed_height = image.naturalHeight / xScale;
|
| 16 |
+
const y_offset = (imageRect.height - displayed_height) / 2;
|
| 17 |
+
var x = Math.round((evt.clientX - imageRect.left) * xScale);
|
| 18 |
+
var y = Math.round((evt.clientY - imageRect.top - y_offset) * xScale);
|
| 19 |
+
} else {
|
| 20 |
+
const displayed_width = image.naturalWidth / yScale;
|
| 21 |
+
const x_offset = (imageRect.width - displayed_width) / 2;
|
| 22 |
+
var x = Math.round((evt.clientX - imageRect.left - x_offset) * yScale);
|
| 23 |
+
var y = Math.round((evt.clientY - imageRect.top) * yScale);
|
| 24 |
+
}
|
| 25 |
+
if (x < 0 || x >= image.naturalWidth || y < 0 || y >= image.naturalHeight) {
|
| 26 |
+
return null;
|
| 27 |
+
}
|
| 28 |
+
return [x, y];
|
| 29 |
+
};
|