import json from LiblibUtil import LiblibUtil 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__": generate_image_with_controlnet()