Image_Gen_Server/fencer_test.py

50 lines
1.5 KiB
Python
Raw Normal View History

2024-10-07 10:45:43 +00:00
import json
import urllib
from urllib import request, parse
import random
from requests_toolbelt.multipart.encoder import MultipartEncoder
server_address = "http://127.0.0.1:8188"
def queue_prompt(prompt):
p = {"prompt": prompt}
data = json.dumps(p).encode('utf-8')
req = request.Request(server_address + "/prompt", data=data)
request.urlopen(req)
def upload_image(input_path, name, server_address, image_type="input", overwrite=False):
with open(input_path, 'rb') as file:
multipart_data = MultipartEncoder(
fields= {
'image': (name, file, 'image/png'),
'type': image_type,
'overwrite': str(overwrite).lower()
}
)
data = multipart_data
headers = { 'Content-Type': multipart_data.content_type }
request = urllib.request.Request("http://{}/upload/image".format(server_address), data=data, headers=headers)
with urllib.request.urlopen(request) as response:
return response.read()
def gen_image(openpose_image_path):
# read fencerAPI.json into prompt_text
with open("fencerAPI.json", "r") as f:
prompt_json = f.read()
prompt = json.loads(prompt_json)
# upload images
upload_image("ref_black.png", "ref_black.png", server_address)
upload_image(openpose_image_path, "openpose_image.png", server_address)
prompt["3"]["inputs"]["seed"] = random.randint(0, 10000000000)
prompt["29"]["inputs"]['image'] = "ref_black.png"
prompt["17"]["inputs"]['image'] = "openpose_image.png"
queue_prompt(prompt)