-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
77 lines (54 loc) · 2.03 KB
/
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
71
72
73
74
75
76
77
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 7 03:17:34 2020
@author: kazzastic
"""
from flask import Flask, render_template, request, jsonify
from werkzeug.utils import secure_filename
# import tensorflow as tf
import os
import numpy as np
import base64
import cv2
from util import convertToJpg, allowed_file
# import model functionality
'''try to keep model functionality separate from server.py,
if possible, creaete a modal.py file and import
a single function doing all the work <3,
'''
UPLOAD_FOLDER = os.path.basename('uploads')
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "6575fae36288be6d1bad40b99808e37f"
# can be any string(not empty)
classes = ["normal", "cancer"] # place it in modal.py if seems appropriate
@app.route("/", methods=["POST"])
def process_image():
image_recieved = request.get_json()
filename = secure_filename(image_recieved['filename'])
imgdata = base64.b64decode(image_recieved['data'])
if (not imgdata) and (filename == '') and (not allowed_file(filename)):
return jsonify({'message': "Invalid Image.", 'code': 400})
# code: 400 for bad request
with open(os.path.join(app.config['UPLOAD_FOLDER'],
filename), 'wb') as f:
f.write(imgdata)
# use this as image path for model
filepath_for_model = convertToJpg(app, filename)
response = {}
'''
model_response = Call_Model(filepath_for_model) #call model functionality
response['message'] = model_response #add model response to response obj
response['further_details'] = ... # add further details about result as needed
response['code'] = 200 #succesfull response
'''
response['message'] = "Model Prediction." # for sake of testing
response['code'] = 200
return jsonify(response)
@app.route("/", methods=["GET"])
def render_index():
return render_template("index.html")
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port='5000')