Spaces:
Running
Running
Update user_dress.py
Browse files- user_dress.py +106 -106
user_dress.py
CHANGED
@@ -1,106 +1,106 @@
|
|
1 |
-
from io import BytesIO
|
2 |
-
import fal_client
|
3 |
-
import os
|
4 |
-
from PIL import Image
|
5 |
-
import requests
|
6 |
-
import time
|
7 |
-
|
8 |
-
UPLOAD_FOLDER = 'uploads'
|
9 |
-
DOWNLOAD_FOLDER = 'downloads'
|
10 |
-
|
11 |
-
|
12 |
-
def user_dress_cat(user_pic, cloth_gen, index):
|
13 |
-
time_1 = time.time()
|
14 |
-
filename_1 = 'user_image.jpg'
|
15 |
-
filename_2 = 'cloth_image.jpg'
|
16 |
-
file_path_1 = os.path.join(UPLOAD_FOLDER, filename_1)
|
17 |
-
file_path_2 = os.path.join(UPLOAD_FOLDER, filename_2)
|
18 |
-
Image.fromarray(user_pic).save(file_path_1)
|
19 |
-
Image.fromarray(cloth_gen).save(file_path_2)
|
20 |
-
time_2 = time.time()
|
21 |
-
save_time = time_2 - time_1
|
22 |
-
print(f"save_time:{save_time}")
|
23 |
-
time_1 = time.time()
|
24 |
-
handler = fal_client.submit(
|
25 |
-
"fal-ai/cat-vton",
|
26 |
-
arguments={
|
27 |
-
"human_image_url": fal_client.upload_file(file_path_1),
|
28 |
-
"garment_image_url": fal_client.upload_file(file_path_2),
|
29 |
-
"cloth_type": "overall"
|
30 |
-
},
|
31 |
-
)
|
32 |
-
request_id = handler.request_id
|
33 |
-
result = fal_client.result("fal-ai/cat-vton", request_id)
|
34 |
-
time_2 = time.time()
|
35 |
-
cat_time = time_2 - time_1
|
36 |
-
print(f"cat_time:{cat_time}")
|
37 |
-
time_1 = time.time()
|
38 |
-
response = requests.get(result['image']['url'])
|
39 |
-
time_2 = time.time()
|
40 |
-
url_time = time_2 - time_1
|
41 |
-
print(f"url_time:{url_time}")
|
42 |
-
time_1 = time.time()
|
43 |
-
save_directory = "downloads"
|
44 |
-
if response.status_code == 200:
|
45 |
-
filename = os.path.join(save_directory, f"cat-vton_{index}.png")
|
46 |
-
with open(filename, 'wb') as f:
|
47 |
-
f.write(response.content)
|
48 |
-
else:
|
49 |
-
print(f"Failed to download image from {result['image']['url']}")
|
50 |
-
time_2 = time.time()
|
51 |
-
downloads_time = time_2 - time_1
|
52 |
-
print(f"downloads_time:{downloads_time}")
|
53 |
-
return os.path.join(save_directory, f"cat-vton_{index}.png")
|
54 |
-
|
55 |
-
|
56 |
-
def user_dress_idm(user_pic, cloth_gen, index):
|
57 |
-
time_1 = time.time()
|
58 |
-
filename_1 = 'user_image.jpg'
|
59 |
-
filename_2 = 'cloth_image.jpg'
|
60 |
-
file_path_1 = os.path.join(UPLOAD_FOLDER, filename_1)
|
61 |
-
file_path_2 = os.path.join(UPLOAD_FOLDER, filename_2)
|
62 |
-
Image.fromarray(user_pic).save(file_path_1)
|
63 |
-
Image.fromarray(cloth_gen).save(file_path_2)
|
64 |
-
time_2 = time.time()
|
65 |
-
save_time = time_2 - time_1
|
66 |
-
print(f"save_time:{save_time}")
|
67 |
-
time_1 = time.time()
|
68 |
-
handler = fal_client.submit(
|
69 |
-
"fal-ai/idm-vton",
|
70 |
-
arguments={
|
71 |
-
"human_image_url": fal_client.upload_file(file_path_1),
|
72 |
-
"garment_image_url": fal_client.upload_file(file_path_2),
|
73 |
-
"description": "long-sleeved long coat"
|
74 |
-
},
|
75 |
-
)
|
76 |
-
request_id = handler.request_id
|
77 |
-
result = fal_client.result("fal-ai/idm-vton", request_id)
|
78 |
-
time_2 = time.time()
|
79 |
-
cat_time = time_2 - time_1
|
80 |
-
print(f"idm_time:{cat_time}")
|
81 |
-
time_1 = time.time()
|
82 |
-
response = requests.get(result['image']['url'])
|
83 |
-
time_2 = time.time()
|
84 |
-
url_time = time_2 - time_1
|
85 |
-
print(f"url_time:{url_time}")
|
86 |
-
time_1 = time.time()
|
87 |
-
save_directory = "downloads"
|
88 |
-
if response.status_code == 200:
|
89 |
-
filename = os.path.join(save_directory, f"idm-vton_{index}.png")
|
90 |
-
with open(filename, 'wb') as f:
|
91 |
-
f.write(response.content)
|
92 |
-
else:
|
93 |
-
print(f"Failed to download image from {result['image']['url']}")
|
94 |
-
time_2 = time.time()
|
95 |
-
downloads_time = time_2 - time_1
|
96 |
-
print(f"downloads_time:{downloads_time}")
|
97 |
-
return os.path.join(save_directory, f"idm-vton_{index}.png")
|
98 |
-
|
99 |
-
|
100 |
-
def user_cloths(user_pic, cloth_gen):
|
101 |
-
user_cloth = []
|
102 |
-
for i in range(1,
|
103 |
-
user_cloth.append(user_dress_cat(user_pic, cloth_gen, i))
|
104 |
-
for i in range(4, 6):
|
105 |
-
|
106 |
-
return user_cloth
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
import fal_client
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
import requests
|
6 |
+
import time
|
7 |
+
|
8 |
+
UPLOAD_FOLDER = 'uploads'
|
9 |
+
DOWNLOAD_FOLDER = 'downloads'
|
10 |
+
|
11 |
+
|
12 |
+
def user_dress_cat(user_pic, cloth_gen, index):
|
13 |
+
time_1 = time.time()
|
14 |
+
filename_1 = 'user_image.jpg'
|
15 |
+
filename_2 = 'cloth_image.jpg'
|
16 |
+
file_path_1 = os.path.join(UPLOAD_FOLDER, filename_1)
|
17 |
+
file_path_2 = os.path.join(UPLOAD_FOLDER, filename_2)
|
18 |
+
Image.fromarray(user_pic).save(file_path_1)
|
19 |
+
Image.fromarray(cloth_gen).save(file_path_2)
|
20 |
+
time_2 = time.time()
|
21 |
+
save_time = time_2 - time_1
|
22 |
+
print(f"save_time:{save_time}")
|
23 |
+
time_1 = time.time()
|
24 |
+
handler = fal_client.submit(
|
25 |
+
"fal-ai/cat-vton",
|
26 |
+
arguments={
|
27 |
+
"human_image_url": fal_client.upload_file(file_path_1),
|
28 |
+
"garment_image_url": fal_client.upload_file(file_path_2),
|
29 |
+
"cloth_type": "overall"
|
30 |
+
},
|
31 |
+
)
|
32 |
+
request_id = handler.request_id
|
33 |
+
result = fal_client.result("fal-ai/cat-vton", request_id)
|
34 |
+
time_2 = time.time()
|
35 |
+
cat_time = time_2 - time_1
|
36 |
+
print(f"cat_time:{cat_time}")
|
37 |
+
time_1 = time.time()
|
38 |
+
response = requests.get(result['image']['url'])
|
39 |
+
time_2 = time.time()
|
40 |
+
url_time = time_2 - time_1
|
41 |
+
print(f"url_time:{url_time}")
|
42 |
+
time_1 = time.time()
|
43 |
+
save_directory = "downloads"
|
44 |
+
if response.status_code == 200:
|
45 |
+
filename = os.path.join(save_directory, f"cat-vton_{index}.png")
|
46 |
+
with open(filename, 'wb') as f:
|
47 |
+
f.write(response.content)
|
48 |
+
else:
|
49 |
+
print(f"Failed to download image from {result['image']['url']}")
|
50 |
+
time_2 = time.time()
|
51 |
+
downloads_time = time_2 - time_1
|
52 |
+
print(f"downloads_time:{downloads_time}")
|
53 |
+
return os.path.join(save_directory, f"cat-vton_{index}.png")
|
54 |
+
|
55 |
+
|
56 |
+
def user_dress_idm(user_pic, cloth_gen, index):
|
57 |
+
time_1 = time.time()
|
58 |
+
filename_1 = 'user_image.jpg'
|
59 |
+
filename_2 = 'cloth_image.jpg'
|
60 |
+
file_path_1 = os.path.join(UPLOAD_FOLDER, filename_1)
|
61 |
+
file_path_2 = os.path.join(UPLOAD_FOLDER, filename_2)
|
62 |
+
Image.fromarray(user_pic).save(file_path_1)
|
63 |
+
Image.fromarray(cloth_gen).save(file_path_2)
|
64 |
+
time_2 = time.time()
|
65 |
+
save_time = time_2 - time_1
|
66 |
+
print(f"save_time:{save_time}")
|
67 |
+
time_1 = time.time()
|
68 |
+
handler = fal_client.submit(
|
69 |
+
"fal-ai/idm-vton",
|
70 |
+
arguments={
|
71 |
+
"human_image_url": fal_client.upload_file(file_path_1),
|
72 |
+
"garment_image_url": fal_client.upload_file(file_path_2),
|
73 |
+
"description": "long-sleeved long coat"
|
74 |
+
},
|
75 |
+
)
|
76 |
+
request_id = handler.request_id
|
77 |
+
result = fal_client.result("fal-ai/idm-vton", request_id)
|
78 |
+
time_2 = time.time()
|
79 |
+
cat_time = time_2 - time_1
|
80 |
+
print(f"idm_time:{cat_time}")
|
81 |
+
time_1 = time.time()
|
82 |
+
response = requests.get(result['image']['url'])
|
83 |
+
time_2 = time.time()
|
84 |
+
url_time = time_2 - time_1
|
85 |
+
print(f"url_time:{url_time}")
|
86 |
+
time_1 = time.time()
|
87 |
+
save_directory = "downloads"
|
88 |
+
if response.status_code == 200:
|
89 |
+
filename = os.path.join(save_directory, f"idm-vton_{index}.png")
|
90 |
+
with open(filename, 'wb') as f:
|
91 |
+
f.write(response.content)
|
92 |
+
else:
|
93 |
+
print(f"Failed to download image from {result['image']['url']}")
|
94 |
+
time_2 = time.time()
|
95 |
+
downloads_time = time_2 - time_1
|
96 |
+
print(f"downloads_time:{downloads_time}")
|
97 |
+
return os.path.join(save_directory, f"idm-vton_{index}.png")
|
98 |
+
|
99 |
+
|
100 |
+
def user_cloths(user_pic, cloth_gen):
|
101 |
+
user_cloth = []
|
102 |
+
for i in range(1, 7):
|
103 |
+
user_cloth.append(user_dress_cat(user_pic, cloth_gen, i))
|
104 |
+
# for i in range(4, 6):
|
105 |
+
# user_cloth.append(user_dress_idm(user_pic, cloth_gen, i))
|
106 |
+
return user_cloth
|