Spaces:
Running
Running
File size: 5,674 Bytes
23d9a29 a6cafe4 23d9a29 0eead1f 9342eb9 0eead1f 23d9a29 62b8cbf 23d9a29 0eead1f 23d9a29 0eead1f 23d9a29 0eead1f 23d9a29 0eead1f 23d9a29 0eead1f 9342eb9 0eead1f 9342eb9 0eead1f 62b8cbf 0eead1f 9342eb9 0eead1f 9342eb9 0eead1f 9342eb9 0eead1f 9342eb9 0eead1f 9342eb9 0eead1f 7067431 0eead1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# Copyright 2023-2025 Marigold Team, ETH Zürich. All rights reserved.
# This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
# See https://creativecommons.org/licenses/by-sa/4.0/ for details.
# --------------------------------------------------------------------------
# DualVision is a Gradio template app for image processing. It was developed
# to support the Marigold project. If you find this code useful, we kindly
# ask you to cite our most relevant papers.
# More information about Marigold:
# https://marigoldmonodepth.github.io
# https://marigoldcomputervision.github.io
# Efficient inference pipelines are now part of diffusers:
# https://huggingface.co/docs/diffusers/using-diffusers/marigold_usage
# https://huggingface.co/docs/diffusers/api/pipelines/marigold
# Examples of trained models and live demos:
# https://huggingface.co/prs-eth
# Related projects:
# https://marigolddepthcompletion.github.io/
# https://rollingdepth.github.io/
# Citation (BibTeX):
# https://github.com/prs-eth/Marigold#-citation
# https://github.com/prs-eth/Marigold-DC#-citation
# https://github.com/prs-eth/rollingdepth#-citation
# --------------------------------------------------------------------------
import gradio
import gradio.component_meta as _component_meta
def _noop_create_or_modify_pyi(*args, **kwargs):
return None
_component_meta.create_or_modify_pyi = _noop_create_or_modify_pyi
import numpy as np
import PIL.Image
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from urllib.parse import quote, urlparse
from gradio import FileData, image_utils, processing_utils, utils, wasm_utils
from gradio.components.gallery import (
GalleryData,
GalleryImage,
GalleryMediaType,
CaptionedGalleryMediaType,
GalleryVideo,
)
from gradio_client import utils as client_utils
from gradio_client.utils import is_http_url_like
from gradio.data_classes import ImageData
class Gallery(gradio.Gallery):
def postprocess(
self,
value: list[GalleryMediaType | CaptionedGalleryMediaType] | None,
) -> GalleryData:
"""
Parameters:
value: Expects the function to return a `list` of images or videos, or `list` of (media, `str` caption) tuples. Each image can be a `str` file path, a `numpy` array, or a `PIL.Image` object. Each video can be a `str` file path.
Returns:
a list of images or videos, or list of (media, caption) tuples
"""
if value is None:
return GalleryData(root=[])
if isinstance(value, str):
raise ValueError(
"The `value` passed into `gr.Gallery` must be a list of images or videos, or list of (media, caption) tuples."
)
output = []
def _save(img):
url = None
caption = None
orig_name = None
mime_type = None
if isinstance(img, (tuple, list)):
img, caption = img
if isinstance(img, np.ndarray):
file = processing_utils.save_img_array_to_cache(
img, cache_dir=self.GRADIO_CACHE, format=self.format
)
file_path = str(utils.abspath(file))
elif isinstance(img, PIL.Image.Image):
format = (
"png" if img.mode == "I;16" else self.format
) # Patch 1: change format based on the inbound dtype
file = processing_utils.save_pil_to_cache(
img, cache_dir=self.GRADIO_CACHE, format=format
)
file_path = str(utils.abspath(file))
elif isinstance(img, str):
mime_type = client_utils.get_mimetype(img)
if img.lower().endswith(".svg"):
svg_content = image_utils.extract_svg_content(img)
orig_name = Path(img).name
url = f"data:image/svg+xml,{quote(svg_content)}"
file_path = None
elif is_http_url_like(img):
url = img
orig_name = Path(urlparse(img).path).name
file_path = img
else:
url = None
orig_name = Path(img).name
file_path = img
elif isinstance(img, Path):
file_path = str(img)
orig_name = img.name
mime_type = client_utils.get_mimetype(file_path)
else:
raise ValueError(f"Cannot process type as image: {type(img)}")
if mime_type is not None and "video" in mime_type:
return GalleryVideo(
video=FileData(
path=file_path, # type: ignore
url=url,
orig_name=orig_name,
mime_type=mime_type,
),
caption=caption,
)
else:
return GalleryImage(
image=ImageData(
path=file_path,
url=url,
orig_name=orig_name,
mime_type=mime_type,
),
caption=caption,
)
if wasm_utils.IS_WASM:
for img in value:
output.append(_save(img))
else:
with ThreadPoolExecutor() as executor:
for o in executor.map(_save, value):
output.append(o)
return GalleryData(root=output)
|