doesntMatter11's picture
confidence scores added...
bde57ad verified
raw
history blame
2.17 kB
import json
from keras.models import load_model
from keras.optimizers import Adam
import gradio as gr
import cv2
import numpy as np
# Load the model without compiling
model = load_model('final_vgg1920epochs.h5', compile=False)
# Compile the model with the desired optimizer and learning rate
model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
# Open JSON file
with open('dat.json') as f:
data = json.load(f)
keys = list(data)
def Predict(image):
# Preprocess the image
img = cv2.resize(image, (32, 32)) / 255.0
prediction = model.predict(img.reshape(1, 32, 32, 3))
# Get the predicted class and confidence score
predicted_class_index = prediction.argmax()
predicted_class = keys[predicted_class_index]
confidence_score = prediction[0][predicted_class_index] * 100 # Convert to percentage
# Retrieve details from the JSON data
description = data[predicted_class]['description']
symptoms = data[predicted_class]['symptoms']
causes = data[predicted_class]['causes']
treatment = data[predicted_class]['treatement-1']
return (
predicted_class,
description,
symptoms,
causes,
treatment,
f"{confidence_score:.2f}%" # Format the confidence score
)
demo = gr.Interface(
fn=Predict,
inputs=gr.Image(type="numpy"), # Updated to use the new interface
outputs=[
gr.Textbox(label='Name Of Disease'),
gr.Textbox(label='Description'),
gr.Textbox(label='Symptoms'),
gr.Textbox(label='Causes'),
gr.Textbox(label='Treatment'),
gr.Textbox(label='Confidence Score')
],
title="Skin Disease Classification",
description='This Space predicts these diseases:\n \n1) Acne and Rosacea Photos. \n2) Actinic Keratosis, Basal Cell Carcinoma, and other Malignant Lesions.\n3) Eczema Photos. \n4) Melanoma Skin Cancer, Nevi, and Moles.\n5) Psoriasis pictures, Lichen Planus, and related diseases.\n6) Tinea, Ringworm, Candidiasis, and other Fungal Infections.\n7) Urticaria Hives.\n8) Nail Fungus and other Nail Diseases.\n'
)
demo.launch(debug=True)