-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
53 lines (38 loc) · 1.27 KB
/
app.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
import base64
import io
from io import BytesIO
import requests as req
from PIL import Image
from flask import Flask, request, render_template
import efficientService as service
# flask web service
app = Flask(__name__, template_folder="web")
@app.route('/detect')
def detect():
return render_template('detect.html')
@app.route('/index')
def index():
return render_template('index.html')
@app.route('/detect/imageDetect', methods=['post'])
def upload():
# step 1. receive image
file = request.form.get('imageBase64Code')
image_link = request.form.get("imageLink")
if image_link:
response = req.get(image_link)
image = Image.open(BytesIO(response.content))
else:
image = Image.open(BytesIO(base64.b64decode(file)))
# step 2. detect image
image_array = service.detect(image)
# step 3. convert image_array to byte_array
img = Image.fromarray(image_array, 'RGB')
img_byte_array = io.BytesIO()
img.save(img_byte_array, format='JPEG')
# step 4. return image_info to page
image_info = base64.b64encode(img_byte_array.getvalue()).decode('ascii')
return image_info
if __name__ == '__main__':
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug=False, port=8081)