-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (43 loc) · 1.69 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
54
from flask import Flask, render_template , request , jsonify
from PIL import Image
import os , io , sys
import numpy as np
import cv2
import base64
from yolo_detection_images import runModel
app = Flask(__name__)
############################################## THE REAL DEAL ###############################################
@app.route('/detectObject' , methods=['POST'])
def mask_image():
# print(request.files , file=sys.stderr)
file = request.files['image'].read() ## byte file
npimg = np.fromstring(file, np.uint8)
img = cv2.imdecode(npimg,cv2.IMREAD_COLOR)
######### Do preprocessing here ################
# img[img > 150] = 0
## any random stuff do here
################################################
img = runModel(img)
img = Image.fromarray(img.astype("uint8"))
rawBytes = io.BytesIO()
img.save(rawBytes, "JPEG")
rawBytes.seek(0)
img_base64 = base64.b64encode(rawBytes.read())
return jsonify({'status':str(img_base64)})
##################################################### THE REAL DEAL HAPPENS ABOVE ######################################
@app.route('/test' , methods=['GET','POST'])
def test():
print("log: got at test" , file=sys.stderr)
return jsonify({'status':'succces'})
@app.route('/')
def home():
return render_template('./index.html')
@app.after_request
def after_request(response):
print("log: setting cors" , file = sys.stderr)
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == '__main__':
app.run(debug = True)