Mikubill / sd-webui-controlnet

WebUI extension for ControlNet
GNU General Public License v3.0
16.83k stars 1.94k forks source link

[Bug]: When sending request via API, UnidentifiedImageError is occurred. #2246

Closed waura closed 10 months ago

waura commented 10 months ago

Is there an existing issue for this?

What happened?

I use version 05ef0b1c.

I send POST requests to /sdapi/v1/txt2img . The request body is as follow.

{
        "prompt": "...",
        "negative_prompt": "...",
        "steps": 30,
        "batch_size": 2,
        "n_iter": 1,
        "seed": -1,
        "sampler_name": "DPM++ 2M Karras",
        "cfg_scale": 6,
        "width": 512,
        "height": 768,
        "enable_hr": true,
        "hr_scale": 2,
        "hr_upscaler": "R-ESRGAN 4x+",
        "denoising_strength": 0.55,
        "alwayson_scripts": {
            "controlnet": {
                "args": [
                    {
                        "input_image": "<Base64 encoded text generated by the following python code>",

                        "module": "none",
                        "model": "control_v11f1p_sd15_depth [cfd03158]",
                        "weight": 1,
                        "control_mode": "My prompt is more important"
                    }
                ]
            }
        }
    }

input_image file is as follow.

$ file depth.png 
depth.png: PNG image data, 512 x 768, 8-bit/color RGBA, non-interlaced

I generate the Base64 text as follow.

import cv2

...

def load_image_as_base64():
    # Read Image in RGB order
    img = cv2.imread('./depth.png')

    # Encode into PNG and send to ControlNet
    retval, bytes = cv2.imencode('.png', img)
    return base64.b64encode(bytes).decode('utf-8')

After accept the POST request, the webui output the following logs.

PIL.UnidentifiedImageError: cannot identify image file

Full stack trace is the following section.

Steps to reproduce the problem

please see the above description.

What should have happened?

No error occurs and the specified input_image is used.

Commit where the problem happens

webui: v1.6.0 controlnet: 05ef0b1c.

What browsers do you use to access the UI ?

No response

Command Line Arguments

No

List of enabled extensions

Extension   URL Branch  Version Date    Update
Stable-Diffusion-Webui-Civitai-Helper   https://github.com/butaixianran/Stable-Diffusion-Webui-Civitai-Helper.git   main    [dc76c0c6](https://github.com/butaixianran/Stable-Diffusion-Webui-Civitai-Helper/commit/dc76c0c68880338185adca16e5fc4ea522e95e4d)   2023-09-29 14:52:56 unknown
a1111-sd-webui-locon    https://github.com/KohakuBlueleaf/a1111-sd-webui-locon  main    [8e0ebd76](https://github.com/KohakuBlueleaf/a1111-sd-webui-locon/commit/8e0ebd7647249ce2609ea62cdd76863d8d1fbd41)  2023-03-30 15:20:06 unknown
openpose-editor https://github.com/fkunn1326/openpose-editor.git    master  [52ac8910](https://github.com/fkunn1326/openpose-editor/commit/52ac89108c73a80ce66592c01528eb442ba022cb)    2023-03-26 08:53:26 unknown
sd-dynamic-prompts  https://github.com/adieyal/sd-dynamic-prompts   main    [1596f556](https://github.com/adieyal/sd-dynamic-prompts/commit/1596f556e23be4865206a0d3c1c59451a17d506a)   2023-03-06 04:49:07 unknown
sd-webui-additional-networks    https://github.com/kohya-ss/sd-webui-additional-networks    main    [d2758b6c](https://github.com/kohya-ss/sd-webui-additional-networks/commit/d2758b6c8e2e8e956865a87b31fd74d3d7c010cb)    2023-03-12 10:58:50 unknown
sd-webui-controlnet https://github.com/Mikubill/sd-webui-controlnet main    [05ef0b1c](https://github.com/Mikubill/sd-webui-controlnet/commit/05ef0b1cd1374cf285dd8d5ccd7db9997549893c) 2023-11-07 06:05:28 unknown
sd-webui-depth-lib  https://github.com/jexom/sd-webui-depth-lib.git main    [28a22387](https://github.com/jexom/sd-webui-depth-lib/commit/28a223873dd355ef725b77a300319d0ccb843e25) 2023-03-06 21:02:28 unknown
sd-webui-llul   https://github.com/hnmr293/sd-webui-llul    master  [28972a8d](https://github.com/hnmr293/sd-webui-llul/commit/28972a8d002a93a60e6da4fea8595d258361c46b)    2023-03-09 11:32:49 unknown
sd-webui-regional-prompter  https://github.com/hako-mikan/sd-webui-regional-prompter.git    main    [89d45b03](https://github.com/hako-mikan/sd-webui-regional-prompter/commit/89d45b03b9fae6c626f4c5a49fb5f0eb4624137e)    2023-10-26 08:50:53 unknown
sd-webui-supermerger    https://github.com/hako-mikan/sd-webui-supermerger  main    [4c6a3650](https://github.com/hako-mikan/sd-webui-supermerger/commit/4c6a3650f44bf741928c4101641aad7860195a97)  2023-03-20 11:41:02 unknown
sdweb-merge-block-weighted-gui  https://github.com/bbc-mc/sdweb-merge-block-weighted-gui    master  [8a62a753](https://github.com/bbc-mc/sdweb-merge-block-weighted-gui/commit/8a62a753e791a75273863dd04958753f0df7532f)    2023-01-19 21:31:06 unknown
stable-diffusion-webui-images-browser   https://github.com/AlUlkesh/stable-diffusion-webui-images-browser.git   main    [9f95e6d4](https://github.com/AlUlkesh/stable-diffusion-webui-images-browser/commit/9f95e6d4812ca7acb3ec56cbf45fbc85aef0236d)   2023-09-11 12:55:51 unknown
stable-diffusion-webui-model-toolkit    https://github.com/arenatemp/stable-diffusion-webui-model-toolkit   master  [8d8f073f](https://github.com/arenatemp/stable-diffusion-webui-model-toolkit/commit/8d8f073f45bb45c22bf1ebc5eb4356f59bb46189)   2023-02-17 20:39:41 unknown
LDSR    [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
Lora    [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
ScuNET  [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
SwinIR  [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
canvas-zoom-and-pan [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
extra-options-section   [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
mobile  [built-in](http://192.168.1.68:7860/?__theme=dark)  None            
prompt-bracket-checker  [built-in](http://192.168.1.68:7860/?__theme=dark)  None

Console logs

Nov 10 08:01:46 ubuntu-gpu bash[10008]: 2023-11-10 08:01:46,692 - ControlNet - INFO - Loading model from cache: control_v11f1p_sd15_depth [cfd03158]
Nov 10 08:01:46 ubuntu-gpu bash[10008]: *** Error running process: /home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py
Nov 10 08:01:46 ubuntu-gpu bash[10008]:     Traceback (most recent call last):
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/modules/api/api.py", line 98, in decode_base64_to_image
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         image = Image.open(BytesIO(base64.b64decode(encoding)))
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/venv/lib/python3.10/site-packages/PIL/Image.py", line 3305, in open
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         raise UnidentifiedImageError(msg)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:     PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7feb38a568e0>
Nov 10 08:01:46 ubuntu-gpu bash[10008]:     The above exception was the direct cause of the following exception:
Nov 10 08:01:46 ubuntu-gpu bash[10008]:     Traceback (most recent call last):
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/modules/scripts.py", line 619, in process
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         script.process(p, *script_args)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py", line 977, in process
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         self.controlnet_hack(p)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py", line 966, in controlnet_hack
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         self.controlnet_main_entry(p)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py", line 696, in controlnet_main_entry
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         input_image, image_from_a1111 = Script.choose_input_image(p, unit, idx)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py", line 560, in choose_input_image
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         image = image_dict_from_any(unit.image)
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/scripts/controlnet.py", line 105, in image_dict_from_any
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         image['image'] = external_code.to_base64_nparray(image['image'])
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/extensions/sd-webui-controlnet/internal_controlnet/external_code.py", line 195, in to_base64_nparray
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         return np.array(api.decode_base64_to_image(encoding)).astype('uint8')
Nov 10 08:01:46 ubuntu-gpu bash[10008]:       File "/home/waura/stable-diffusion-webui/modules/api/api.py", line 101, in decode_base64_to_image
Nov 10 08:01:46 ubuntu-gpu bash[10008]:         raise HTTPException(status_code=500, detail="Invalid encoded image") from e
Nov 10 08:01:46 ubuntu-gpu bash[10008]:     fastapi.exceptions.HTTPException


### Additional information

The issues output same error message.
https://github.com/Mikubill/sd-webui-controlnet/issues/1058
https://github.com/Mikubill/sd-webui-controlnet/issues/1001
https://github.com/Mikubill/sd-webui-controlnet/issues/950
waura commented 10 months ago

Sorry, this issue was caused by my code.

phucbienvan commented 4 months ago

hello, i also got the same error, what is your problem