def post(self): args = parser.parse_args() image = np.array(json.loads(args['image'])) image = process_image(image) with graph.as_default(): prediction = model.predict(image) return prediction.tolist()[0]
def init_input(): image = process_new(model_params, False) with graph.as_default(): preds = model.predict(image) preds = preds[0] preds = [round(val, 2) for val in preds[:3]] image_loc = "../static/images/demo2.jpg" return render_template("pages/input.html", image=image_loc, preds=preds)
def classify_image(image): """Call ResXception to return emotion probabilities from Keras model. @param image: RGB cropped face image @return: map from emotion to probability """ nn_input_shape = nn.input_shape[1:3] gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) gray_image = cv2.resize(gray_image, (nn_input_shape)) gray_image = clean_image(gray_image) gray_image = np.expand_dims(gray_image, 0) gray_image = np.expand_dims(gray_image, -1) with graph.as_default(): emotion_proba = nn.predict(gray_image) emotion_map = index_to_emotion(emotion_proba[0].tolist()) return emotion_map
def upload_image(): if request.method == 'POST': if request.files: image = request.files['imgInp'] if allow_image(image.filename, ["JPEG", "JPG", "PNG"]): # Image preprocessing and classification. global sess global graph global new_model test_img = process_img(image, 150, 32) with graph.as_default(): set_session(sess) prediction = new_model.predict(test_img, verbose=1)[0][0] prob = 100 * round(prediction, 3) classes = ['Normal', 'Pneumonia'] pred_class = classes[int(round(prediction))] c = [100 - prob, prob] imagem = get_encoded_image(c) # Memory release gc.collect() # The result.html page will be rendered with 3 objects: # - The class of uploaded image; # - Probability of 'pneumonia' class; # - Generated donut plot containing the class probabilities within the HTML code. return render_template( 'result.html', classification=pred_class, probability=prob, img_code=Markup( '<img src=data:image/png;base64,{} style="height:250px; width:auto;" alt="Responsive image">' .format(imagem))) else: # In case file is no choosen or file format is invalid. feedback = "File format not allowed." return render_template("diagnosis.html", feedback=feedback) return render_template('diagnosis.html')
def upload_file(): if request.method == 'POST': f = request.files['file'] path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename) f.save(path) img = image.load_img(path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = mobilenet.preprocess_input(x) global graph with graph.as_default(): prediction = model.predict(x) results = imagenet_utils.decode_predictions(prediction) rst = results[0][0][1] return render_template('uploaded.html', title='Success', predictions=rst, user_image=f.filename)
def download(): url = request.args['url'] filename = request.args.get('filename', 'image.png') r = requests.get(url) imgnamedate = datetime.datetime.now().strftime("%Y%m%d%H%M%S") with app.open_instance_resource(imgnamedate + '.jpg', 'wb') as f: f.write(r.content) imagepath = app.open_instance_resource(imgnamedate + '.jpg') img = image.load_img(imagepath, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = mobilenet.preprocess_input(x) global graph with graph.as_default(): prediction = model.predict(x) prediction = model.predict(x) results = imagenet_utils.decode_predictions(prediction) userimage = imgnamedate + '.jpg' rst = results[0][0][1] return render_template('uploaded.html', title='Success', predictions=rst, user_image=userimage)