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