-
Notifications
You must be signed in to change notification settings - Fork 0
/
GauganSend.py
90 lines (74 loc) · 2.73 KB
/
GauganSend.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests,re, shutil, os, base64, random, string
from datetime import date
import concurrent.futures
import itertools
from more_itertools import grouper
today = date.today()
style = 2
indir = ".\in"
outdir = '.\out'
def getUrl():
print('Getting new server address...')
r = requests.get('http://gaugan.org/gaugan2/demo.js')
# Regular expression to match the specified format
pattern = re.compile(r'http://ec2-\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}\.us-west-2\.compute\.amazonaws\.com:443/')
match = pattern.search(r.text)
if match:
return match.group()
else:
return None
def get_image(img):
# Prints the name of the image being processed
print(f'Processing image \'{img}\'')
# Create a session object
session = requests.Session()
# Open the image file and read it
with open(os.path.join(indir, img), "rb") as f:
# Encode the image file in base64 format
imgb64 = 'data:image/png;base64,' + str(base64.b64encode(f.read()))[2:-1]
# Create a unique name for the image using the current date, random digits
name = today.strftime("%d/%m/%Y") + "," + ''.join(random.choice(string.digits) for _ in range(13)) + "-" + ''.join(random.choice(string.digits) for _ in range(9))
# Create a data payload for the POST request
POSTdata = {
'name': name,
'masked_segmap': imgb64,
'style_name' : str(style),
'caption' : '',
'enable_seg' : 'true',
'enable_edge' : 'false',
'enable_caption' : 'false',
'enable_image' : 'false',
'use_model2' : 'false',
}
# Send the POST request with the data payload to the specified URL
session.post(url + 'gaugan2_infer', data = POSTdata)
# Prepare another data payload for another POST request
POSTdata = {
'name': name,
}
# Print the URL for the second POST request
print(url + 'gaugan2_receive_output')
# Send the POST request and get the response in stream mode
r = session.post(url + 'gaugan2_receive_output', data = POSTdata, stream = True)
# Open a new file with the output directory, original filename and jpg extension
with open(os.path.join(outdir, img.split('.')[0] + '.jpg'),'wb') as f:
# Copy the response in the new file
shutil.copyfileobj(r.raw, f)
# close the session
session.close()
def main(items):
for img in items:
get_image(img)
######################
def execute():
items = os.listdir(indir)
executor = concurrent.futures.ProcessPoolExecutor(5)
groups = grouper(5, items, fillvalue=None)
futures = [executor.submit(main, group) for group in groups]
return concurrent.futures.wait(futures, timeout=None)
def grouper(n, iterable, fillvalue=None):
args = [iter(iterable)] * n
return itertools.zip_longest(fillvalue=fillvalue, *args)
url = getUrl()
if __name__ == "__main__":
execute()