vale46n1 / immich_duplicate_finder

A Comprehensive Solution for Identifying and Managing Duplicate Photos in Immich
Apache License 2.0
228 stars 19 forks source link

UnidentifiedImageError #3

Closed agross closed 8 months ago

agross commented 8 months ago
Fetch con cache
2024-03-21 22:35:33.351 Uncaught app exception
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 293, in <module>
    main()
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 275, in main
    show_duplicate_photos(assets,limit)
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 193, in show_duplicate_photos
    image1 = np.array(stream_asset(original['id'],immich_server_url))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 210, in wrapper
    return cached_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 239, in __call__
    return self._get_or_create_cached_value(args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 266, in _get_or_create_cached_value
    return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/caching/cache_utils.py", line 322, in _handle_cache_miss
    computed_value = self._info.func(*func_args, **func_kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 90, in stream_asset
    image = Image.open(image_bytes)
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/PIL/Image.py", line 3309, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x10288ccc0>
vale46n1 commented 8 months ago

Try now. I revise the stream_asset code as per:

def stream_asset(asset_id, immich_server_url):

Construct the download URL for the asset

asset_download_url = f"{immich_server_url}/api/download/asset/{asset_id}"
# Stream the asset
response = requests.post(asset_download_url, headers={'Accept': 'application/octet-stream','x-api-key': api_key}, stream=True)
if response.status_code == 200 and 'image/' in response.headers.get('Content-Type', ''):
    image_bytes = BytesIO(response.content)
    try:
        image = Image.open(image_bytes)
        return image
    except UnidentifiedImageError:
        # Log error or save the problematic data for inspection
        print(f"Failed to identify image for asset_id {asset_id}. Content-Type: {response.headers.get('Content-Type')}")
        # Optionally, save or log the problematic bytes
        # with open(f"debug_{asset_id}.bin", "wb") as f_debug:
        #     f_debug.write(response.content)
        return None
else:
    print(f"Failed to fetch image for asset_id {asset_id}. Status code: {response.status_code}, Content-Type: {response.headers.get('Content-Type')}")
    return None

Already commited the code

agross commented 8 months ago
Failed to fetch image for asset_id 2889f083-d930-4206-b761-23c2262225dd. Status code: 200, Content-Type: video/mp4
Failed to fetch image for asset_id 5ee47ed6-7d49-4a0a-8a55-04527ac58cc3. Status code: 200, Content-Type: video/mp4
2024-03-21 22:49:09.038 Uncaught app exception
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 542, in _run_script
    exec(code, module.__dict__)
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 328, in <module>
    main()
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 310, in main
    show_duplicate_photos(assets,limit)
  File "/Users/agross/Downloads/immich_duplicate_finder/app.py", line 232, in show_duplicate_photos
    image_comparison(
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit_image_comparison/__init__.py", line 192, in image_comparison
    img1_pillow = read_image_as_pil(img1)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/streamlit_image_comparison/__init__.py", line 74, in read_image_as_pil
    if image.shape[0] < 5:  # image in CHW
       ~~~~~~~~~~~^^^
IndexError: tuple index out of range
agross commented 8 months ago

I'm AFK now until tomorrow. Thanks and see you later ;-)

vale46n1 commented 8 months ago

Just uploaded the revised version. Let me know

agross commented 8 months ago

Thanks!