-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_local_server.py
More file actions
101 lines (78 loc) · 3.21 KB
/
Copy pathsetup_local_server.py
File metadata and controls
101 lines (78 loc) · 3.21 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------
# Created By : Wanying Li
# Created Date: May 15, 2017
# ---------------------------------------------------------------------------
"""
It entails how the local server at localhost:5000 is set up.
It takes a post request from localhost:5000 (where localhost:5000 is rerouted to a new URL through ngrok).
The post request contains the image data, which is then saved onto the local server and classified as 'normal' or 'infect.'
Lastly, this result is returned to the client.
"""
# ---------------------------------------------------------------------------
import os, os.path
from flask import Flask, flash, request, redirect, url_for, jsonify
import pickle
import numpy as np
import sklearn
import tensorflow as tf
import tensorflow.python.platform
from tensorflow.python.platform import gfile
def create_graph(model_path):
"""
The create_graph function loads the inception model to memory. This function should be called before
calling extract_features or extract_features_single_img.
Input:
model_path = path to inception model in protobuf form.
"""
with gfile.FastGFile(model_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def extract_features_single_img(image_path, verbose=False):
"""
The extract_features_single_img function computes the inception bottleneck feature for one single image.
Input:
image_path = directory path of the image
Output:
feature = 2-d np array in the shape of (1, 2048)
"""
feature_dimension = 2048
feature = np.empty((1, feature_dimension))
with tf.Session() as sess:
flattened_tensor = sess.graph.get_tensor_by_name('pool_3:0')
if verbose:
print('Processing %s...' % (image_path))
if not gfile.Exists(image_path):
tf.logging.fatal('File does not exist %s', image)
image_data = gfile.FastGFile(image_path,'rb').read()
feature_tmp = sess.run(flattened_tensor, {'DecodeJpeg/contents:0': image_data})
feature[:] = np.squeeze(feature_tmp)
return feature
# create a graph from the Inception V3.0 model
model_path = '/innovating_ear_infection_diagnostics/inception_dec_2015/tensorflow_inception_graph.pb'
create_graph(model_path)
# load classifier
clf = pickle.load(open('CNN_clf_binary','rb'))
# setup webserver
UPLOAD_FOLDER = '/innovating_ear_infection_diagnostics/post_test'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/predict', methods=['POST'])
def predict():
# upload user's photo to webserver and save the photo to the UPLOAD_FOLDER on the webserver
file = request.files['file']
filepath = os.path.join(UPLOAD_FOLDER,'data.JPG')
file.save(filepath)
# extraction feature of an user input image
img_input = filepath
print(img_input)
img_feature = extract_features_single_img(img_input)
# classification
y_pred = clf.predict(img_feature)
output = np.array2string(y_pred[0])
print(output)
return output
if __name__ == "__main__":
app.run()