File size: 6,508 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
"""
Examples codes for Fooocus API
"""
import json
import os
import requests


class Config:
    """
    Config
    Attributes:
        fooocus_host (str): Fooocus API host
        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'

    text2image = '/v1/generation/text-to-image'
    img_upscale = '/v1/generation/image-upscale-vary'
    inpaint_outpaint = '/v1/generation/image-inpaint-outpaint'
    img_prompt = '/v1/generation/image-prompt'


def read_image(image_name: str) -> bytes:
    """
    Read image from file
    Args:
        image_name (str): Image file name
    Returns:
        image (bytes): Image data
    """
    path = os.path.join('imgs', image_name)
    with open(path, "rb") as f:
        image = f.read()
        f.close()
    return image


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 text2image(params: dict) -> dict:
    """
    Text to image
    Args:
        params (dict): Params
    Returns:
        dict: Response
    """
    data = json.dumps(params)
    response = requests.post(
        url=f"{Config.fooocus_host}{Config.text2image}",
        data=data,
        timeout=300)
    return response.json()


def upscale_vary(image: bytes, params: dict) -> dict:
    """
    Upscale or Vary
    Args:
        image (bytes): Image data
        params (dict): Params
    Returns:
        dict: Response
    """
    response = requests.post(
        url=f"{Config.fooocus_host}{Config.img_upscale}",
        data=params,
        files={
            'input_image': image,
        },
        timeout=300)
    return response.json()


def inpaint_outpaint(
        params: dict,
        input_image: bytes,
        input_mask: bytes = None) -> dict:
    """
    Inpaint or Outpaint
    Args:
        params (dict): Params
        input_image (bytes): Image data
        input_mask (bytes): Image mask data
    Returns:
        dict: Response
    """
    response = requests.post(
        url=f"{Config.fooocus_host}{Config.inpaint_outpaint}",
        data=params,
        files={
            'input_image': input_image,
            'input_mask': input_mask
        },
        timeout=300)
    return response.json()


def image_prompt(
        params: dict,
        input_image: bytes = None,
        input_mask: bytes = None,
        cn_img1: bytes = None,
        cn_img2: bytes = None,
        cn_img3: bytes = None,
        cn_img4: bytes = None,) -> dict:
    """
    Image Prompt
    Args:
        params (dict): Params
        input_image (bytes): Image data
        input_mask (bytes): Image mask data
        cn_img1 (bytes): Image data
        cn_img2 (bytes): Image data
        cn_img3 (bytes): Image data
        cn_img4 (bytes): Image data
    Returns:
        dict: Response
    """
    response = requests.post(
        url=f"{Config.fooocus_host}{Config.img_prompt}",
        data=params,
        files={
            'input_image': input_image,
            'input_mask': input_mask,
            'cn_img1': cn_img1,
            'cn_img2': cn_img2,
            'cn_img3': cn_img3,
            'cn_img4': cn_img4
        },
        timeout=300)
    return response.json()


# ###############################################################
# Text to image example
# ################################################################

# Text to image example
t2i_params = {
    "prompt": "a cat",
    "performance_selection": "Lightning",
    "aspect_ratios_selection": "896*1152",
    "async_process": True
}

t2i_result = text2image(params=t2i_params)
print(json.dumps(t2i_result))


# ###############################################################
# Upscale or Vary example
# ################################################################

# Upscale or Vary example
up_params = {
    "uov_method": "Upscale (2x)",
    "async_process": True
}

up_result = upscale_vary(
    image=ImageList.bear,
    params=up_params
)
print(json.dumps(up_result))


# ###############################################################
# Inpaint or Outpaint example
# ################################################################

# Inpaint or Outpaint example
io_params = {
    "prompt": "a cat",
    "outpaint_selections": "Left,Top",
    "async_process": True
}

io_result = inpaint_outpaint(
    params=io_params,
    input_image=ImageList.inpaint_source,
    input_mask=ImageList.inpaint_mask
)
print(json.dumps(io_result))


# ###############################################################
# Image prompt example
# ################################################################

# Image prompt example
ip_params = {
    "prompt": "a cat",
    "image_prompts": [
        {
            "cn_stop": 0.6,
            "cn_weight": 0.6,
            "cn_type": "ImagePrompt"
        },
        {
            "cn_stop": 0.6,
            "cn_weight": 0.6,
            "cn_type": "ImagePrompt"
        }
    ]
}

ip_result = image_prompt(
    params=ip_params,
    cn_img1=ImageList.image_prompt_0,
    cn_img2=ImageList.image_prompt_1,
)
print(json.dumps(ip_result))

# ###############################################################
# Image Enhance
# ################################################################

# Image Enhance

import requests

url = "http://localhost:8888/v1/generation/image-enhance"

# Define the file path and other form data
file_path = "./examples/imgs/source_face_man.png"
form_data = {
    "enhance_checkbox": True,
    "enhance_uov_method": "Disabled",
    "enhance_enabled_1": True,
    "enhance_mask_dino_prompt_1": "face",
    "enhance_enabled_2": True,
    "enhance_mask_dino_prompt_2": "eyes",
}

# Open the file and prepare it for the request
with open(file_path, "rb") as f:
    image = f.read()
    f.close()

# Send the request
response = requests.post(
    url,
    files={"enhance_input_image": image},
    data=form_data,
    timeout=180)

# Print the response content
print(response.text)