stable-diffusion-webui icon indicating copy to clipboard operation
stable-diffusion-webui copied to clipboard

[Bug]: API Error: /sdapi/v1/img2img returns error as 'str' object has no attribute 'convert'"

Open muruga86 opened this issue 2 years ago • 16 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues and checked the recent builds/commits

What happened?

I followed the steps to run img2img through swagger AP, but face this issue 'str' object has no attribute 'convert'".

{ "init_images": [ "" ], "resize_mode": 0, "denoising_strength": 0.75, "image_cfg_scale": 0, "mask": "", "mask_blur": 4, "inpainting_fill": 0, "inpaint_full_res": true, "inpaint_full_res_padding": 0, "inpainting_mask_invert": 0, "initial_noise_multiplier": 0, "prompt": "cute white dog", "styles": [ "string" ], "seed": -1, "subseed": -1, "subseed_strength": 0, "seed_resize_from_h": -1, "seed_resize_from_w": -1, "batch_size": 1, "n_iter": 1, "steps": 50, "cfg_scale": 7, "width": 512, "height": 512, "restore_faces": false, "tiling": false, "do_not_save_samples": false, "do_not_save_grid": false, "negative_prompt": "ugly", "eta": 0, "s_churn": 0, "s_tmax": 0, "s_tmin": 0, "s_noise": 1, "override_settings": {}, "override_settings_restore_afterwards": true, "script_args": [], "sampler_index": "Euler", "include_init_images": false, "send_images": true, "save_images": false, "alwayson_scripts": {} }

error: { "error": "AttributeError", "detail": "", "body": "", "errors": "'str' object has no attribute 'convert'" }

console: API error: POST: https://9205563b314e0ba6a2.gradio.live/sdapi/v1/img2img {'error': 'AttributeError', 'detail': '', 'body': '', 'errors': "'str' object has no attribute 'convert'"} Traceback (most recent call last): File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 94, in receive return self.receive_nowait() File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 89, in receive_nowait raise WouldBlock anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 78, in call_next message = await recv_stream.receive() File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 114, in receive raise EndOfStream anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/stable-diffusion-webui/modules/api/api.py", line 145, in exception_handling return await call_next(request) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next raise app_exc File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro await self.app(scope, receive_or_disconnect, send_no_error) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 108, in call response = await self.dispatch_func(request, call_next) File "/home/ubuntu/stable-diffusion-webui/modules/api/api.py", line 110, in log_and_time res: Response = await call_next(req) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next raise app_exc File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro await self.app(scope, receive_or_disconnect, send_no_error) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in call await responder(scope, receive, send) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in call await self.app(scope, receive, self.send_with_gzip) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in call raise exc File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in call await self.app(scope, receive, sender) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call raise e File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call await self.app(scope, receive, send) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in call await route.handle(scope, receive, send) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle await self.app(scope, receive, send) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app response = await func(request) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app raw_response = await run_endpoint_function( File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/routing.py", line 165, in run_endpoint_function return await run_in_threadpool(dependant.call, **values) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool return await anyio.to_thread.run_sync(func, *args) File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync return await get_asynclib().run_sync_in_worker_thread( File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread return await future File "/home/ubuntu/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run result = context.run(func, *args) File "/home/ubuntu/stable-diffusion-webui/modules/api/api.py", line 376, in img2imgapi processed = process_images(p) File "/home/ubuntu/stable-diffusion-webui/modules/processing.py", line 503, in process_images res = process_images_inner(p) File "/home/ubuntu/stable-diffusion-webui/modules/processing.py", line 594, in process_images_inner p.init(p.all_prompts, p.all_seeds, p.all_subseeds) File "/home/ubuntu/stable-diffusion-webui/modules/processing.py", line 977, in init image_mask = image_mask.convert('L') AttributeError: 'str' object has no attribute 'convert'

img2img-error500 sd-api-img2img-ip-err2.txt

Steps to reproduce the problem

  1. start websh

ubuntu@ip- Textual inversion embeddings loaded(0): Model loaded in 4.8s (load weights from disk: 0.3s, create model: 0.8s, apply weights to model: 1.2s, apply half(): 0.7s, load VAE: 1.1s, move model to device: 0.6s). Running on local URL: http://127.0.0.1:7860 Running on public URL: https://9205563b314e0ba6a2.gradio.live

This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces Startup time: 15.2s (import torch: 1.0s, import gradio: 0.9s, import ldm: 0.6s, other imports: 0.7s, load scripts: 0.4s, load SD checkpoint: 5.0s, create ui: 0.2s, gradio launch: 6.2s, scripts app_started_callback: 0.1s). 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:07<00:00, 1.44s/it] API error: POST: https://9205563b314e0ba6a2.gradio.live/sdapi/v1/img2img {'error': 'HTTPException', 'detail': 'Invalid encoded image', 'body': '', 'errors': ''} API error: POST: https://9205563b314e0ba6a2.gradio.live/sdapi/v1/img2img {'error': 'HTTPException', 'detail': 'Sampler not found', 'body': '', 'errors': ''} API error: POST: https://9205563b314e0ba6a2.gradio.live/sdapi/v1/img2img {'error': 'AttributeError', 'detail': '', 'body': '', 'errors': "'str' object has no attribute 'convert'"}

  1. Use swagger to issue img2img generation using base64 img

What should have happened?

Should have generated img

Commit where the problem happens

np

What platforms do you use to access the UI ?

No response

What browsers do you use to access the UI ?

No response

Command Line Arguments

attached

List of extensions

none

Console logs

attached

Additional information

No response

muruga86 avatar Apr 21 '23 11:04 muruga86

Followed the suggestion given in https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/9467 added mask field with empty quotes

muruga86 avatar Apr 21 '23 12:04 muruga86

Can some one share correct mask parameter format or sample working config for img2img feature

muruga86 avatar Apr 22 '23 09:04 muruga86

Followed the suggestion given in #9467 added mask field with empty quotes

Delete the variable named "mask". When "mask" exists, it will attempt to encode the masked image and then throw an error.

lazyeo avatar May 26 '23 10:05 lazyeo

removing "mask": "" from payload object fixed it.

ImranBug avatar Jul 23 '23 14:07 ImranBug

Followed the suggestion given in #9467 added mask field with empty quotes

Delete the variable named "mask". When "mask" exists, it will attempt to encode the masked image and then throw an error.

I have a similar problem when doing img2img with a mask for inpaint. I thought that is due to the API has not process the input str (a base64 encoded image for my case), but i found that it actually has decoded the str back to image in api.py line 396 and then updated the variable "mask". This should have passed to the next step in processing.py but seems not successful.

CCJetWing avatar Sep 30 '23 14:09 CCJetWing

Is anybody working on a fix for this?

Removing the mask parameter is only solving the problem for those who don't need the mask, but I have not found any way to pass an actual mask at this time

mgschwan avatar Jan 06 '24 11:01 mgschwan

Same issue guys. Receiving "'str' object has no attribute 'convert'" while trying to pass mask for Img2img API.

It turns out, that if host SD locally and make API request - everything works fine, but when host API SD somewhere, then this error occurs.

Rezzenmann avatar Feb 15 '24 06:02 Rezzenmann

Followed the suggestion given in #9467 added mask field with empty quotes

Delete the variable named "mask". When "mask" exists, it will attempt to encode the masked image and then throw an error.

I have a similar problem when doing img2img with a mask for inpaint. I thought that is due to the API has not process the input str (a base64 encoded image for my case), but i found that it actually has decoded the str back to image in api.py line 396 and then updated the variable "mask". This should have passed to the next step in processing.py but seems not successful.

Guys @CCJetWing, @mgschwan, I was wondering if you had any progress on solving this for yourself? Stuck with this for already 6 days..

Rezzenmann avatar Feb 17 '24 14:02 Rezzenmann

Has this issue been resolved yet? I also have this issue, even if the image is converted to base64, it is still this error

ffhelly avatar Apr 17 '24 08:04 ffhelly

yes, workaround works for me.

On Wed, Apr 17, 2024 at 1:58 PM ffhelly @.***> wrote:

Has this issue been resolved yet?

— Reply to this email directly, view it on GitHub https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/9779#issuecomment-2060694881, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABT2UTXDXAFVDVIE572U7S3Y5YXBTAVCNFSM6AAAAAAXGX76ACVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRQGY4TIOBYGE . You are receiving this because you authored the thread.Message ID: @.***>

muruga86 avatar Apr 17 '24 13:04 muruga86

Followed the suggestion given in #9467 added mask field with empty quotes

Delete the variable named "mask". When "mask" exists, it will attempt to encode the masked image and then throw an error.

I have a similar problem when doing img2img with a mask for inpaint. I thought that is due to the API has not process the input str (a base64 encoded image for my case), but i found that it actually has decoded the str back to image in api.py line 396 and then updated the variable "mask". This should have passed to the next step in processing.py but seems not successful.

Guys @CCJetWing, @mgschwan, I was wondering if you had any progress on solving this for yourself? Stuck with this for already 6 days..

I've moved my whole process tom comfyui because last time I checked it ( which was some February release ) it was stil not working.

mgschwan avatar Apr 17 '24 14:04 mgschwan

yes, workaround works for me. On Wed, Apr 17, 2024 at 1:58 PM ffhelly @.> wrote: Has this issue been resolved yet? — Reply to this email directly, view it on GitHub <#9779 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABT2UTXDXAFVDVIE572U7S3Y5YXBTAVCNFSM6AAAAAAXGX76ACVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRQGY4TIOBYGE . You are receiving this because you authored the thread.Message ID: @.>

If you are not relying on the mask then yes you can remove it and it won't throw an error, but if you need the mask then this won't help

mgschwan avatar Apr 17 '24 14:04 mgschwan

on modules/processing.py self.latent_mask if self.latent_mask is not None else image_mask

on server latent_mask = "" is str so throw this error modify: latent_mask = self.latent_mask if self.latent_mask is not None and self.latent_mask!="" else image_mask it's work

quanwuji avatar Jun 18 '24 02:06 quanwuji

Has anyone had one success with this? I can confirm that providing (what I believe is) a proper base64 image for "mask" still causes the bug: 'str' object has no attribute 'convert'

dubtor avatar Jul 09 '24 13:07 dubtor

I can confirm that @quanwuji 's workaround works.

It seems that maybe different comparisons of type "mask is None" / "mask is not None" fail in situations with empty strings, since an empty string is obviously not a valid mask, yet it evaluates as "not None".

dubtor avatar Jul 09 '24 20:07 dubtor