Skip to content

Commit 49b3d0c

Browse files
committedOct 18, 2019
Upgraded API #8
1 parent 390f018 commit 49b3d0c

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed
 

‎app.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!../v2/v2venv/bin/python
2-
from flask import Flask, request, abort
2+
from flask import Flask, request, abort, Response, make_response, send_file
33
import torch
4+
import cv2
5+
import json
6+
import jsonpickle
7+
import io
48

59
from transfer import *
610
from demo_images import load_image_as_tensor, style_transfer
11+
from main import reformat
712

813
app = Flask(__name__)
914

@@ -41,24 +46,43 @@ def test():
4146

4247
@app.route('/img')
4348
def img():
49+
name_id = 'x'
4450
if 'img_path' not in request.args:
4551
return abort(404)
52+
if 'id' in request.args:
53+
name_id = request.args['id']
4654
img_path = request.args['img_path']
4755
frame = load_image_as_tensor(img_path, scale=2)
4856
if t.gpu:
4957
frame = frame.cuda()
5058
# get processed image
5159
output = style_transfer(frame, t)
5260
output = (1 + output)/2
53-
save_image(output, '../imfolder/results/fromapi_out.jpg')
54-
save_image(frame, '../imfolder/results/fromapi_in.jpg')
61+
save_image(output, '../imfolder/results/{}_fromapi_out.jpg'.format(name_id))
62+
save_image(frame, '../imfolder/results/{}_fromapi_in.jpg'.format(name_id))
5563
return img_path
5664

5765

58-
@app.route('/imglive')
66+
@app.route('/imglive', methods=['POST'])
5967
def imglive():
60-
# WIP
61-
return False
68+
h = int(request.args['h'])
69+
w = int(request.args['w'])
70+
r = request
71+
nparr = np.fromstring(r.data, np.uint8)
72+
img = nparr.reshape([h, w, 3])
73+
print(type(img), img.shape, img.min(), img.max())
74+
75+
output = torch.from_numpy(np.array([img]))
76+
output = output.type('torch.FloatTensor')
77+
output = output/255
78+
output = torch.transpose(output, 1,3)
79+
output = torch.transpose(output, 3,2)
80+
output = style_transfer(output, t)
81+
output = reformat(output)
82+
print(type(output), output.shape, output.min(), output.max())
83+
84+
response = make_response(output.tostring())
85+
return response
6286

6387

6488
if __name__ == '__main__':

‎demo_image_app.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import requests
2+
import numpy as np
3+
import time
4+
import cv2
5+
from PIL import Image
6+
7+
from demo_images import get_files_path, load_image_as_tensor
8+
"""
9+
Process images in the given folder with a given style
10+
from API
11+
"""
12+
13+
def truc():
14+
size=None
15+
scale=2
16+
keep_asp=False
17+
img = Image.open(files[i]).convert('RGB')
18+
if size is not None:
19+
if keep_asp:
20+
size2 = int(size * 1.0 / img.size[0] * img.size[1])
21+
img = img.resize((size, size2), Image.ANTIALIAS)
22+
else:
23+
img = img.resize((size, size), Image.ANTIALIAS)
24+
elif scale is not None:
25+
img = img.resize((int(img.size[0] / scale), int(img.size[1] / scale)), Image.ANTIALIAS)
26+
img = np.array(img)#.transpose(2, 0, 1)
27+
return img
28+
29+
if __name__ == '__main__':
30+
impath = '../imfolder'
31+
get_url = 'http://127.0.0.1:5000/img?img_path={}&id={}'
32+
post_url = 'http://127.0.0.1:5000/imglive'
33+
# loading IMAGES NAMES
34+
files = get_files_path(impath)
35+
36+
for i in range(len(files)):
37+
print('{}/{}'.format(i, len(files)))
38+
# t1 = time.time()
39+
# req = requests.get(get_url.format(files[i], i))
40+
# t2 = time.time()
41+
# print('{:.2f} s'.format(t2-t1))
42+
43+
img = truc()
44+
h, w, _ = img.shape
45+
print(type(img), img.shape, img.min(), img.max())
46+
47+
t1 = time.time()
48+
# h and w are quick fix for undesired image unfolding. To be changed
49+
req = requests.post(post_url+'?h={}&w={}'.format(h, w), data=img.tostring())
50+
t2 = time.time()
51+
print('REQUETE EFFECTUEE EN {:.2f} s'.format(t2-t1))
52+
53+
nparr = np.fromstring(req.content, np.uint8)
54+
# VERY dirty, that's just temporary
55+
try:
56+
imgout = nparr.reshape([h+2, w, 3])
57+
except ValueError:
58+
try:
59+
imgout = nparr.reshape([h, w, 3])
60+
except ValueError:
61+
imgout = nparr.reshape([h+1, w, 3])
62+
print(type(imgout), imgout.shape, imgout.min(), imgout.max())
63+
64+
A = Image.fromarray(img)
65+
A.save("../imfolder/results/{}testinloc.jpg".format(i))
66+
B = Image.fromarray(imgout)
67+
B.save("../imfolder/results/{}testoutloc.jpg".format(i))

0 commit comments

Comments
 (0)
Please sign in to comment.