This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
forked from akshaybhatia10/DeepClassifyML
-
Notifications
You must be signed in to change notification settings - Fork 6
/
server_new.py
executable file
·95 lines (79 loc) · 2.82 KB
/
server_new.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from keras.applications import inception_v3,imagenet_utils
from keras.models import load_model
import cv2
import numpy as np
from flask import Flask, request, make_response,jsonify
import numpy as np
import json
import urllib.request
from urllib.request import Request, urlopen
import base64
import numpy as np
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import logging
model = None
app = Flask(__name__,static_url_path='')
def preprocess_img(img,target_size=(299,299)):
if (img.shape[2] == 4):
img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
img = cv2.resize(img,target_size)
img = np.divide(img,255.)
img = np.subtract(img,0.5)
img = np.multiply(img,2.)
return img
def load_im_from_url(url):
requested_url = urlopen(Request(url,headers={'User-Agent': 'Mozilla/5.0'}))
image_array = np.asarray(bytearray(requested_url.read()), dtype=np.uint8)
print (image_array.shape)
print (image_array)
image_array = cv2.imdecode(image_array, -1)
image_array = np.expand_dims(image_array,axis=0)
print (image_array.shape)
return image_array
def load_im_from_system(url):
image_url = url.split(',')[1]
image_url = image_url.replace(" ", "+")
image_array = base64.b64decode(image_url)
#image_array = np.asarray(bytearray(image_array), dtype=np.uint8)
image_array = np.fromstring(image_array, np.uint8)
image_array = cv2.imdecode(image_array, -1)
return image_array
def predict(img):
img=preprocess_img(img)
print (img.shape)
global model
if model is None:
#model =inception_v3.InceptionV3()
model = load_model('model.h5')
#model.compile(optimizer='adam', loss='categorical_crossentropy')
preds = model.predict(np.array([img]))
return decode_predictions(preds)
@app.route('/classify_system', methods=['GET'])
def classify_system():
image_url = request.args.get('imageurl')
image_array = load_im_from_system(image_url)
resp = predict(image_array)
result = []
for r in resp[0]:
result.append({"class_name":r[1],"score":float(r[2])})
return jsonify({'results':result})
@app.route('/classify_url', methods=['GET'])
def classify_url():
image_url = request.args.get('imageurl')
image_array = load_im_from_url(image_url)
resp = predict(image_array)
result = []
for r in resp[0]:
result.append({"class_name":r[1],"score":float(r[2])})
return jsonify({'results':result})
@app.route('/classify-system', methods=['GET'])
def do_system():
return app.send_static_file('system.html')
@app.route('/classify-url', methods=['GET'])
def do_url():
return app.send_static_file('url.html')
@app.route('/', methods=['GET'])
def root():
return app.send_static_file('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)