mrhan1993 / Fooocus-API

FastAPI powered API for Fooocus
GNU General Public License v3.0
502 stars 131 forks source link

Outpaintng: Some outputs contain visible lines where the crop was extended. #337

Closed alexatnv closed 3 weeks ago

alexatnv commented 1 month ago

Quesion #1: Using real photos of people who are usually cropped around the shoulders, sometimes closer like the head, and outpainting them in all directions to extend the crop. I noticed some visible lines on some of the output images.

import requests import json import base64 import argparse import os

Define the host for Fooocus API

host = "http://127.0.0.1:8888"

def inpaint_outpaint(input_image: bytes, positive_prompt: str, negative_prompt: str, performance_selection: str, guidance_scale: float, inpaint_strength: float, steps: int, base_model_name: str) -> dict: params = { "input_image": base64.b64encode(input_image).decode('utf-8'), "input_mask": None, "outpaint_selections": ["Left", "Right", "Bottom", "Top"], "outpaint_distance_left": 750, # Define the amount to outpaint to the left "outpaint_distance_right": 750, # Define the amount to outpaint to the right "outpaint_distance_top": 150, # Define the amount to outpaint to the top "outpaint_distance_bottom": 750, # Define the amount to outpaint to the bottom "positive_prompt": positive_prompt, "negative_prompt": negative_prompt, "performance_selection": performance_selection, "style_selections": ["Ads Corporate", "Misc Minimalist"], "async_process": False, "base_model_name": base_model_name, "advanced_params": { "guidance_scale": guidance_scale, "inpaint_strength": inpaint_strength, "overwrite_step": steps # Define the number of steps } } response = requests.post( url=f"{host}/v2/generation/image-inpaint-outpaint", data=json.dumps(params), headers={"Content-Type": "application/json"} ) return response.json()

def main(image_path):

Load the image

with open(image_path, "rb") as img_file:
    image_data = img_file.read()

# Define the parameters
positive_prompt = "A person looking in camera with body visible. Match close as possible the original clothing, (professional clothing:1.5), maintain original essence, professional pose"
negative_prompt = "blurry, unnatural, deformed, distracting background, hands above shoulders, objects, (obstructed view of body:2.5), objects in front of shoulders, cropped, (picture frame:1.5), (skin exposure:2.5), uniforms, (people in background:2.5), body cut-off, body not visible, raised hands, raised arms, guns, weapons, logos, logo on clothing, offensive material"
performance_selection = "Quality"
guidance_scale = 20.0
inpaint_strength = 1.0
steps = 80  # Define the number of steps
base_model_name = "Juggernaut_X_RunDiffusion.safetensors"  # Define your base model name

# Perform outpainting using v2 endpoint
result = inpaint_outpaint(
    input_image=image_data,
    positive_prompt=positive_prompt,
    negative_prompt=negative_prompt,
    performance_selection=performance_selection,
    guidance_scale=guidance_scale,
    inpaint_strength=inpaint_strength,
    steps=steps,
    base_model_name=base_model_name
)
print("Outpaint Result:", json.dumps(result, indent=4, ensure_ascii=False))

if name == "main": parser = argparse.ArgumentParser(description="Process an image for outpainting") parser.add_argument('--image', required=True, help='Path to the image to be processed') args = parser.parse_args()

if not os.path.exists(args.image):
    print(f"Image not found: {args.image}")
else:
    main(args.image)

How do i remove these lines? Right now it outpaints all directions at once, should I do one at a time?

Question #2: In my code above I have guidance strength and inpaint strength, im not really sure what each one does, but I noticed in my console some other parameters came up that I didn't see in the API - things like CFG,ControlNet Softness, Adapative CFG. Are these also changeable values?

[2024-05-23 08:32:01] INFO [Task Queue] Waiting for task finished, job_id=35ad05b3-8638-4694-84a6-90c5a3ba4a15 [2024-05-23 08:32:01] INFO [Task Queue] Task queue start task, job_id=35ad05b3-8638-4694-84a6-90c5a3ba4a15 [2024-05-23 08:32:01] INFO [Parameters] Adaptive CFG = 7.0 [2024-05-23 08:32:01] INFO [Parameters] Sharpness = 2.0 [2024-05-23 08:32:01] INFO [Parameters] ControlNet Softness = 0.25 [2024-05-23 08:32:01] INFO [Parameters] ADM Scale = 1.5 : 0.8 : 0.3 [2024-05-23 08:32:01] INFO [Parameters] CFG = 4.0 [2024-05-23 08:32:01] INFO [Parameters] Seed = 1014433614765127678 [2024-05-23 08:32:01] INFO [Fooocus] Downloading upscale models ... [2024-05-23 08:32:01] INFO [Fooocus] Downloading inpainter ...

mrhan1993 commented 1 month ago

The first question:

I'm sorry, I don't know what to do, but it works better when the default parameters are fully used, and if outpaint_distance is specified, the dividing line looks pretty obvious. The second question: You can view the complete changeable parameters at http://127.0.0.1:8888/docs#/GenerateV2/img_inpaint_or_outpaint_v2_generation_image_inpaint_outpaint_post

davefojtik commented 1 month ago

For question no.1 - Sometimes using the inpainting version of the model (if there is one) helps. But the lines are a classic problem with outpainting and always were in every UI. The usual workflow is to do outpaint and then img2img where the visible seams or blurring is fixed. You can then combine the img2img result with the original photo for the best outcome.

alexatnv commented 1 month ago

Thanks for the answers everyone. Ill try an inpainting model. where do i place it? I placed juggernautXL_v9rdphoto2Inpaint.safetensors and ran it but seems to return an error with every "inpaint" versions of models

davefojtik commented 1 month ago

Sorry for the late reply and the wrong info. It really seems like Fooocus does not accept these inpaint models as other UIs do. Instead, you can try to play with the inpaint_engine in advanced params. It can have quite different results for outpainting depending on the used model. Other than that the mentioned workflow is probably still the best bet

alexatnv commented 3 weeks ago

Thanks @davefojtik !

Curious how many 'versions' of the inpaint engine there are? 2.6 is the default. Would be great to know the range of versions that are available. And also - does high version necessarily mean it's 'newer' and therefore 'better'?

davefojtik commented 3 weeks ago
['None', 'v1', 'v2.5', 'v2.6']

It indeed means 'newer' but not necessarily 'better'. From my observations, it depends on the used model and other things. Worth experimenting a bit and finding the best one for your use case.

alexatnv commented 3 weeks ago

you are the best @davefojtik thank you for your time