'commit'
This commit is contained in:
76
dsLightRag/Liblib/ControlNetGenerator.py
Normal file
76
dsLightRag/Liblib/ControlNetGenerator.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
import json
|
||||||
|
from LiblibUtil import LiblibUtil
|
||||||
|
|
||||||
|
class ControlNetImageGenerator:
|
||||||
|
def __init__(self):
|
||||||
|
self.liblib_util = LiblibUtil()
|
||||||
|
# PuLID人像换脸参数示例
|
||||||
|
self.template_uuid = "6f7c4652458d4802969f8d089cf5b91f"
|
||||||
|
self.default_params = {
|
||||||
|
"steps": 20,
|
||||||
|
"width": 768,
|
||||||
|
"height": 1024,
|
||||||
|
"imgCount": 1
|
||||||
|
}
|
||||||
|
|
||||||
|
def set_template_uuid(self, template_uuid):
|
||||||
|
"""设置模板UUID"""
|
||||||
|
self.template_uuid = template_uuid
|
||||||
|
return self
|
||||||
|
|
||||||
|
def set_default_params(self, **kwargs):
|
||||||
|
"""设置默认生成参数"""
|
||||||
|
self.default_params.update(kwargs)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def build_control_net_params(self, reference_image_url, control_weight=1.0):
|
||||||
|
"""构建控制网络参数"""
|
||||||
|
return [{
|
||||||
|
"unitOrder": 0,
|
||||||
|
"sourceImage": reference_image_url,
|
||||||
|
"width": self.default_params["width"],
|
||||||
|
"height": self.default_params["height"],
|
||||||
|
"preprocessor": 0,
|
||||||
|
"annotationParameters": {"none": {}},
|
||||||
|
"model": "405836d1ae2646b4ba2716ed6bd5453a",
|
||||||
|
"controlWeight": control_weight,
|
||||||
|
"startingControlStep": 0,
|
||||||
|
"endingControlStep": 1,
|
||||||
|
"pixelPerfect": 1,
|
||||||
|
"controlMode": 0,
|
||||||
|
"resizeMode": 1
|
||||||
|
}]
|
||||||
|
|
||||||
|
def generate_image(self, prompt, reference_image_url, control_weight=1.0):
|
||||||
|
"""根据提示词和参考图片生成图像"""
|
||||||
|
# 构建生成参数
|
||||||
|
generate_params = {
|
||||||
|
"templateUuid": self.template_uuid,
|
||||||
|
"generateParams": {
|
||||||
|
"prompt": prompt,
|
||||||
|
**self.default_params,
|
||||||
|
"controlNet": self.build_control_net_params(reference_image_url, control_weight)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# 调用生成接口
|
||||||
|
try:
|
||||||
|
response = self.liblib_util.post_request(
|
||||||
|
"/api/generate/webui/text2img",
|
||||||
|
generate_params
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"API响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
|
||||||
|
|
||||||
|
if response and "generateUuid" in response:
|
||||||
|
print("✅ 图像生成任务已成功提交!")
|
||||||
|
print(f"生成UUID: {response['generateUuid']}")
|
||||||
|
print("提示: 可使用T2.py中的get_generation_status方法查询生成进度")
|
||||||
|
return response
|
||||||
|
else:
|
||||||
|
error_msg = response.get('message', '未知错误') if response else 'API无响应'
|
||||||
|
print(f"❌ 图像生成失败: {error_msg}")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
print(f"请求发生异常: {str(e)}")
|
||||||
|
return None
|
@@ -1,10 +1,13 @@
|
|||||||
import hmac
|
import hmac
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
import base64
|
import base64
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
import requests
|
import requests
|
||||||
import Config.Config
|
import Config.Config
|
||||||
|
from Util.ObsUtil import ObsUploader
|
||||||
|
|
||||||
|
|
||||||
class LiblibUtil:
|
class LiblibUtil:
|
||||||
@@ -79,7 +82,7 @@ class LiblibUtil:
|
|||||||
payload = {"generateUuid": generate_uuid}
|
payload = {"generateUuid": generate_uuid}
|
||||||
return self.post_request(uri, payload)
|
return self.post_request(uri, payload)
|
||||||
|
|
||||||
def wait_for_generation_completion(self, generate_uuid, interval=5):
|
def wait_for_generation_completion(self, generate_uuid, interval=5, max_wait_time=100):
|
||||||
"""等待生成完成并返回最终状态"""
|
"""等待生成完成并返回最终状态"""
|
||||||
while True:
|
while True:
|
||||||
status_data = self.get_generation_status(generate_uuid)
|
status_data = self.get_generation_status(generate_uuid)
|
||||||
|
@@ -1,9 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
import requests
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
from LiblibUtil import LiblibUtil
|
from LiblibUtil import LiblibUtil
|
||||||
from Util.ObsUtil import ObsUploader # 导入OBS上传工具
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
liblib = LiblibUtil()
|
liblib = LiblibUtil()
|
||||||
|
@@ -1,61 +1,17 @@
|
|||||||
import json
|
import json
|
||||||
from LiblibUtil import LiblibUtil
|
from LiblibUtil import LiblibUtil
|
||||||
|
from ControlNetGenerator import ControlNetImageGenerator
|
||||||
def generate_image_with_controlnet():
|
|
||||||
# 初始化Liblib工具类
|
|
||||||
liblib_util = LiblibUtil()
|
|
||||||
|
|
||||||
# 构建生成参数
|
|
||||||
generate_params = {
|
|
||||||
"templateUuid": "6f7c4652458d4802969f8d089cf5b91f",
|
|
||||||
"generateParams": {
|
|
||||||
"prompt": "filmfotos, Asian portrait,A young woman wearing a green baseball cap,covering one eye with her hand",
|
|
||||||
"steps": 20,
|
|
||||||
"width": 768,
|
|
||||||
"height": 1024,
|
|
||||||
"imgCount": 1,
|
|
||||||
"controlNet": [
|
|
||||||
{
|
|
||||||
"unitOrder": 0,
|
|
||||||
"sourceImage": "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/HuangWanQiao.jpg",
|
|
||||||
"width": 768,
|
|
||||||
"height": 1024,
|
|
||||||
"preprocessor": 0,
|
|
||||||
"annotationParameters": {"none": {}},
|
|
||||||
"model": "405836d1ae2646b4ba2716ed6bd5453a",
|
|
||||||
"controlWeight": 1,
|
|
||||||
"startingControlStep": 0,
|
|
||||||
"endingControlStep": 1,
|
|
||||||
"pixelPerfect": 1,
|
|
||||||
"controlMode": 0,
|
|
||||||
"resizeMode": 1
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# 调用生成接口
|
|
||||||
try:
|
|
||||||
response = liblib_util.post_request(
|
|
||||||
endpoint="/api/generate/webui/text2img",
|
|
||||||
params=generate_params
|
|
||||||
)
|
|
||||||
|
|
||||||
print(f"API响应: {json.dumps(response, ensure_ascii=False, indent=2)}")
|
|
||||||
|
|
||||||
# 修复成功条件判断 - 检查是否返回generateUuid
|
|
||||||
if response and "generateUuid" in response:
|
|
||||||
print("✅ 图像生成任务已成功提交!")
|
|
||||||
print(f"生成UUID: {response['generateUuid']}")
|
|
||||||
print("提示: 可使用T2.py中的get_generation_status方法查询生成进度")
|
|
||||||
return response
|
|
||||||
else:
|
|
||||||
error_msg = response.get('message', '未知错误') if response else 'API无响应'
|
|
||||||
print(f"❌ 图像生成失败: {error_msg}")
|
|
||||||
return None
|
|
||||||
except Exception as e:
|
|
||||||
print(f"请求发生异常: {str(e)}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
generate_image_with_controlnet()
|
# 创建生成器实例
|
||||||
|
generator = ControlNetImageGenerator()
|
||||||
|
|
||||||
|
# 可选:修改默认参数
|
||||||
|
generator.set_default_params(steps=25, width=800, height=1000)
|
||||||
|
|
||||||
|
# 生成图像
|
||||||
|
generator.generate_image(
|
||||||
|
prompt="filmfotos, Asian portrait,A young woman wearing a green baseball cap,covering one eye with her hand",
|
||||||
|
reference_image_url="https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/Backup/HuangWanQiao.jpg",
|
||||||
|
control_weight=0.8
|
||||||
|
)
|
Reference in New Issue
Block a user