from flask import Flask, redirect, url_for, request, render_template, send_from_directory import urllib.request import numpy as np from PIL import Image import requests from predict import predict_image, load_model_h5, get_class_names import json, io ,gc, random import sys, os, cv2 from flask_caching import Cache from flask_cors import CORS from authhelper import * api = Flask(__name__) api.debug = True CORS(api) import keras import tensorflow as tf #collected = gc.collect() os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' global graph, model graph = tf.get_default_graph() def get_session_class(): config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.6 config.gpu_options.allow_growth = True return tf.Session(config=config) keras.backend.tensorflow_backend.set_session(get_session_class()) model = load_model_h5('/var/www/models/model1/Concat_VGG16_64_100_0_224_adam_0.0001.h5') class_names = get_class_names('/var/www/models/model1/class_names.csv') api.route('/') def start(): return "Started" @api.route('/healthcheck') def ping(): return "ping" @api.route('/upload') def upload(): return '
Select image to upload:
' @api.route('/echo', methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']) def api_echo(): if request.method == 'GET': return "ECHO: GET\n" elif request.method == 'POST': return "ECHO: POST\n" elif request.method == 'PATCH': return "ECHO: PACTH\n" elif request.method == 'PUT': return "ECHO: PUT\n" elif request.method == 'DELETE': return "ECHO: DELETE" @api.route('/api', methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE']) @checktoken def predict(): img =['image'].stream) img = img.convert('RGB') prediction = predict_image(model, class_names, img) return json.dumps(prediction) @api.route('/class') def render(): with graph.as_default(): sess = tf.Session() if 'filename' in request.args: image_url = request.args.get('filename') ''' user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/2009021910 Firefox/3.0.7' headers = {'User-Agent':user_agent,} request_image = urllib.request.Request(image_url,None,headers) response_data = urllib.request.urlopen(request_image) img = ''' response = requests.get(image_url, stream=True).raw image_data = np.asarray(bytearray(, dtype="uint8") img = cv2.imdecode(image_data, -1) img = img.convert('RGB') prediction = predict_image(model, class_names, img) return json.dumps(prediction) sess.close() @api.route('/login', methods=['GET', 'POST']) def login_user(): auth = request.get_json() print(auth) return loginhelper(auth) if __name__ == '__main__':'', os.environ.get('PORT', 5000),debug=True, use_reloader=True)