Spaces:
Sleeping
Sleeping
wait 5s->1s
Browse files- kolorsvton1024.py +179 -179
kolorsvton1024.py
CHANGED
@@ -1,180 +1,180 @@
|
|
1 |
-
import time
|
2 |
-
import jwt
|
3 |
-
import requests
|
4 |
-
import json
|
5 |
-
import base64
|
6 |
-
import os
|
7 |
-
|
8 |
-
|
9 |
-
def image_to_base64(image_path):
|
10 |
-
"""
|
11 |
-
将图片文件转换为Base64编码格式
|
12 |
-
:param image_path: 图片文件路径
|
13 |
-
:return: Base64编码字符串
|
14 |
-
"""
|
15 |
-
with open(image_path, "rb") as image_file:
|
16 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
17 |
-
|
18 |
-
|
19 |
-
ak = "c1748494bc5d42ed8db2b2d24ceb1a2b" # 填写access key
|
20 |
-
sk = "1cf0bbef768746f79d30ef13630b393b" # 填写secret key
|
21 |
-
|
22 |
-
|
23 |
-
def encode_jwt_token(ak, sk):
|
24 |
-
headers = {
|
25 |
-
"alg": "HS256",
|
26 |
-
"typ": "JWT"
|
27 |
-
}
|
28 |
-
payload = {
|
29 |
-
"iss": ak,
|
30 |
-
"exp": int(time.time()) + 1800, # 有效时间,此处示例代表当前时间+1800s(30min)
|
31 |
-
"nbf": int(time.time()) - 5 # 开始生效的时间,此处示例代表当前时间-5秒
|
32 |
-
}
|
33 |
-
token = jwt.encode(payload, sk, headers=headers)
|
34 |
-
return token
|
35 |
-
|
36 |
-
|
37 |
-
# 图片及回调配置
|
38 |
-
# HUMAN_IMAGE = image_to_base64(r"F:\codetest24\0903_lf\vton\people.jpg") # 替换为人物图片的Base64或URL
|
39 |
-
# CLOTH_IMAGE = image_to_base64(r"F:\codetest24\0903_lf\vton\hanfu.jpg") # 替换为服饰图片的Base64或URL
|
40 |
-
# CALLBACK_URL = "" # 可选,任务结果回调通知URL
|
41 |
-
SAVE_DIRECTORY = "downloads/kolor"
|
42 |
-
BASE_URL = "https://api.klingai.com/v1/images/kolors-virtual-try-on"
|
43 |
-
|
44 |
-
|
45 |
-
# ========== 步骤 1:创建虚拟试穿任务 ==========
|
46 |
-
|
47 |
-
def create_virtual_tryon_task(humen, cloth, HEADERS):
|
48 |
-
"""
|
49 |
-
创建虚拟试穿任务
|
50 |
-
"""
|
51 |
-
|
52 |
-
# ========== 配置参数 ==========
|
53 |
-
|
54 |
-
|
55 |
-
# 请求体数据
|
56 |
-
data = {
|
57 |
-
"model_name": "kolors-virtual-try-on-v1",
|
58 |
-
"human_image": image_to_base64(humen),
|
59 |
-
"cloth_image": image_to_base64(cloth),
|
60 |
-
}
|
61 |
-
|
62 |
-
# 发起POST请求创建任务
|
63 |
-
response = requests.post(BASE_URL, headers=HEADERS, data=json.dumps(data))
|
64 |
-
|
65 |
-
# 处理响应
|
66 |
-
if response.status_code == 200:
|
67 |
-
result = response.json()
|
68 |
-
task_id = result['data']['task_id']
|
69 |
-
print(f"创建任务成功,任务ID: {task_id}")
|
70 |
-
return task_id
|
71 |
-
else:
|
72 |
-
print(f"创建任务失败: {response.status_code}, {response.text}")
|
73 |
-
return None
|
74 |
-
|
75 |
-
|
76 |
-
# ========== 步骤 2:查询单个虚拟试穿任务状态并保存图片 ==========
|
77 |
-
|
78 |
-
def query_virtual_tryon_task(task_id, i, HEADERS):
|
79 |
-
"""
|
80 |
-
根据任务ID查询任务状态,成功后保存生成的图片
|
81 |
-
"""
|
82 |
-
# 请求URL
|
83 |
-
url = f"{BASE_URL}/{task_id}"
|
84 |
-
|
85 |
-
# 发起GET请求查询任务状态
|
86 |
-
response = requests.get(url, headers=HEADERS)
|
87 |
-
|
88 |
-
# 处理响应
|
89 |
-
if response.status_code == 200:
|
90 |
-
result = response.json()
|
91 |
-
task_status = result['data']['task_status']
|
92 |
-
print(f"任务状态: {task_status}")
|
93 |
-
|
94 |
-
# 如果任务成功,下载并保存生成的图片
|
95 |
-
if task_status == 'succeed':
|
96 |
-
images = result['data']['task_result']['images']
|
97 |
-
for image in images:
|
98 |
-
image_url = image['url']
|
99 |
-
image_index = i
|
100 |
-
save_image(image_url, image_index)
|
101 |
-
|
102 |
-
# 返回任务状态
|
103 |
-
return task_status
|
104 |
-
else:
|
105 |
-
print(f"查询任务失败: {response.status_code}, {response.text}")
|
106 |
-
return None
|
107 |
-
|
108 |
-
|
109 |
-
# ========== 步骤 3:下载并保存图片 ==========
|
110 |
-
|
111 |
-
def save_image(image_url, image_index):
|
112 |
-
"""
|
113 |
-
根据图片URL下载并保存到本地
|
114 |
-
"""
|
115 |
-
response = requests.get(image_url)
|
116 |
-
|
117 |
-
if response.status_code == 200:
|
118 |
-
# 保存图片到本地
|
119 |
-
image_path = os.path.join(SAVE_DIRECTORY, f"kolor_{image_index}.png")
|
120 |
-
with open(image_path, "wb") as file:
|
121 |
-
file.write(response.content)
|
122 |
-
print(f"图片已保存到: {image_path}")
|
123 |
-
else:
|
124 |
-
print(f"下载图片失败: {response.status_code}, {response.text}")
|
125 |
-
|
126 |
-
|
127 |
-
# ========== 步骤 4:查询任务列表 ==========
|
128 |
-
|
129 |
-
def query_task_list(page_num=1, page_size=30, HEADERS=None):
|
130 |
-
"""
|
131 |
-
查询任务列表
|
132 |
-
"""
|
133 |
-
# 查询参数
|
134 |
-
params = {
|
135 |
-
"pageNum": page_num,
|
136 |
-
"pageSize": page_size
|
137 |
-
}
|
138 |
-
|
139 |
-
# 发起GET请求查询任务列表
|
140 |
-
response = requests.get(BASE_URL, headers=HEADERS, params=params)
|
141 |
-
|
142 |
-
# 处理响应
|
143 |
-
if response.status_code == 200:
|
144 |
-
result = response.json()
|
145 |
-
print("任务列表:")
|
146 |
-
for task in result['data']:
|
147 |
-
print(f"任务ID: {task['task_id']}, 状态: {task['task_status']}, 创建时间: {task['created_at']}")
|
148 |
-
else:
|
149 |
-
print(f"查询任务列表失败: {response.status_code}, {response.text}")
|
150 |
-
|
151 |
-
|
152 |
-
# ========== 主流程 ==========
|
153 |
-
|
154 |
-
def kolor_vton(humen, cloth, i):
|
155 |
-
# API 请求的基础配置
|
156 |
-
api_token = encode_jwt_token(ak, sk)
|
157 |
-
print(api_token) # 打印生成的API_TOKEN
|
158 |
-
# 请求头
|
159 |
-
HEADERS = {
|
160 |
-
"Content-Type": "application/json",
|
161 |
-
"Authorization": f"Bearer {api_token}"
|
162 |
-
}
|
163 |
-
|
164 |
-
# 1. 创建虚拟试穿任务
|
165 |
-
task_id = create_virtual_tryon_task(humen, cloth, HEADERS)
|
166 |
-
|
167 |
-
#
|
168 |
-
if task_id:
|
169 |
-
# 2. 定期查询该任务状态,直到任务完成
|
170 |
-
while True:
|
171 |
-
status = query_virtual_tryon_task(task_id, i, HEADERS)
|
172 |
-
if status in ['succeed', 'failed']:
|
173 |
-
break
|
174 |
-
print("任务正在处理中,等待
|
175 |
-
time.sleep(
|
176 |
-
|
177 |
-
# 3. 查询任务列表
|
178 |
-
query_task_list(page_num=1, page_size=10, HEADERS=HEADERS)
|
179 |
-
|
180 |
# kolor_vton("uploads/user_image.jpg", "uploads/user_image.jpg", 1)
|
|
|
1 |
+
import time
|
2 |
+
import jwt
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
|
8 |
+
|
9 |
+
def image_to_base64(image_path):
|
10 |
+
"""
|
11 |
+
将图片文件转换为Base64编码格式
|
12 |
+
:param image_path: 图片文件路径
|
13 |
+
:return: Base64编码字符串
|
14 |
+
"""
|
15 |
+
with open(image_path, "rb") as image_file:
|
16 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
17 |
+
|
18 |
+
|
19 |
+
ak = "c1748494bc5d42ed8db2b2d24ceb1a2b" # 填写access key
|
20 |
+
sk = "1cf0bbef768746f79d30ef13630b393b" # 填写secret key
|
21 |
+
|
22 |
+
|
23 |
+
def encode_jwt_token(ak, sk):
|
24 |
+
headers = {
|
25 |
+
"alg": "HS256",
|
26 |
+
"typ": "JWT"
|
27 |
+
}
|
28 |
+
payload = {
|
29 |
+
"iss": ak,
|
30 |
+
"exp": int(time.time()) + 1800, # 有效时间,此处示例代表当前时间+1800s(30min)
|
31 |
+
"nbf": int(time.time()) - 5 # 开始生效的时间,此处示例代表当前时间-5秒
|
32 |
+
}
|
33 |
+
token = jwt.encode(payload, sk, headers=headers)
|
34 |
+
return token
|
35 |
+
|
36 |
+
|
37 |
+
# 图片及回调配置
|
38 |
+
# HUMAN_IMAGE = image_to_base64(r"F:\codetest24\0903_lf\vton\people.jpg") # 替换为人物图片的Base64或URL
|
39 |
+
# CLOTH_IMAGE = image_to_base64(r"F:\codetest24\0903_lf\vton\hanfu.jpg") # 替换为服饰图片的Base64或URL
|
40 |
+
# CALLBACK_URL = "" # 可选,任务结果回调通知URL
|
41 |
+
SAVE_DIRECTORY = "downloads/kolor"
|
42 |
+
BASE_URL = "https://api.klingai.com/v1/images/kolors-virtual-try-on"
|
43 |
+
|
44 |
+
|
45 |
+
# ========== 步骤 1:创建虚拟试穿任务 ==========
|
46 |
+
|
47 |
+
def create_virtual_tryon_task(humen, cloth, HEADERS):
|
48 |
+
"""
|
49 |
+
创建虚拟试穿任务
|
50 |
+
"""
|
51 |
+
|
52 |
+
# ========== 配置参数 ==========
|
53 |
+
|
54 |
+
|
55 |
+
# 请求体数据
|
56 |
+
data = {
|
57 |
+
"model_name": "kolors-virtual-try-on-v1",
|
58 |
+
"human_image": image_to_base64(humen),
|
59 |
+
"cloth_image": image_to_base64(cloth),
|
60 |
+
}
|
61 |
+
|
62 |
+
# 发起POST请求创建任务
|
63 |
+
response = requests.post(BASE_URL, headers=HEADERS, data=json.dumps(data))
|
64 |
+
|
65 |
+
# 处理响应
|
66 |
+
if response.status_code == 200:
|
67 |
+
result = response.json()
|
68 |
+
task_id = result['data']['task_id']
|
69 |
+
print(f"创建任务成功,任务ID: {task_id}")
|
70 |
+
return task_id
|
71 |
+
else:
|
72 |
+
print(f"创建任务失败: {response.status_code}, {response.text}")
|
73 |
+
return None
|
74 |
+
|
75 |
+
|
76 |
+
# ========== 步骤 2:查询单个虚拟试穿任务状态并保存图片 ==========
|
77 |
+
|
78 |
+
def query_virtual_tryon_task(task_id, i, HEADERS):
|
79 |
+
"""
|
80 |
+
根据任务ID查询任务状态,成功后保存生成的图片
|
81 |
+
"""
|
82 |
+
# 请求URL
|
83 |
+
url = f"{BASE_URL}/{task_id}"
|
84 |
+
|
85 |
+
# 发起GET请求查询任务状态
|
86 |
+
response = requests.get(url, headers=HEADERS)
|
87 |
+
|
88 |
+
# 处理响应
|
89 |
+
if response.status_code == 200:
|
90 |
+
result = response.json()
|
91 |
+
task_status = result['data']['task_status']
|
92 |
+
print(f"任务状态: {task_status}")
|
93 |
+
|
94 |
+
# 如果任务成功,下载并保存生成的图片
|
95 |
+
if task_status == 'succeed':
|
96 |
+
images = result['data']['task_result']['images']
|
97 |
+
for image in images:
|
98 |
+
image_url = image['url']
|
99 |
+
image_index = i
|
100 |
+
save_image(image_url, image_index)
|
101 |
+
|
102 |
+
# 返回任务状态
|
103 |
+
return task_status
|
104 |
+
else:
|
105 |
+
print(f"查询任务失败: {response.status_code}, {response.text}")
|
106 |
+
return None
|
107 |
+
|
108 |
+
|
109 |
+
# ========== 步骤 3:下载并保存图片 ==========
|
110 |
+
|
111 |
+
def save_image(image_url, image_index):
|
112 |
+
"""
|
113 |
+
根据图片URL下载并保存到本地
|
114 |
+
"""
|
115 |
+
response = requests.get(image_url)
|
116 |
+
|
117 |
+
if response.status_code == 200:
|
118 |
+
# 保存图片到本地
|
119 |
+
image_path = os.path.join(SAVE_DIRECTORY, f"kolor_{image_index}.png")
|
120 |
+
with open(image_path, "wb") as file:
|
121 |
+
file.write(response.content)
|
122 |
+
print(f"图片已保存到: {image_path}")
|
123 |
+
else:
|
124 |
+
print(f"下载图片失败: {response.status_code}, {response.text}")
|
125 |
+
|
126 |
+
|
127 |
+
# ========== 步骤 4:查询任务列表 ==========
|
128 |
+
|
129 |
+
def query_task_list(page_num=1, page_size=30, HEADERS=None):
|
130 |
+
"""
|
131 |
+
查询任务列表
|
132 |
+
"""
|
133 |
+
# 查询参数
|
134 |
+
params = {
|
135 |
+
"pageNum": page_num,
|
136 |
+
"pageSize": page_size
|
137 |
+
}
|
138 |
+
|
139 |
+
# 发起GET请求查询任务列表
|
140 |
+
response = requests.get(BASE_URL, headers=HEADERS, params=params)
|
141 |
+
|
142 |
+
# 处理响应
|
143 |
+
if response.status_code == 200:
|
144 |
+
result = response.json()
|
145 |
+
print("任务列表:")
|
146 |
+
for task in result['data']:
|
147 |
+
print(f"任务ID: {task['task_id']}, 状态: {task['task_status']}, 创建时间: {task['created_at']}")
|
148 |
+
else:
|
149 |
+
print(f"查询任务列表失败: {response.status_code}, {response.text}")
|
150 |
+
|
151 |
+
|
152 |
+
# ========== 主流程 ==========
|
153 |
+
|
154 |
+
def kolor_vton(humen, cloth, i):
|
155 |
+
# API 请求的基础配置
|
156 |
+
api_token = encode_jwt_token(ak, sk)
|
157 |
+
print(api_token) # 打印生成的API_TOKEN
|
158 |
+
# 请求头
|
159 |
+
HEADERS = {
|
160 |
+
"Content-Type": "application/json",
|
161 |
+
"Authorization": f"Bearer {api_token}"
|
162 |
+
}
|
163 |
+
|
164 |
+
# 1. 创建虚拟试穿任务
|
165 |
+
task_id = create_virtual_tryon_task(humen, cloth, HEADERS)
|
166 |
+
|
167 |
+
# 如果任务创建成功,继续执行查询步骤
|
168 |
+
if task_id:
|
169 |
+
# 2. 定期查询该任务状态,直到任务完成
|
170 |
+
while True:
|
171 |
+
status = query_virtual_tryon_task(task_id, i, HEADERS)
|
172 |
+
if status in ['succeed', 'failed']:
|
173 |
+
break
|
174 |
+
print("任务正在处理中,等待1秒后重试...")
|
175 |
+
time.sleep(1) # 等待1秒后重试
|
176 |
+
|
177 |
+
# 3. 查询任务列表
|
178 |
+
query_task_list(page_num=1, page_size=10, HEADERS=HEADERS)
|
179 |
+
|
180 |
# kolor_vton("uploads/user_image.jpg", "uploads/user_image.jpg", 1)
|