Upload Medical Image
Analyzing image with AI models...
This may take a few momentsAnalysis Results
Upload an image to see analysis results
# from flask import Flask, render_template, request # import os # from tensorflow.keras.models import load_model # from tensorflow.keras.preprocessing.image import load_img, img_to_array # import numpy as np # # Flask App # app = Flask(__name__) # # Paths to Saved Models # MOBILENET_MODEL_PATH = "model/mobilenet_oral_cancer.h5" # RESNET_MODEL_PATH = "model/resnet152v2_oral_cancer.h5" # # Load Models # mobilenet_model = load_model(MOBILENET_MODEL_PATH) # resnet_model = load_model(RESNET_MODEL_PATH) # # Image Preprocessing Parameters # IMG_SIZE = (224, 224) # def preprocess_image(image_path): # image = load_img(image_path, target_size=IMG_SIZE) # image_array = img_to_array(image) / 255.0 # Normalize # image_array = np.expand_dims(image_array, axis=0) # Add batch dimension # return image_array # @app.route('/') # def index(): # return render_template('index.html') # @app.route('/predict', methods=['POST']) # def predict(): # if 'image' not in request.files: # return render_template('index.html', prediction="No file uploaded.") # file = request.files['image'] # if file.filename == '': # return render_template('index.html', prediction="No file selected.") # # Save uploaded image # upload_path = os.path.join('static', file.filename) # file.save(upload_path) # # Preprocess the image # image_array = preprocess_image(upload_path) # # Get predictions # mobilenet_pred = mobilenet_model.predict(image_array)[0][0] # resnet_pred = resnet_model.predict(image_array)[0][0] # mobilenet_result = 'Cancer' if mobilenet_pred > 0.5 else 'Non-Cancer' # resnet_result = 'Cancer' if resnet_pred > 0.5 else 'Non-Cancer' # # Prepare prediction result # prediction = { # 'mobilenet': f"MobileNet Prediction: {mobilenet_result} (Confidence: {mobilenet_pred:.2f})", # 'resnet': f"ResNet152V2 Prediction: {resnet_result} (Confidence: {resnet_pred:.2f})", # 'image_path': upload_path # } # return render_template('index.html', prediction=prediction) # if __name__ == '__main__': # app.run(debug=True) #------------- from flask import Flask, render_template, request, jsonify import os import base64 from tensorflow.keras.models import load_model from tensorflow.keras.preprocessing.image import load_img, img_to_array import numpy as np from datetime import datetime import json import sys # Set UTF-8 encoding for Windows compatibility if sys.platform.startswith('win'): import codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer) sys.stderr = codecs.getwriter('utf8')(sys.stderr.buffer) # Flask App app = Flask(__name__) # Paths to Saved Models MOBILENET_MODEL_PATH = "mobilenet_oral_cancer.h5" RESNET_MODEL_PATH = "resnet152v2_oral_cancer.h5" # Load Models mobilenet_model = load_model(MOBILENET_MODEL_PATH) resnet_model = load_model(RESNET_MODEL_PATH) # Image Preprocessing Parameters IMG_SIZE = (224, 224) # Create directories if they don't exist os.makedirs('static/uploads', exist_ok=True) os.makedirs('templates', exist_ok=True) def preprocess_image(image_path): image = load_img(image_path, target_size=IMG_SIZE) image_array = img_to_array(image) / 255.0 # Normalize image_array = np.expand_dims(image_array, axis=0) # Add batch dimension return image_array def get_risk_level(confidence): """Determine risk level based on confidence score""" if confidence >= 0.8: return "HIGH", "#e74c3c" elif confidence >= 0.6: return "MODERATE", "#f39c12" elif confidence >= 0.4: return "LOW", "#f1c40f" else: return "MINIMAL", "#27ae60" def generate_recommendations(prediction_result, mobilenet_conf, resnet_conf): """Generate medical recommendations based on predictions""" recommendations = [] if prediction_result == "Cancer": recommendations = [ "Immediate consultation with an oral pathologist is recommended", "Consider scheduling a biopsy for definitive diagnosis", "Avoid tobacco and alcohol consumption", "Maintain excellent oral hygiene", "Schedule regular follow-up appointments" ] else: recommendations = [ "Continue regular oral health check-ups", "Maintain good oral hygiene practices", "Avoid risk factors like tobacco and excessive alcohol", "Consider annual oral cancer screenings", "Consult a dentist if you notice any changes" ] return recommendations @app.route('/') def index(): # Generate the advanced HTML template html_template = """
Advanced AI-Powered Medical Imaging Analysis
Analyzing image with AI models...
This may take a few momentsUpload an image to see analysis results