-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
92 lines (69 loc) · 2.81 KB
/
server.py
File metadata and controls
92 lines (69 loc) · 2.81 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
# Importing all the libraries for the server.
from flask import Flask, jsonify, abort, request, make_response, url_for
from flask_cors import CORS, cross_origin
from flask_uploads import UploadSet, configure_uploads, IMAGES
import tensorflow as tf
import numpy as np
import os, glob, cv2
import sys,argparse
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = '*'
# Setting the directory for the uploaded images.
photos = UploadSet('photos', IMAGES)
app.config['UPLOADED_PHOTOS_DEST'] = '.\static\img'
configure_uploads(app, photos)
@cross_origin()
# The function that gets the requests from my front end and prepares it for prediction.
@app.route('/classify/', methods=['POST'])
def hello():
json = {}
print(request.files)
if 'image' in request.files:
filename = photos.save(request.files['image'])
path = app.config['UPLOADED_PHOTOS_DEST'] + '\\' + filename
print(path)
return jsonify(str(score(path))), 201
# Function that scores the images based on the trained model.
def score(image_path):
image_path=image_path
filename = image_path
image_size=128
num_channels=3
images = []
# Reading and resizing the images.
image = cv2.cv2.imread(filename)
image = cv2.cv2.resize(image, (image_size, image_size),0,0, cv2.cv2.INTER_LINEAR)
images.append(image)
images = np.array(images, dtype=np.uint8)
images = images.astype('float32')
images = np.multiply(images, 1.0/255.0)
x_batch = images.reshape(1, image_size,image_size,num_channels)
sess = tf.Session()
# Importing the trained model.
saver = tf.train.import_meta_graph('image-model.meta')
saver.restore(sess, tf.train.latest_checkpoint('./'))
# Predicting the uploaded image using the graph of the model.
graph = tf.get_default_graph()
y_pred = graph.get_tensor_by_name("y_pred:0")
x = graph.get_tensor_by_name("x:0")
y_true = graph.get_tensor_by_name("y_true:0")
y_test_images = np.zeros((1, 5))
feed_dict_testing = {x: x_batch, y_true: y_test_images}
result=sess.run(y_pred, feed_dict=feed_dict_testing)
# Returning the results based on the probabilities.
for i in result:
result = ""
if np.argmax(i) == 0:
result = "This image is category (1), the classifier is "
elif np.argmax(i) == 1:
result = "This image is category (2), the classifier is"
elif np.argmax(i) == 2:
result = "This image is category (3), the classifier is"
elif np.argmax(i) == 3:
result = "This image is category (4), the classifier is"
elif np.argmax(i) == 4:
result = "This image is category (5), lmao, the classifier is"
response = result + " " + str(np.max(i) * 100) + "% sure"
return response
app.run()