Skip to content

Commit 390f018

Browse files
Added building blocks for API
1 parent a04b2af commit 390f018

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

app.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!../v2/v2venv/bin/python
2+
from flask import Flask, request, abort
3+
import torch
4+
5+
from transfer import *
6+
from demo_images import load_image_as_tensor, style_transfer
7+
8+
app = Flask(__name__)
9+
10+
# model_path = 'models/state_dict_WAVEWORKING_stylecontent.pth'
11+
model_path = 'models/state_dict_STARWORKING_contentandstyle.pth'
12+
t = Transfer(10,
13+
'./video/',
14+
'./examples/style_img/wave.png',
15+
'/home/tfm/.torch/models/vgg19-dcbb9e9d.pth',
16+
1e-3,
17+
1e5, 1e7, 0, 1e-8)
18+
# load style
19+
t.style_net.load_state_dict(torch.load(model_path, map_location='cpu'))
20+
# some params
21+
width = 640
22+
height = 360
23+
24+
25+
@app.route('/')
26+
def index():
27+
return "Hello, World!"
28+
29+
30+
@app.route('/test', methods=['GET'])
31+
def test():
32+
print(request.args)
33+
if 'type' in request.args:
34+
typ = request.args['type']
35+
if typ == 'data_path':
36+
return t.data_path
37+
if typ == 'style_net':
38+
return str(t.style_net)
39+
return 'nope'
40+
41+
42+
@app.route('/img')
43+
def img():
44+
if 'img_path' not in request.args:
45+
return abort(404)
46+
img_path = request.args['img_path']
47+
frame = load_image_as_tensor(img_path, scale=2)
48+
if t.gpu:
49+
frame = frame.cuda()
50+
# get processed image
51+
output = style_transfer(frame, t)
52+
output = (1 + output)/2
53+
save_image(output, '../imfolder/results/fromapi_out.jpg')
54+
save_image(frame, '../imfolder/results/fromapi_in.jpg')
55+
return img_path
56+
57+
58+
@app.route('/imglive')
59+
def imglive():
60+
# WIP
61+
return False
62+
63+
64+
if __name__ == '__main__':
65+
app.run(debug=True)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ requests==2.22.0
66
torch==1.0.0
77
torchvision==0.2.2
88
urllib3==1.25.3
9+
Flask==1.1.1

0 commit comments

Comments
 (0)