forked from facebookresearch/pifuhd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveBackgrounds
65 lines (54 loc) · 1.75 KB
/
removeBackgrounds
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#All credit goes to the respective authors of the various parts of this software
import requests
import time
import shutil
import json
headers = {'Authorization': '**Removed**'}
file_list = ['testfiles/blah.png', 'testfiles/blah.png', 'testfiles/blah.png']
params = {
'lang': 'en',
'convert_to': 'image-backgroundremover'
}
api_url = 'https://api.backgroundremover.app/v1/convert/'
results_url = 'https://api.backgroundremover.app/v1/results/'
def download_file(url, local_filename):
with requests.get("https://api.backgroundremover.app/%s" % url, stream=True) as r:
with open(local_filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return local_filename
def convert_files(api_url, params, headers):
files = [eval(f'("files", open("{file}", "rb"))') for file in file_list]
print(files)
r = requests.post(
url=api_url,
files=files,
data=params,
headers=headers
)
return r.json()
def get_results(params):
if params.get('error'):
return params.get('error')
r = requests.post(
url=results_url,
data=params
)
data = r.json()
finished = data.get('finished')
while not finished:
if int(data.get('queue_count')) > 0:
print('queue: %s' % data.get('queue_count'))
time.sleep(5)
results = get_results(params)
print(results)
results = json.dumps(results)
if results:
break
if finished:
print(data.get('files'))
for f in data.get('files'):
print(f.get('url'))
download_file("%s" % f.get('url'), "%s" % f.get('filename'))
return {"finished": "files downloaded"}
return r.json()
get_results(convert_files(api_url, params, headers))