#import sys #import os #import glob #import socket #from keras.backend import GraphExecutionFunction #sys.path.append('c:/git/keras') #sys.path.append('c:/git/absl') # This upgrades all modules #py -mpip freeze | %{$_.split('==')[0]} | %{py -mpip install --upgrade $_} # installed # py -mpip install flask # py -mpip install numpy # py -mpip show numpy # py -mpip install tensorflow # py -mpip show tensorflow # py -mpip install matplotlib # c:\users\skess\appdata\local\programs\python\python39\lib\site-packages import keras from keras.preprocessing.image import ImageDataGenerator from flask import Flask, render_template, request import numpy as np import tensorflow import sys import os import io import PIL.Image #init flaskapp app = Flask(__name__) # def save_image(imgData): # with open('output.jpg','wb') as output: # output.write(imgData) def render_index(): return """ Convolutional Neural Network Hosting Platform

You have reached the main page of the Convolutional Neural Network Hosting Platform. This platform hosts the following models:resnet50,vgg16, and inception

The models can be invoked by submitting a POST request with 128,128 grayscale image in jpg format

The following are some examples

http://127.0.0.1/predict_incpetion

http://127.0.0.1/predict_vgg16

http://127.0.0.1/predict_resnet50

""" @app.route('/') def index(): return render_index() @app.route('/ping', methods=['GET','POST']) def ping(): return "Alive" # @app.route('/predict_vgg16', methods=['GET','POST']) # def predict_vgg16(): # test_image=request.get_data() # test_image = PIL.Image.open(io.BytesIO(test_image)) # test_image = test_image.convert('L') # test_array=keras.preprocessing.image.img_to_array(test_image) # batch_test_array=np.array([test_array]) # predictions=vgg16_model.predict(batch_test_array) # if type(predictions) == list: # average_prediction = sum(predictions)/len(predictions) # threshold_output = np.where(average_prediction > 0.5, 1, 0) # else : # threshold_output = np.where(predictions > 0.5, 1, 0) # response=str(predictions)+'-->'+str(threshold_output) # return response @app.route('/predict_resnet50', methods=['GET','POST']) def predict_resnet50(): print('/predict_resnet50') test_image=request.get_data() test_image = PIL.Image.open(io.BytesIO(test_image)) test_image = test_image.convert('L') test_array=keras.preprocessing.image.img_to_array(test_image) batch_test_array=np.array([test_array]) predictions=resnet50_model.predict(batch_test_array) if type(predictions) == list: average_prediction = sum(predictions)/len(predictions) threshold_output = np.where(average_prediction > 0.5, 1, 0) else : threshold_output = np.where(predictions > 0.5, 1, 0) response=str(predictions)+'-->'+str(threshold_output) return response @app.route('/predict_lenet5', methods=['GET','POST']) def predict_lenet5(): print('/predict_lenet5') test_image=request.get_data() test_image = PIL.Image.open(io.BytesIO(test_image)) test_image = test_image.convert('L') test_array=keras.preprocessing.image.img_to_array(test_image) batch_test_array=np.array([test_array]) predictions=lenet_model.predict(batch_test_array) if type(predictions) == list: average_prediction = sum(predictions)/len(predictions) threshold_output = np.where(average_prediction > 0.5, 1, 0) else : threshold_output = np.where(predictions > 0.5, 1, 0) response=str(predictions)+'-->'+str(threshold_output) return response if __name__ == '__main__': resnet50_model_name='../Weights/resnet50.h5' resnet50_model = keras.models.load_model(resnet50_model_name) # vgg16_model_name='../Weights/model_vgg16.h5' # vgg16_model=keras.models.load_model(vgg16_model_name) lenet_model_name='../Weights/lenet5.h5' lenet_model=keras.models.load_model(lenet_model_name) port = int(os.environ.get('PORT',5000)) app.run(host='0.0.0.0',port=port)