-
Notifications
You must be signed in to change notification settings - Fork 9
/
run_server.py
70 lines (49 loc) · 1.71 KB
/
run_server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Run this file on server to return a Concentration Index (CI).
# Analysis is in 'Util' folder.
import base64
import io
import sys
import cv2
import numpy as np
from flask import (Flask, Response, jsonify, json, make_response, render_template,
request, send_file, send_from_directory)
from flask_cors import CORS, cross_origin
from util.analysis_server import analysis
from PIL import Image
from io import BytesIO
# from StringIO import StringIO
sys.path.append("../")
# from VideoCap import VideoCap
app = Flask(__name__)
cors = CORS(app, resources={r'/*': {"origins": '*'}})
app.config['CORS_HEADER'] = 'Content-Type'
threadDict = {}
ana = analysis()
def stringToImage(base64_string):
imgdata = base64.b64decode(base64_string)
return Image.open(io.BytesIO(imgdata))
def toRGB(image):
return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
def readb64(base64_string):
sbuf = BytesIO()
sbuf.write(base64.b64decode(base64_string))
pimg = Image.open(sbuf)
return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
def data_uri_to_cv2_img(uri):
# encoded_data = uri.split(',')[1]
nparr = np.fromstring(uri.decode('base64'), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return img
@app.route('/api/v1/predict/', methods=['POST'])
@cross_origin(origin='*', headers=['Content-Type'])
def predictImage():
data = request.get_json(force=True)
datainString = data.get('image')
img = readb64(datainString)
cv2.imshow("test", img)
returnData = ana.detect_face(img)
response = make_response(returnData)
response.headers.set('Content-Type', 'application/json')
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)