Files
change_hair/project/photo_service/webui_im2im2.py
T
xsl 443cfa298f 初始化:换发型/换发色/训练发型服务
包含:
- hair_service_sd: 主服务(换发型/换发色/生发,端口8801)
- photo_service: LoRA调度+训练(端口32678)
- hair_grow_service: 调试测试页(端口8888,含4个测试页)
- 批量训练脚本(batch_train_hairstyles.py)
- 发际线mask自动识别(hairline_mask.py,4种方案)
- 手绘mask换发型(hair_swap_manual.py)
- 文档:README.md + LARGE_FILES.md + docs/

大文件(模型权重200G、训练数据123G)已排除,见 LARGE_FILES.md
OSS/COS密钥已脱敏为环境变量,原文件备份在本地
2026-07-07 13:53:52 +08:00

87 lines
3.2 KiB
Python
Executable File

import io
import cv2
import base64
import requests
from PIL import Image
class ControlnetRequestImg2Img:
def __init__(self, prompt, net_prompt, path, mask):
self.url = "http://127.0.0.1:57860/sdapi/v1/img2img"
self.prompt = prompt
self.neg_prompt = net_prompt
self.img_path = path
self.mask = mask
self.body = None
def build_body(self):
img = cv2.imread(self.img_path)
self.body = {
"prompt": self.prompt,
"negative_prompt": self.neg_prompt,
"sampler_name": "DPM++ 2M Karras",
"batch_size": 1,
"steps": 30,
"width": img.shape[1],
"height": img.shape[0],
"cfg_scale": 7,
"seed": -1,
"mask_blur": 15,
"init_images": [
self.read_image()
],
"inpaint_full_res": True,
"inpainting_fill": 1,
"inpainting_mask_invert": 1,
"mask": self.read_mask(),
"denoising_strength": 0.4,
"alwayson_scripts": {
"controlnet": {
"args": [
{
"enabled": True,
"module": "openpose_full",
"model": "openpose",
"weight": 1.0,
"resize_mode": 1,
"lowvram": False,
"processor_res": 512,
"guidance_start": 0.0,
"guidance_end": 1.0,
"control_mode": 0,
"pixel_perfect": True
},
]
},
}
}
def send_request(self):
response = requests.post(url=self.url, json=self.body)
return response.json()
def read_image(self):
img = cv2.imread(self.img_path)
retval, bytes = cv2.imencode('.png', img)
encoded_image = base64.b64encode(bytes).decode('utf-8')
return encoded_image
def read_mask(self):
img = cv2.imread(self.mask)
retval, bytes = cv2.imencode('.png', img)
encoded_image = base64.b64encode(bytes).decode('utf-8')
return encoded_image
if __name__ == '__main__':
path = '/home/chinatszrn/Downloads/user1_hr.png'
mask_path = '/home/chinatszrn/Downloads/user1_hr_mask.png'
prompt = 'a woman with long blonde hair and a blue shirt on a gray background with a gray background and a gray background, lyco art, An Gyeon, realistic face, a character portrait'
neg_prompt = '(nsfw:1.5), ng_deepnegative_v1_75t, (badhandv4:1.2), (worst quality:2), (low quality:2), (normal quality:2), lowres, bad anatomy, bad hands, ((monochrome)), ((grayscale)) watermark, moles, large breast, big breast, bad_pictures,easynegative'
control_net = ControlnetRequestImg2Img(prompt, neg_prompt, path, mask_path)
control_net.build_body()
output = control_net.send_request()
result = output['images'][0]
image = Image.open(io.BytesIO(base64.b64decode(result.split(",", 1)[0])))
image.save('save2.png')