MackinationsAi / sd-webui-udav2

A1111 Extension integration for Upgraded-Depth-Anything-V2 - UDAV2
https://github.com/MackinationsAi/Upgraded-Depth-Anything-V2
Apache License 2.0
39 stars 2 forks source link

Compute depth for Single image not working, 'numpy.ndarray' object has no attribute 'seek' #8

Closed nitinmukesh closed 1 month ago

nitinmukesh commented 1 month ago

So I installed in Forge from extensions tab and make some changes mentioned in other thread to make it work. It is now appearing in Forge.

When I select an image and click on Compute depth for Single image.

To create a public link, set `share=True` in `launch()`.
Startup time: 30.0s (prepare environment: 6.3s, import torch: 11.1s, initialize shared: 0.3s, other imports: 0.8s, load scripts: 3.7s, create ui: 5.6s, gradio launch: 2.2s).
Environment vars changed: {'stream': False, 'inference_memory': 1024.0, 'pin_shared_memory': False}
[GPU Setting] You will use 87.49% GPU memory (7163.00 MB) to load weights, and use 12.51% GPU memory (1024.00 MB) to do matrix computation.
Traceback (most recent call last):
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\PIL\Image.py", line 3240, in open
    fp.seek(0)
AttributeError: 'numpy.ndarray' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\queueing.py", line 536, in process_events
    response = await route_utils.call_process_api(
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\route_utils.py", line 285, in call_process_api
    output = await app.get_blocks().process_api(
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\blocks.py", line 1923, in process_api
    result = await self.call_function(
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\blocks.py", line 1508, in call_function
    prediction = await anyio.to_thread.run_sync(  # type: ignore
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\anyio\to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\anyio\_backends\_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\utils.py", line 818, in wrapper
    response = f(*args, **kwargs)
  File "C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-webui-udav2\scripts\upgraded_depth_anything_v2.py", line 271, in on_submit_single
    image = np.array(Image.open(image_path))
  File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\PIL\Image.py", line 3242, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'numpy.ndarray' object has no attribute 'read'
(venv) C:\usable\webui_forge_cu121_torch231\webui>pip list
Package                   Version
------------------------- ------------------
absl-py                   2.1.0
accelerate                0.21.0
aenum                     3.1.15
aiofiles                  23.2.1
aiohappyeyeballs          2.4.0
aiohttp                   3.10.5
aiosignal                 1.3.1
albucore                  0.0.14
albumentations            1.4.14
annotated-types           0.7.0
antlr4-python3-runtime    4.9.3
anyio                     3.7.1
async-timeout             4.0.3
attrs                     24.2.0
bitsandbytes              0.43.3
blendmodes                2022
certifi                   2024.8.30
cffi                      1.17.1
chardet                   5.2.0
charset-normalizer        3.3.2
clean-fid                 0.1.35
click                     8.1.7
clip                      1.0
colorama                  0.4.6
coloredlogs               15.0.1
colorlog                  6.8.2
contourpy                 1.3.0
cssselect2                0.7.0
cycler                    0.12.1
Cython                    3.0.11
deprecation               2.1.0
depth_anything            2024.1.22.0
depth_anything_v2         2024.7.1.0
diffusers                 0.29.2
diskcache                 5.6.3
dlib                      19.24.6
easydict                  1.13
einops                    0.4.1
eval_type_backport        0.2.0
exceptiongroup            1.2.2
facexlib                  0.3.0
fastapi                   0.104.1
ffmpy                     0.4.0
filelock                  3.16.0
filterpy                  1.4.5
flatbuffers               24.3.25
fonttools                 4.53.1
frozenlist                1.4.1
fsspec                    2024.9.0
ftfy                      6.2.3
fvcore                    0.1.5.post20221221
gitdb                     4.0.11
GitPython                 3.1.32
gradio                    4.40.0
gradio_client             1.2.0
gradio_imageslider        0.0.20
gradio_rangeslider        0.0.6
h11                       0.12.0
handrefinerportable       2024.2.12.0
httpcore                  0.15.0
httpx                     0.24.1
huggingface-hub           0.24.6
humanfriendly             10.0
idna                      3.8
imageio                   2.35.1
importlib_metadata        8.4.0
importlib_resources       6.4.5
inflection                0.5.1
insightface               0.7.3
intel-openmp              2021.4.0
iopath                    0.1.9
jax                       0.4.31
jaxlib                    0.4.31
Jinja2                    3.1.4
joblib                    1.4.2
jsonmerge                 1.8.0
jsonschema                4.23.0
jsonschema-specifications 2023.12.1
kiwisolver                1.4.7
kornia                    0.6.7
lark                      1.1.2
lazy_loader               0.4
lightning-utilities       0.11.7
llvmlite                  0.43.0
loadimg                   0.1.2
lxml                      5.3.0
manifold3d                2.5.1
mapbox_earcut             1.0.2
markdown-it-py            3.0.0
MarkupSafe                2.1.5
matplotlib                3.9.2
mdurl                     0.1.2
mediapipe                 0.10.14
mkl                       2021.4.0
ml-dtypes                 0.4.0
mpmath                    1.3.0
multidict                 6.1.0
networkx                  3.3
numba                     0.60.0
numpy                     1.26.2
omegaconf                 2.2.3
onnx                      1.16.2
onnxruntime               1.19.2
open-clip-torch           2.20.0
opencv-contrib-python     4.10.0.84
opencv-python             4.10.0.84
opencv-python-headless    4.10.0.84
opt-einsum                3.3.0
orjson                    3.10.7
packaging                 24.1
pandas                    2.2.2
peft                      0.12.0
piexif                    1.1.3
Pillow                    9.5.0
pillow-avif-plugin        1.4.3
pip                       24.2
platformdirs              4.3.4
pooch                     1.8.2
portalocker               2.10.1
prettytable               3.11.0
protobuf                  3.20.0
psutil                    5.9.5
pycollada                 0.8
pycparser                 2.22
pydantic                  2.8.2
pydantic_core             2.20.1
pydub                     0.25.1
Pygments                  2.18.0
PyMatting                 1.1.12
pyparsing                 3.1.4
pyreadline3               3.4.1
python-dateutil           2.9.0.post0
python-multipart          0.0.9
pytorch-lightning         1.9.4
pytz                      2024.1
PyWavelets                1.7.0
pywin32                   306
PyYAML                    6.0.2
referencing               0.35.1
regex                     2024.7.24
rembg                     2.0.59
reportlab                 4.2.2
requests                  2.32.3
resize-right              0.0.2
rich                      13.8.0
rpds-py                   0.20.0
Rtree                     1.3.0
ruff                      0.6.4
safetensors               0.4.2
scikit-image              0.21.0
scikit-learn              1.5.1
scipy                     1.14.1
semantic-version          2.10.0
sentencepiece             0.2.0
setuptools                69.5.1
shapely                   2.0.6
shellingham               1.5.4
six                       1.16.0
smmap                     5.0.1
sniffio                   1.3.1
sounddevice               0.5.0
spandrel                  0.3.4
spandrel_extra_arches     0.1.1
starlette                 0.27.0
svg.path                  6.3
svglib                    1.5.1
sympy                     1.13.2
tabulate                  0.9.0
tbb                       2021.13.1
termcolor                 2.4.0
threadpoolctl             3.5.0
tifffile                  2024.8.30
timm                      1.0.9
tinycss2                  1.3.0
tokenizers                0.19.1
tomesd                    0.1.3
tomlkit                   0.12.0
torch                     2.3.1+cu121
torchdiffeq               0.2.3
torchmetrics              1.4.1
torchsde                  0.2.6
torchvision               0.18.1+cu121
tqdm                      4.66.1
trampoline                0.1.2
transformers              4.44.0
trimesh                   4.4.9
triton                    2.1.0
typer                     0.12.5
typing_extensions         4.12.2
tzdata                    2024.1
urllib3                   2.2.2
uvicorn                   0.30.6
vhacdx                    0.0.8.post1
wcwidth                   0.2.13
webencodings              0.5.1
websockets                12.0
xatlas                    0.0.9
xxhash                    3.5.0
yacs                      0.1.8
yarl                      1.11.1
zipp                      3.20.1
nitinmukesh commented 1 month ago

If I use sd-forge-udav2 it doesn't load in WebUI

2024-09-18 00:05:08,106 - ControlNet - INFO - ControlNet UI callback registered.
C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py:246: GradioDeprecationWarning: unexpected argument for Image: display_fn
  input_image = gr.Image(label="Input Image", type='numpy', elem_id='img-display-input', height=794, display_fn=lambda x: x)
C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py:247: GradioDeprecationWarning: unexpected argument for Image: display_fn
  depth_image_slider = gr.Image(label="Colourized Depth Map View", elem_id='img-display-output', height=794, display_fn=lambda x: x)
C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py:248: GradioDeprecationWarning: unexpected argument for Image: display_fn
  grey_depth_file_single = gr.Image(label="Greyscale Depth Map View", elem_id='img-display-output', height=794, display_fn=lambda x: x)
C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py:254: GradioDeprecationWarning: unexpected argument for Button: height
  submit_single = gr.Button(value="Compute Depth for Single Image", variant="primary", height=26)
*** Error executing callback ui_tabs_callback for C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py
    Traceback (most recent call last):
      File "C:\usable\webui_forge_cu121_torch231\webui\modules\script_callbacks.py", line 283, in ui_tabs_callback
        res += c.callback() or []
      File "C:\usable\webui_forge_cu121_torch231\webui\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py", line 268, in on_ui_tabs
        input_images = gr.Files(label="Upload Images", type="file", elem_id="img-display-input")
      File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\component_meta.py", line 163, in wrapper
        return fn(self, **kwargs)
      File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\templates.py", line 513, in __init__
        super().__init__(
      File "C:\usable\webui_forge_cu121_torch231\webui\modules\gradio_extensions.py", line 147, in __repaired_init__
        original(self, *args, **fixed_kwargs)
      File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\component_meta.py", line 163, in wrapper
        return fn(self, **kwargs)
      File "C:\usable\webui_forge_cu121_torch231\webui\venv\lib\site-packages\gradio\components\file.py", line 98, in __init__
        raise ValueError(
    ValueError: Invalid value for parameter `type`: file. Please choose from one of: ['filepath', 'binary']
MackinationsAi commented 1 month ago

It works in the older version of forge before the major update, I will look into this error & get back to you.

nitinmukesh commented 1 month ago

Thank you @MackinationsAi

So many breaking changes are introduced every few updates, the extensions stop working. :(

MackinationsAi commented 1 month ago

@nitinmukesh - So I did a fresh install of the latest version of forge & then installed sd-forge-udav2_v0.0.5. I had no issues w/ single.img, batch.img, single.vid, batch.vid or today's.gallery tab. Everything worked as it should.

UDAV2_functional_in_forge

That being said, I took a look at the errors you encountered & made a patch I think might help you get UDAV2 back up & running. Hopefully this works, cheers!

Open 'A:\your_path_to\stable-diffusion-webui-forge\extensions\sd-forge-udav2\scripts\upgraded_depth_anything_v2.py' & replace lines 276 to 309 w/ the following code should solve the issues you are facing.

        with gr.Tab("Single Image Processing"):
            with gr.Row():
                model_encoder_image_single = gr.Dropdown(label="Select Model Encoder:", choices=encoders, value='vitl')            
            with gr.Row():
                input_image = gr.Image(label="Input Image", type='filepath', elem_id='img-display-input', height=794) # Changed type from 'Numpy' to 'filepath' & removed deprecated display_fn from gr.Image 
                depth_image_slider = gr.Image(label="Colourized Depth Map View", elem_id='img-display-output', height=794) # Removed deprecated display_fn from gr.Image
                grey_depth_file_single = gr.Image(label="Greyscale Depth Map View", elem_id='img-display-output', height=794) # Removed deprecated display_fn from gr.Image
            with gr.Row():
                with gr.Column(scale=8):
                    colour_map_dropdown_single = gr.Dropdown(label="Select Colour Map Method:", choices=colour_map_methods, value='Spectral')            
                with gr.Column(scale=1):
                    colour_map_selection_single = gr.Dropdown(label="Colour Map Method Selection:", choices=colour_map_selections, value='Default')     
            submit_single = gr.Button(value="Compute Depth for Single Image", variant="primary") # Removed deprecated height argument from gr.Button size 'height=26'

            def on_submit_single(image_path, colour_map_method, encoder, selection):
                if isinstance(image_path, str):  # If it's a file path, open it
                    image = np.array(Image.open(image_path))
                else:
                    image = image_path  # If it's already a NumPy array, use it directly
                colourized_filenames, grey_depth_filename = process_image(image, colour_map_method, encoder, selection)
                first_colourized_image = Image.open(colourized_filenames[0])
                first_colourized_image_np = np.array(first_colourized_image)
                return first_colourized_image_np, grey_depth_filename

            colour_map_selection_single.change(fn=lambda selection: gr.update(choices=get_colour_map_methods(selection)), inputs=colour_map_selection_single, outputs=colour_map_dropdown_single)
            submit_single.click(on_submit_single, inputs=[input_image, colour_map_dropdown_single, model_encoder_image_single, colour_map_selection_single], outputs=[depth_image_slider, grey_depth_file_single])

        with gr.Tab("Batch Image Processing"):
            with gr.Row():
                model_encoder_image_batch = gr.Dropdown(label="Select Model Encoder:", choices=encoders, value='vitl')            
            with gr.Row():
                input_images = gr.Files(label="Upload Images", type="filepath", elem_id="img-display-input") # Changed type from 'file' to 'filepath'
nitinmukesh commented 1 month ago

@MackinationsAi

Thank you very much. I will install it now.