forked from cyrildiagne/basnet-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (47 loc) · 1.4 KB
/
main.py
File metadata and controls
63 lines (47 loc) · 1.4 KB
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
import io
import os
import sys
from flask import Flask, request, send_file, jsonify
from flask_cors import CORS
from PIL import Image
import numpy as np
import time
import logging
import basnet
logging.basicConfig(level=logging.INFO)
# Initialize the Flask application
app = Flask(__name__)
CORS(app)
# Simple probe.
@app.route('/', methods=['GET'])
def hello():
return 'Hello BASNet!'
# Route http posts to this method
@app.route('/', methods=['POST'])
def run():
start = time.time()
# Convert string of image data to uint8
if 'data' not in request.files:
return jsonify({'error': 'missing file param `data`'}), 400
data = request.files['data'].read()
if len(data) == 0:
return jsonify({'error': 'empty image'}), 400
# Convert string data to PIL Image
img = Image.open(io.BytesIO(data))
# Ensure i,qge size is under 1024
if img.size[0] > 1024 or img.size[1] > 1024:
img.thumbnail((1024, 1024))
# Process Image
res = basnet.run(np.array(img))
# Save to buffer
buff = io.BytesIO()
res.save(buff, 'PNG')
buff.seek(0)
# Print stats
logging.info(f'Completed in {time.time() - start:.2f}s')
# Return data
return send_file(buff, mimetype='image/png')
if __name__ == '__main__':
os.environ['FLASK_ENV'] = 'development'
port = int(os.environ.get('PORT', 8080))
app.run(debug=True, host='0.0.0.0', port=port)