Spaces:
Runtime error
Runtime error
File size: 7,955 Bytes
5fbd25d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
"""
Examples codes for Fooocus API
"""
import json
import os
import base64
import requests
class Config:
"""
Config
Attributes:
fooocus_host (str): Fooocus API host
text2img_ip (str): Text to Image with IP
img_upscale (str): Upscale or Vary
inpaint_outpaint (str): Inpaint or Outpaint
img_prompt (str): Image Prompt
"""
fooocus_host = 'http://127.0.0.1:8888'
text2img_ip = '/v2/generation/text-to-image-with-ip'
img_upscale = '/v2/generation/image-upscale-vary'
inpaint_outpaint = '/v2/generation/image-inpaint-outpaint'
img_prompt = '/v2/generation/image-prompt'
def read_image(image_name: str) -> str:
"""
Read image from file
Args:
image_name (str): Image file name
Returns:
str: Image base64
"""
path = os.path.join('imgs', image_name)
with open(path, "rb") as f:
image = f.read()
f.close()
return base64.b64encode(image).decode('utf-8')
class ImageList:
"""
Image List
"""
bear = read_image('bear.jpg')
image_prompt_0 = read_image('image_prompt-0.jpg')
image_prompt_1 = read_image('image_prompt-1.png')
image_prompt_2 = read_image('image_prompt-2.png')
image_prompt_3 = read_image('image_prompt-3.png')
inpaint_source = read_image('inpaint_source.jpg')
inpaint_mask = read_image('inpaint_mask.png')
source_face_f = read_image('source_face_female.png')
source_face_m = read_image('source_face_man.png')
target_face = read_image('target_face.png')
def upscale_vary(params: dict) -> dict:
"""
Upscale or Vary
Args:
params (dict): Params
Returns:
dict: Response
"""
data = json.dumps(params)
response = requests.post(
url=f"{Config.fooocus_host}{Config.img_upscale}",
data=data,
timeout=300)
return response.json()
def inpaint_outpaint(params: dict = None) -> dict:
"""
Inpaint or Outpaint
Args:
params (dict): Params
Returns:
dict: Response
"""
data = json.dumps(params)
response = requests.post(
url=f"{Config.fooocus_host}{Config.inpaint_outpaint}",
data=data,
timeout=300)
return response.json()
def image_prompt(params: dict) -> dict:
"""
Image Prompt
Args:
params (dict): Params
Returns:
dict: Response
"""
data = json.dumps(params)
response = requests.post(
url=f"{Config.fooocus_host}{Config.img_prompt}",
data=data,
timeout=300)
return response.json()
def text2image_image_prompt(params: dict) -> dict:
"""
Text to image with image Prompt
Args:
params (dict): Params
Returns:
dict: Response
"""
params["outpaint_selections"] = ["Left", "Right"]
data = json.dumps(params)
response = requests.post(
url=f"{Config.fooocus_host}{Config.text2img_ip}",
data=data,
timeout=300)
return response.json()
# ################################################################
# Upscale or Vary
# ################################################################
# Upscale (2x) example
uov_params = {
"input_image": ImageList.image_prompt_0,
"performance_selection": "Lightning",
"uov_method": "Upscale (2x)",
"async_process": True
}
upscale_result = upscale_vary(params=uov_params)
print(
json.dumps(
upscale_result,
indent=4,
ensure_ascii=False
))
# Vary (Strong) example
uov_params['uov_method'] = 'Vary (Strong)'
vary_result = upscale_vary(params=uov_params)
print(
json.dumps(
vary_result,
indent=4,
ensure_ascii=False
))
# ################################################################
# Inpaint or Outpaint
# ################################################################
# Inpaint outpaint example
inpaint_params = {
"prompt": "a cat", # use background prompt to remove anything what you don't want
"performance_selection": "Speed", # use Lightning the quality is not good
"input_image": ImageList.inpaint_source,
"input_mask": ImageList.inpaint_mask,
"outpaint_selections": ["Left", "Right"],
"async_process": False
}
inpaint_result = inpaint_outpaint(params=inpaint_params)
print(json.dumps(inpaint_result))
# ################################################################
# Image Prompt example
# more detail for image prompt can be found:
# https://github.com/lllyasviel/Fooocus/discussions/557
# ################################################################
# face swap example
# This parameter comes from the default parameter of the Fooocus interface,
# but the effect of using this parameter for face swap with Fooocus is general.
# It is too large for the return of the original image. If necessary, try to adjust more parameters.
face_swap_params = {
"performance_selection": "Speed",
"aspect_ratios_selection": "896*1152",
"image_prompts": [
{
"cn_img": ImageList.source_face_m,
"cn_stop": 0.5,
"cn_weight": 0.6,
"cn_type": "ImagePrompt"
}, {
"cn_img": ImageList.target_face,
"cn_stop": 0.9,
"cn_weight": 0.75,
"cn_type": "FaceSwap"
}
],
"async_process": False
}
face_swap_result = image_prompt(params=face_swap_params)
print(json.dumps(face_swap_result))
# ################################################################
# Text to image with image Prompt
# ################################################################
# Text to image with image Prompt example
t2i_ip_params = {
"prompt": "a cat",
"performance_selection": "Speed",
"image_prompts": [
{
"cn_img": ImageList.image_prompt_1,
"cn_stop": 0.6,
"cn_weight": 0.8,
"cn_type": "ImagePrompt"
}, {
"cn_img": ImageList.image_prompt_2,
"cn_stop": 0.6,
"cn_weight": 0.6,
"cn_type": "ImagePrompt"
}
],
"async_process": False
}
t2i_ip_result = text2image_image_prompt(params=t2i_ip_params)
print(json.dumps(t2i_ip_result))
# ################################################################
# Image Enhance
# ################################################################
# Image Enhance
import requests
import json
url = "http://localhost:8888/v2/generation/image-enhance"
headers = {
"Content-Type": "application/json"
}
data = {
"enhance_input_image": "https://raw.githubusercontent.com/mrhan1993/Fooocus-API/main/examples/imgs/source_face_man.png",
"enhance_checkbox": True,
"enhance_uov_method": "Vary (Strong)",
"enhance_uov_processing_order": "Before First Enhancement",
"enhance_uov_prompt_type": "Original Prompts",
"enhance_ctrlnets": [
{
"enhance_enabled": True,
"enhance_mask_dino_prompt": "face",
"enhance_prompt": "",
"enhance_negative_prompt": "",
"enhance_mask_model": "sam",
"enhance_mask_cloth_category": "full",
"enhance_mask_sam_model": "vit_b",
"enhance_mask_text_threshold": 0.25,
"enhance_mask_box_threshold": 0.3,
"enhance_mask_sam_max_detections": 0,
"enhance_inpaint_disable_initial_latent": False,
"enhance_inpaint_engine": "v2.6",
"enhance_inpaint_strength": 1.0,
"enhance_inpaint_respective_field": 0.618,
"enhance_inpaint_erode_or_dilate": 0.0,
"enhance_mask_invert": False
}
]
}
response = requests.post(
url,
headers=headers,
data=json.dumps(data),
timeout=180)
if response.status_code == 200:
print("Request successful!")
print("Response:", response.json())
else:
print("Request failed with status code:", response.status_code)
print("Response:", response.text)
|