ChrisJohnson111 commited on
Commit
b37731e
·
verified ·
1 Parent(s): fb7f5b8

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +148 -148
utils.py CHANGED
@@ -1,148 +1,148 @@
1
-
2
- import os
3
- import sys
4
- import cv2
5
- import json
6
- import random
7
- import time
8
- import requests
9
- import func_timeout
10
- import numpy as np
11
- import gradio as gr
12
-
13
-
14
- ApiUrl = os.environ['ApiUrl']
15
- OpenId = os.environ['OpenId']
16
- ApiKey = os.environ['ApiKey']
17
- OssUrl = os.environ['OssUrl']
18
- Regions = os.environ['Regions']
19
-
20
-
21
- proj_dir = os.path.dirname(os.path.abspath(__file__))
22
- data_dir = os.path.join(proj_dir, 'Datas')
23
- # data_dir = "Datas"
24
- tmpFolder = "tmp"
25
- os.makedirs(tmpFolder, exist_ok=True)
26
-
27
-
28
- def get_cloth_examples():
29
- cloth_dir = os.path.join(data_dir, 'ClothImgs')
30
- examples = []
31
- files = sorted(os.listdir(cloth_dir))
32
- # files = sorted(os.listdir(cloth_dir))[::-1]
33
- for f in files:
34
- cloth_id = f
35
- cloth_path = os.path.join(cloth_dir, f)
36
- examples.append([cloth_id, cloth_path])
37
- examples = examples[::-1]
38
- return examples
39
-
40
- def get_pose_examples():
41
- pose_dir = os.path.join(data_dir, 'PoseImgs')
42
- examples = []
43
- for f in os.listdir(pose_dir):
44
- pose_id = f
45
- pose_path = os.path.join(pose_dir, f)
46
- examples.append([pose_id, pose_path])
47
- return examples
48
-
49
- def get_result_example(cloth_id, pose_id):
50
- result_dir = os.path.join(data_dir, 'ResultImgs')
51
- res_path = os.path.join(result_dir, f"{cloth_id}_{pose_id}")
52
- return res_path
53
-
54
- def getAllFastInfs(apiUrl, openId, apiKey, clientIp):
55
- params = {'openId':openId, 'apiKey':apiKey, 'ipId':clientIp, 'page':0}
56
- session = requests.session()
57
- ret = requests.post(f"{apiUrl}/api/inf/get_batch_fast_results", data=json.dumps(params))
58
- res = []
59
- if ret.status_code==200:
60
- if 'data' in ret.json():
61
- records = ret.json()['data']['records']
62
- for record in records:
63
- res.append({'pose':OssUrl+record['bodyUrl']+"?thumbnail/768x768>",
64
- 'res':OssUrl+record['showUrl']+"?thumbnail/768x768>",
65
- 'state':int(record['state']),
66
- 'position':int(record['position'])})
67
- return res
68
-
69
- def upload_imgs(apiUrl, openId, apiKey, clientIp, cloth_image, pose_image):
70
- folder = os.path.join(tmpFolder, clientIp.replace(".", ""))
71
- os.makedirs(folder, exist_ok=True)
72
- pose_path = os.path.join(folder, 'pose.jpg')
73
- cloth_path = os.path.join(folder, 'cloth.jpg')
74
- cv2.imwrite(pose_path, pose_image[:,:,::-1])
75
- cv2.imwrite(cloth_path, cloth_image[:,:,::-1])
76
-
77
- params = {'openId':openId, 'apiKey':apiKey, 'ipId':clientIp,
78
- 'poseFileName':os.path.basename(pose_path),
79
- 'clothFileName':os.path.basename(cloth_path),
80
- 'maskFileName':''}
81
- session = requests.session()
82
- ret = requests.post(f"{apiUrl}/api/inf/fastinf_upload", data=json.dumps(params))
83
- res = 0
84
- if ret.status_code==200:
85
- if 'data' in ret.json():
86
- data = ret.json()['data']
87
- if data['cod'] in [2, 3]: return data['cod']
88
- with open(cloth_path, 'rb') as file:
89
- response = requests.put(data['clothUrl'], data=file)
90
- with open(pose_path, 'rb') as file:
91
- response = requests.put(data['poseUrl'], data=file)
92
- if os.path.exists(pose_path): os.remove(pose_path)
93
- if os.path.exists(cloth_path): os.remove(cloth_path)
94
- return data['infId']
95
- return res
96
-
97
- def publicFastSwap(apiUrl, openId, apiKey, infId, category, caption, denoise_steps):
98
- if category=="upper_cloth":
99
- category = 1
100
- elif category=="lower_cloth":
101
- category = 2
102
- elif category=="dresses":
103
- category = 3
104
- elif category=="full_body":
105
- category = 4
106
- params = {'openId':OpenId, 'apiKey':ApiKey, 'infId':infId,
107
- 'denoise_steps':int(denoise_steps), 'auto_mask':1, 'auto_crop':1,
108
- 'category':category, 'caption':caption}
109
- session = requests.session()
110
- ret = requests.post(f"{ApiUrl}/api/inf/public_fastinf", data=json.dumps(params))
111
- if ret.status_code==200:
112
- if 'data' in ret.json():
113
- """
114
- [Success] An example returns the result
115
- {'code': 200, 'msg': 'ok', 'data': True}
116
- """
117
- print('public task successfully!')
118
- return ret.json()['data']
119
-
120
- def getFastInfRes(apiUrl, openId, apiKey, infId):
121
- params = {'openId':openId, 'apiKey':apiKey, 'infId':infId}
122
- session = requests.session()
123
- ret = requests.get(f"{apiUrl}/api/inf/get_fast_result", params=params)
124
- if ret.status_code==200:
125
- if 'data' not in ret.json():
126
- return 0
127
- return ret.json()['data']
128
- else:
129
- return 0
130
-
131
- @func_timeout.func_set_timeout(10)
132
- def check_func(ip):
133
- session = requests.session()
134
- ret = requests.get(f"https://webapi-pc.meitu.com/common/ip_location?ip={ip}")
135
- for k in ret.json()['data']:
136
- nat = ret.json()['data'][k]['nation']
137
- if nat.lower() in Regions.lower():
138
- print(nat, 'invalid')
139
- return False
140
- else:
141
- print(nat, 'valid')
142
- return True
143
- def check_warp(ip):
144
- try:
145
- return check_func(ip)
146
- except Exception as e:
147
- print(e)
148
- return True
 
1
+
2
+ import os
3
+ import sys
4
+ import cv2
5
+ import json
6
+ import random
7
+ import time
8
+ import requests
9
+ import func_timeout
10
+ import numpy as np
11
+ import gradio as gr
12
+
13
+
14
+ ApiUrl = os.environ['ApiUrl']
15
+ OpenId = os.environ['OpenId']
16
+ ApiKey = os.environ['ApiKey']
17
+ OssUrl = os.environ['OssUrl']
18
+ Regions = os.environ['Regions']
19
+
20
+
21
+ proj_dir = os.path.dirname(os.path.abspath(__file__))
22
+ data_dir = os.path.join(proj_dir, 'Datas')
23
+ # data_dir = "Datas"
24
+ tmpFolder = "tmp"
25
+ os.makedirs(tmpFolder, exist_ok=True)
26
+
27
+
28
+ def get_cloth_examples():
29
+ cloth_dir = os.path.join(data_dir, 'ClothImgs')
30
+ examples = []
31
+ files = sorted(os.listdir(cloth_dir))
32
+ # files = sorted(os.listdir(cloth_dir))[::-1]
33
+ for f in files:
34
+ cloth_id = f
35
+ cloth_path = os.path.join(cloth_dir, f)
36
+ examples.append([cloth_id, cloth_path])
37
+ examples = examples[::-1]
38
+ return examples
39
+
40
+ def get_pose_examples():
41
+ pose_dir = os.path.join(data_dir, 'PoseImgs')
42
+ examples = []
43
+ for f in os.listdir(pose_dir):
44
+ pose_id = f
45
+ pose_path = os.path.join(pose_dir, f)
46
+ examples.append([pose_id, pose_path])
47
+ return examples
48
+
49
+ def get_result_example(cloth_id, pose_id):
50
+ result_dir = os.path.join(data_dir, 'ResultImgs')
51
+ res_path = os.path.join(result_dir, f"{cloth_id}_{pose_id}")
52
+ return res_path
53
+
54
+ def getAllFastInfs(apiUrl, openId, apiKey, clientIp):
55
+ params = {'openId':openId, 'apiKey':apiKey, 'ipId':clientIp, 'page':0}
56
+ session = requests.session()
57
+ ret = requests.post(f"{apiUrl}/api/inf/get_batch_fast_results", data=json.dumps(params))
58
+ res = []
59
+ if ret.status_code==200:
60
+ if 'data' in ret.json():
61
+ records = ret.json()['data']['records']
62
+ for record in records:
63
+ res.append({'pose':OssUrl+record['bodyUrl']+"?thumbnail/768x768>",
64
+ 'res':OssUrl+record['showUrl']+"?thumbnail/768x768>",
65
+ 'state':int(record['state']),
66
+ 'position':int(record['position'])})
67
+ return res
68
+
69
+ def upload_imgs(apiUrl, openId, apiKey, clientIp, cloth_image, pose_image):
70
+ folder = os.path.join(tmpFolder, clientIp.replace(".", ""))
71
+ os.makedirs(folder, exist_ok=True)
72
+ pose_path = os.path.join(folder, 'pose.jpg')
73
+ cloth_path = os.path.join(folder, 'cloth.jpg')
74
+ cv2.imwrite(pose_path, pose_image[:,:,::-1])
75
+ cv2.imwrite(cloth_path, cloth_image[:,:,::-1])
76
+
77
+ params = {'openId':openId, 'apiKey':apiKey, 'ipId':'',
78
+ 'poseFileName':os.path.basename(pose_path),
79
+ 'clothFileName':os.path.basename(cloth_path),
80
+ 'maskFileName':''}
81
+ session = requests.session()
82
+ ret = requests.post(f"{apiUrl}/api/inf/fastinf_upload", data=json.dumps(params))
83
+ res = 0
84
+ if ret.status_code==200:
85
+ if 'data' in ret.json():
86
+ data = ret.json()['data']
87
+ if data['cod'] in [2, 3]: return data['cod']
88
+ with open(cloth_path, 'rb') as file:
89
+ response = requests.put(data['clothUrl'], data=file)
90
+ with open(pose_path, 'rb') as file:
91
+ response = requests.put(data['poseUrl'], data=file)
92
+ if os.path.exists(pose_path): os.remove(pose_path)
93
+ if os.path.exists(cloth_path): os.remove(cloth_path)
94
+ return data['infId']
95
+ return res
96
+
97
+ def publicFastSwap(apiUrl, openId, apiKey, infId, category, caption, denoise_steps):
98
+ if category=="upper_cloth":
99
+ category = 1
100
+ elif category=="lower_cloth":
101
+ category = 2
102
+ elif category=="dresses":
103
+ category = 3
104
+ elif category=="full_body":
105
+ category = 4
106
+ params = {'openId':OpenId, 'apiKey':ApiKey, 'infId':infId,
107
+ 'denoise_steps':int(denoise_steps), 'auto_mask':1, 'auto_crop':1,
108
+ 'category':category, 'caption':caption}
109
+ session = requests.session()
110
+ ret = requests.post(f"{ApiUrl}/api/inf/public_fastinf", data=json.dumps(params))
111
+ if ret.status_code==200:
112
+ if 'data' in ret.json():
113
+ """
114
+ [Success] An example returns the result
115
+ {'code': 200, 'msg': 'ok', 'data': True}
116
+ """
117
+ print('public task successfully!')
118
+ return ret.json()['data']
119
+
120
+ def getFastInfRes(apiUrl, openId, apiKey, infId):
121
+ params = {'openId':openId, 'apiKey':apiKey, 'infId':infId}
122
+ session = requests.session()
123
+ ret = requests.get(f"{apiUrl}/api/inf/get_fast_result", params=params)
124
+ if ret.status_code==200:
125
+ if 'data' not in ret.json():
126
+ return 0
127
+ return ret.json()['data']
128
+ else:
129
+ return 0
130
+
131
+ @func_timeout.func_set_timeout(10)
132
+ def check_func(ip):
133
+ session = requests.session()
134
+ ret = requests.get(f"https://webapi-pc.meitu.com/common/ip_location?ip={ip}")
135
+ for k in ret.json()['data']:
136
+ nat = ret.json()['data'][k]['nation']
137
+ if nat.lower() in Regions.lower():
138
+ print(nat, 'invalid')
139
+ return False
140
+ else:
141
+ print(nat, 'valid')
142
+ return True
143
+ def check_warp(ip):
144
+ try:
145
+ return check_func(ip)
146
+ except Exception as e:
147
+ print(e)
148
+ return True