diff --git a/BaiHu/Test/TestImg2Img.py b/BaiHu/Test/TestImg2Img.py index 4886ffa8..806d12f0 100644 --- a/BaiHu/Test/TestImg2Img.py +++ b/BaiHu/Test/TestImg2Img.py @@ -1,39 +1,11 @@ -import base64 -import json - - -def encode_image(image_path): - with open(image_path, "rb") as i: - b64 = base64.b64encode(i.read()) - return b64.decode("utf-8") - - -# 读取JSON文件 -json_file = r'../JSON/28_1.json' -with open(json_file, 'r', encoding='utf-8') as file: - payload = json.load(file) - -# 此处为读取一张图片作为输入图像 -refer_img = encode_image('../Image/28/1.jpg') -source_img = encode_image('../Image/1ae93e2587822c291362abf9504cb49ec.jpeg') - -payload["alwayson_scripts"]["ControlNet"]["args"][0]["input_image"] = source_img # 按用户输入的图片进行面部参考 -payload["init_images"] = [refer_img] # 图生图,风格参考图是输入 -payload["alwayson_scripts"]["ControlNet"]["args"][1]["input_image"] = refer_img -print(json.dumps(payload, sort_keys=False, ensure_ascii=False, indent=4)) # 生成可以正确显示中文的json格式输出 +from Util.CommonUtil import * # API地址 url = "http://192.168.1.21:7860" -# response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload) - -# if response.status_code != 200: -# print(response.text) -# exit(0) -# r = response.json() - -# image = Image.open(io.BytesIO(base64.b64decode(r['images'][0]))) - +# 用户输入的源图 +input_source_img = '../Image/1ae93e2587822c291362abf9504cb49ec.jpeg' +# 输出图片 +output_file = "../Out/Result.png" -# print(payload) -# image.show() -# image.save('output.png') +# 调用图生图 +img2img(url, 28, 1, input_source_img, output_file) diff --git a/BaiHu/Util/CommonUtil.py b/BaiHu/Util/CommonUtil.py index 163360ff..b31b7031 100644 --- a/BaiHu/Util/CommonUtil.py +++ b/BaiHu/Util/CommonUtil.py @@ -103,3 +103,34 @@ def split_4_image(image_path, out_path): bottom_right.save(fi) return res + + +# 图生图函数 +def img2img(url,model_id, prompt_id, input_source_img, output_file): + # JSON文件模板 + templet_file = r'../JSON/' + str(model_id) + '_' + str(prompt_id) + '.json' + # 模型图片 + input_refer_img = '../Image/' + str(model_id) + '/' + str(prompt_id) + '.jpg' + + # 读取JSON文件 + with open(templet_file, 'r', encoding='utf-8') as file: + payload = json.load(file) + + # 用户输入的图片 + source_img = encode_image(input_source_img) + # 模型固定的参考图片 + refer_img = encode_image(input_refer_img) + + # 在ControlNet的第一个图片中使用用户输入的图片,用此图片的人脸进行换脸操作 + payload["alwayson_scripts"]["ControlNet"]["args"][0]["input_image"] = source_img + # 图生图,风格参考图是输入 + payload["init_images"] = [refer_img] + payload["alwayson_scripts"]["ControlNet"]["args"][1]["input_image"] = refer_img + # 调用SD服务 + response = requests.post(url=f'{url}/sdapi/v1/img2img', json=payload) + if response.status_code != 200: + print(response.text) + exit(0) + r = response.json() + # 保存图片 + save_encoded_image(response.json()['images'][0], output_file) \ No newline at end of file diff --git a/BaiHu/Util/__pycache__/CommonUtil.cpython-310.pyc b/BaiHu/Util/__pycache__/CommonUtil.cpython-310.pyc index 01622a09..d7e9bb6e 100644 Binary files a/BaiHu/Util/__pycache__/CommonUtil.cpython-310.pyc and b/BaiHu/Util/__pycache__/CommonUtil.cpython-310.pyc differ