lllyasviel / Fooocus

Focus on prompting and generating
GNU General Public License v3.0
41.02k stars 5.76k forks source link

Using FaceSwap via API #1407

Closed Lexi07 closed 7 months ago

Lexi07 commented 10 months ago

I'm trying to use the FaceSwap capability through the API, has anyone been able to do this? If so, do you mind posting a snippet? This is what I have so far:

    client = Client("http://127.0.0.1:7866/", serialize=False)
    result = client.predict(
    "None", # str in 'parameter_10' Textbox component
    "", # str in 'Negative Prompt' Textbox component
    ["Fooocus V2", "Fooocus Enhance", "Fooocus Sharp", "Fooocus Photograph"],   # List[str] in 'Selected Styles' Checkboxgroup component
    "Speed",    # str in 'Performance' Radio component
    "1152×896 <span style='color: grey;'> | 1:2</span>",    # str in 'Aspect Ratios' Radio component
    1,  # int | float (numeric value between 1 and 32)in 'Image Number' Slider component
    "123",  # str in 'Seed' Textbox component
    0,  # int | float (numeric value between 0.0 and 30.0)in 'Image Sharpness' Slider component
    1,  # int | float (numeric value between 1.0 and 30.0)in 'Guidance Scale' Slider component
    "juggernautXL_version6Rundiffusion.safetensors",    # str (Option from: ['juggernautXL_version6Rundiffusion.safetensors', 'realisticStockPhoto_v10.safetensors', 'bluePencilXL_v050.safetensors', 'DreamShaper_8_pruned.safetensors'])in 'Base Model (SDXL only)' Dropdown component
    "None", # str (Option from: ['None', 'juggernautXL_version6Rundiffusion.safetensors', 'realisticStockPhoto_v10.safetensors', 'bluePencilXL_v050.safetensors', 'DreamShaper_8_pruned.safetensors'])in 'Refiner (SDXL or SD 1.5)' Dropdown component
    0.1,    # int | float (numeric value between 0.1 and 1.0)in 'Refiner Switch At' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 1' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 2' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 3' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 4' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    "None", # str (Option from: ['None', 'sd_xl_offset_example-lora_1.0.safetensors', 'SDXL_FILM_PHOTOGRAPHY_STYLE_BetaV0.4.safetensors', 'sdxl_lcm_lora.safetensors'])in 'LoRA 5' Dropdown component
    -2, # int | float (numeric value between -2 and 2)in 'Weight' Slider component
    True,   # bool in 'Input Image' Checkbox component
    "", # str in 'parameter_73' Textbox component
    "Disabled", # str in 'Upscale or Variation:' Radio component
        "data:/Users/xxx/Desktop/Projects/ImageGen/dataset/Image_4.jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",    # str (filepath or URL to image)in 'Image' Image component                                                                            ",    # str (filepath or URL to image)in 'Drag above image to here' Image component
        ["Left"],   # List[str] in 'Outpaint Direction' Checkboxgroup component
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
        "", # str (filepath or URL to image)in 'Image' Image component
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
        0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
        0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
        "ImagePrompt",  # str in 'Type' Radio component
        "data:/Users/xxxx/Desktop/Projects/ImageGen/dataset/Image_4.jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component                                                                            ",    # str (filepath or URL to image)in 'Image' Image component
        1,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
        0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
        "ImagePrompt",  # str in 'Type' Radio component
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
        0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
        0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
        "ImagePrompt",  # str in 'Type' Radio component
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==",   # str (filepath or URL to image)in 'Image' Image component
        0,  # int | float (numeric value between 0.0 and 1.0)in 'Stop At' Slider component
        0,  # int | float (numeric value between 0.0 and 2.0)in 'Weight' Slider component
        "ImagePrompt",  # str in 'Type' Radio component
    fn_index=29
    )
    print(result)
usliders commented 9 months ago

это тайна видимо, хотят нас на стороннюю ветку увести с api. Я так же столкнулся с этой проблемой не принимает ни кодированный файл ни не кодированный, ни путь!!!, и так и сяк пробовал serialize=True ни с serialize=False

photo_filename = os.path.join(unique_folder, rf"{str(uuid.uuid4())}.jpg")

# Используем BytesIO для записи файла
file_data = await bot.download_file(file_path)
file_bytes = BytesIO(file_data.read())

with open(photo_filename, 'wb') as photo_file:
    photo_file.write(file_bytes.getvalue()) 

# Прочитайте изображение из файла и закодируйте его в base64
with open(photo_filename, "rb") as image_file:
    original_image_data = image_file.read()
    encoded_image = base64.b64encode(original_image_data).decode("utf-8")

# Генерация уникального имени файла
new_filename = str(uuid.uuid4()) + "_" + os.path.basename(photo_filename)
new_filepath = os.path.join(unique_folder, new_filename)

# Сохранение файла с новым именем
with open(new_filepath, 'wb') as new_file:
    new_file.write(base64.b64decode(encoded_image))

... client = Client("http://127.0.0.1:7865/", serialize=True) result = await client.predict( ... f"data:image/jpeg;base64,{new_file}", # serialize=False or new_file, # serialize=True ... ########### File "base64.py", line 87, in b64decode binascii.Error: Incorrect padding or File "d:\Deep_Learning_School\Proekts\Fooocus_win64_2-1-831\python_embeded\lib\site-packages\PIL\Image.py", line 3147, in open raise UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000002148CD7E2F0>

help!!!!!!!!!!!!!!!!!!!!! Bugs

usliders commented 9 months ago

ок, получилось

client = Client("http://127.0.0.1:7865/", serialize=False)

... "data:image/jpeg;base64," + encoded_image, # str (filepath or URL to image) in 'Drag above image to here' Image component ...

проблема была в том что я указывал пустые значения в кавычках "", # str (filepath or URL to image) in 'Image' Image component так не работает нужно везде указывать где не загружаем картинки, но заполняем код: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==", # str (filepath or URL to image) in 'Image' Image component

"data:image/jpeg;base64," + encoded_image, # str (filepath or URL to image) in 'Drag above image to here' Image component ["Left"], # List[str] in 'Outpaint Direction' Checkboxgroup component "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==", # str (filepath or URL to image) in 'Drag above image to here' Image component chosen_style_description, # str in 'Inpaint Additional Prompt' Textbox component "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==", # str (filepath or URL to image) in 'Image' Image component 0, # int | float (numeric value between 0.0 and 1.0) in 'Stop At' Slider component 0, # int | float (numeric value between 0.0 and 2.0) in 'Weight' Slider component "ImagePrompt", # str in 'Type' Radio component "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==", # str (filepath or URL to image) in 'Image' Image component 0, # int | float (numeric value between 0.0 and 1.0) in 'Stop At' Slider component 0, # int | float (numeric value between 0.0 and 2.0) in 'Weight' Slider component

mashb1t commented 7 months ago

@Lexi07 is this issue still relevant?