Commit e9316413 authored by Wijesinghe.K.H's avatar Wijesinghe.K.H

Upload New File

parent 57148937
import os
import shutil
from flask import Flask, request, jsonify
from const import OUTPUT_DIR
from detect import run
from predict import start_predict
from flask_cors import CORS
#lahiri
from image_service import predict_disease
APP_NAME = 'DME_IDENTIFIER'
app = Flask(APP_NAME)
CORS(app)
def clean_dir(save_dir):
try:
shutil.rmtree(save_dir)
print(f"Directory '{save_dir}' removed successfully.")
except OSError as e:
print(f"Error: {e}")
@app.route('/predict', methods=['POST'])
def predict():
# Check if an image was uploaded
if 'image' not in request.files:
return jsonify({'error': 'No image uploaded'})
file = request.files['image']
# Save the uploaded image to a designated directory
if file:
file = request.files['image']
image = file.read()
save_dir = 'temp'
# clean_dir(save_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
image_path = os.path.join(save_dir, "image.png") # Specify the path where you want to save the image
with open(image_path, 'wb') as image_file:
image_file.write(image)
# clean output dir befor crop
clean_dir(OUTPUT_DIR)
run()
predict_class , predicted_percentage = start_predict()
# clean output dir after crop
# clean_dir(save_dir)
# clean_dir(OUTPUT_DIR)
prediction = predict_class
prediction_label = 'DME' if prediction == 0 else 'NORMAL'
# my return jsonify({'predicted_class': prediction , "prediction_label" : prediction_label , "predicted_percentage" : predicted_percentage})
return jsonify({'time': prediction , "top" : prediction_label , "confidence" : predicted_percentage, 'image' : {'height': 1584, 'width': 2376},})
@app.route('/predict/glaucoma', methods=['POST'])
# @cross_origin(origin='*',headers=['Content- Type','Authorization'])
def predictG():
print("hihi")
# Check if an image was uploaded
if 'image' not in request.files:
print("hoooo1")
return jsonify({'error': 'No image uploaded'})
# Load and preprocess the image received from the Flask API
file = request.files['image']
image = file.read()
save_dir = 'tempG'
# clean_dir(save_dir)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# Save the image as "image.png"
image_path = os.path.join(save_dir, "image.png") # Specify the path where you want to save the image
with open(image_path, 'wb') as image_file:
image_file.write(image)
prediction = predict_disease(image)
# return jsonify({'predicted_class': prediction})
return (prediction)
if APP_NAME == 'DME_IDENTIFIER':
# app.run(debug=False)
app.run(host='0.0.0.0', port=8081, debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment