|
1 | 1 | #!../v2/v2venv/bin/python
|
2 |
| -from flask import Flask, request, abort |
| 2 | +from flask import Flask, request, abort, Response, make_response, send_file |
3 | 3 | import torch
|
| 4 | +import cv2 |
| 5 | +import json |
| 6 | +import jsonpickle |
| 7 | +import io |
4 | 8 |
|
5 | 9 | from transfer import *
|
6 | 10 | from demo_images import load_image_as_tensor, style_transfer
|
| 11 | +from main import reformat |
7 | 12 |
|
8 | 13 | app = Flask(__name__)
|
9 | 14 |
|
@@ -41,24 +46,43 @@ def test():
|
41 | 46 |
|
42 | 47 | @app.route('/img')
|
43 | 48 | def img():
|
| 49 | + name_id = 'x' |
44 | 50 | if 'img_path' not in request.args:
|
45 | 51 | return abort(404)
|
| 52 | + if 'id' in request.args: |
| 53 | + name_id = request.args['id'] |
46 | 54 | img_path = request.args['img_path']
|
47 | 55 | frame = load_image_as_tensor(img_path, scale=2)
|
48 | 56 | if t.gpu:
|
49 | 57 | frame = frame.cuda()
|
50 | 58 | # get processed image
|
51 | 59 | output = style_transfer(frame, t)
|
52 | 60 | 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)) |
55 | 63 | return img_path
|
56 | 64 |
|
57 | 65 |
|
58 |
| -@app.route('/imglive') |
| 66 | +@app.route('/imglive', methods=['POST']) |
59 | 67 | 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 |
62 | 86 |
|
63 | 87 |
|
64 | 88 | if __name__ == '__main__':
|
|
0 commit comments