-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (48 loc) · 1.61 KB
/
app.py
File metadata and controls
59 lines (48 loc) · 1.61 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
import os
import time
import base64
from PIL import Image
from flask import Flask, request
from flask_cors import CORS
import inference
def read_image(image_data):
TAG2 = TAG + '[read_image]'
print(TAG2, '[starts]')
image_data = base64.decodebytes(image_data)
with open('temp_image.jpg', 'wb') as f:
f.write(image_data)
f.close()
image = Image.open('temp_image.jpg').convert('RGB')
os.remove('temp_image.jpg')
return image
app = Flask(__name__)
CORS(app)
TAG = f'[{__name__}]'
BASE_WORK_DIR = os.getcwd()
# This is the server function to handle requests and get images from client
@app.route('/inference', methods=['POST'])
def inference_handle():
TAG2 = TAG + '[inference]'
print(TAG2, '[starts]')
# check for valid request
if not request.json:
return 'Server Error!', 500
# STEP 1: process input image
header_len = len('data:image/jpeg;base64,')
image_data = request.json['image_data'][header_len:].encode()
classifier_name = request.json['classifier_name']
t1 = time.time()
image = read_image(image_data)
print(TAG2, '[read_image][time_taken]', time.time() - t1)
# STEP 2: perform detection
t1 = time.time()
score, class_label = inference.inference(image, classifier_name)
print(TAG2, '[inference][time_taken]', time.time() - t1)
prediction = {'score': score, 'class_label': class_label, 'status': 'Operation Completed without Error'}
print(TAG, '[prediction]\n', prediction)
return prediction, 200
@app.route('/hello')
def index():
return 'Welcome'
if __name__ == '__main__':
app.run(debug=True)