Update main.py
Browse files
main.py
CHANGED
@@ -199,31 +199,125 @@ def txt2img(prompt, width, height, product_codes):
|
|
199 |
def generate_mask(image_resource_id, position, selected_product_code):
|
200 |
try:
|
201 |
if not image_resource_id:
|
202 |
-
raise Exception("
|
203 |
-
|
|
|
|
|
204 |
short_code = selected_product_code.split()[0]
|
205 |
texture_filepath = PRODUCT_IMAGE_MAP.get(selected_product_code)
|
|
|
206 |
if not texture_filepath or not os.path.exists(texture_filepath):
|
207 |
-
raise Exception(f"
|
208 |
|
209 |
texture_resource_id = upload_image_to_tensorart(texture_filepath)
|
|
|
210 |
if not texture_resource_id:
|
211 |
-
raise Exception(f"
|
212 |
-
time.sleep(10)
|
|
|
|
|
|
|
|
|
213 |
|
|
|
214 |
workflow_params = {
|
215 |
-
"1": {
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
220 |
}
|
221 |
-
|
222 |
-
|
|
|
|
|
223 |
except Exception as e:
|
224 |
print(f"Mask generation error: {str(e)}")
|
225 |
return None
|
226 |
|
|
|
227 |
def run_workflow(payload, step_name):
|
228 |
headers = {
|
229 |
'Content-Type': 'application/json',
|
|
|
199 |
def generate_mask(image_resource_id, position, selected_product_code):
|
200 |
try:
|
201 |
if not image_resource_id:
|
202 |
+
raise Exception("Không có image_resource_id hợp lệ - ảnh gốc chưa được upload")
|
203 |
+
print(f"Using image_resource_id: {image_resource_id}")
|
204 |
+
time.sleep(10) # Đợi đồng bộ tài nguyên
|
205 |
+
|
206 |
short_code = selected_product_code.split()[0]
|
207 |
texture_filepath = PRODUCT_IMAGE_MAP.get(selected_product_code)
|
208 |
+
print(f"Texture file: {texture_filepath}, exists: {os.path.exists(texture_filepath)}")
|
209 |
if not texture_filepath or not os.path.exists(texture_filepath):
|
210 |
+
raise Exception(f"Không tìm thấy ảnh sản phẩm cho mã {short_code}")
|
211 |
|
212 |
texture_resource_id = upload_image_to_tensorart(texture_filepath)
|
213 |
+
print(f"Texture resource_id: {texture_resource_id}")
|
214 |
if not texture_resource_id:
|
215 |
+
raise Exception(f"Không thể upload ảnh sản phẩm {short_code}")
|
216 |
+
time.sleep(10) # Đợi đồng bộ tài nguyên
|
217 |
+
|
218 |
+
if isinstance(position, (set, list)):
|
219 |
+
position = position[0] if position else "default"
|
220 |
+
print(f"Position: {position}, type: {type(position)}")
|
221 |
|
222 |
+
# Dùng params đúng như mẫu TensorArt
|
223 |
workflow_params = {
|
224 |
+
"1": {
|
225 |
+
"classType": "LayerMask: SegmentAnythingUltra V3",
|
226 |
+
"inputs": {
|
227 |
+
"black_point": 0.3,
|
228 |
+
"detail_dilate": 6,
|
229 |
+
"detail_erode": 65,
|
230 |
+
"detail_method": "GuidedFilter",
|
231 |
+
"device": "cuda",
|
232 |
+
"image": ["2", 0],
|
233 |
+
"max_megapixels": 2,
|
234 |
+
"process_detail": True,
|
235 |
+
"prompt": ["4", 0],
|
236 |
+
"sam_models": ["3", 0],
|
237 |
+
"threshold": 0.3,
|
238 |
+
"white_point": 0.99
|
239 |
+
},
|
240 |
+
"properties": {"Node name for S&R": "LayerMask: SegmentAnythingUltra V3"}
|
241 |
+
},
|
242 |
+
"10": {
|
243 |
+
"classType": "Image Seamless Texture",
|
244 |
+
"inputs": {
|
245 |
+
"blending": 0.37,
|
246 |
+
"images": ["17", 0],
|
247 |
+
"tiled": "true",
|
248 |
+
"tiles": 2
|
249 |
+
},
|
250 |
+
"properties": {"Node name for S&R": "Image Seamless Texture"}
|
251 |
+
},
|
252 |
+
"13": {
|
253 |
+
"classType": "Paste By Mask",
|
254 |
+
"inputs": {
|
255 |
+
"image_base": ["2", 0],
|
256 |
+
"image_to_paste": ["10", 0],
|
257 |
+
"mask": ["8", 0],
|
258 |
+
"resize_behavior": "resize"
|
259 |
+
},
|
260 |
+
"properties": {"Node name for S&R": "Paste By Mask"}
|
261 |
+
},
|
262 |
+
"17": {
|
263 |
+
"classType": "TensorArt_LoadImage",
|
264 |
+
"inputs": {
|
265 |
+
"_height": 768,
|
266 |
+
"_width": 512,
|
267 |
+
"image": texture_resource_id,
|
268 |
+
"upload": "image"
|
269 |
+
},
|
270 |
+
"properties": {"Node name for S&R": "TensorArt_LoadImage"}
|
271 |
+
},
|
272 |
+
"2": {
|
273 |
+
"classType": "TensorArt_LoadImage",
|
274 |
+
"inputs": {
|
275 |
+
"_height": 1024,
|
276 |
+
"_width": 768,
|
277 |
+
"image": image_resource_id,
|
278 |
+
"upload": "image"
|
279 |
+
},
|
280 |
+
"properties": {"Node name for S&R": "TensorArt_LoadImage"}
|
281 |
+
},
|
282 |
+
"3": {
|
283 |
+
"classType": "LayerMask: LoadSegmentAnythingModels",
|
284 |
+
"inputs": {
|
285 |
+
"grounding_dino_model": "GroundingDINO_SwinB (938MB)",
|
286 |
+
"sam_model": "sam_vit_h (2.56GB)"
|
287 |
+
},
|
288 |
+
"properties": {"Node name for S&R": "LayerMask: LoadSegmentAnythingModels"}
|
289 |
+
},
|
290 |
+
"4": {
|
291 |
+
"classType": "TensorArt_PromptText",
|
292 |
+
"inputs": {"Text": position.lower()},
|
293 |
+
"properties": {"Node name for S&R": "TensorArt_PromptText"}
|
294 |
+
},
|
295 |
+
"7": {
|
296 |
+
"classType": "PreviewImage",
|
297 |
+
"inputs": {"images": ["13", 0]},
|
298 |
+
"properties": {"Node name for S&R": "PreviewImage"}
|
299 |
+
},
|
300 |
+
"8": {
|
301 |
+
"classType": "MaskToImage",
|
302 |
+
"inputs": {"mask": ["1", 1]},
|
303 |
+
"properties": {"Node name for S&R": "MaskToImage"}
|
304 |
+
}
|
305 |
+
}
|
306 |
+
|
307 |
+
payload = {
|
308 |
+
"requestId": f"workflow_{int(time.time())}",
|
309 |
+
"params": workflow_params,
|
310 |
+
"runningNotifyUrl": ""
|
311 |
}
|
312 |
+
|
313 |
+
output_path = run_workflow(payload, "full_workflow")
|
314 |
+
return output_path
|
315 |
+
|
316 |
except Exception as e:
|
317 |
print(f"Mask generation error: {str(e)}")
|
318 |
return None
|
319 |
|
320 |
+
|
321 |
def run_workflow(payload, step_name):
|
322 |
headers = {
|
323 |
'Content-Type': 'application/json',
|