File size: 2,512 Bytes
d4eeee5
 
e0745e9
 
 
d4eeee5
e0745e9
 
 
961e329
e0745e9
 
 
 
 
d4eeee5
e0745e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e733b3
e0745e9
 
 
6c84255
99db137
6c84255
99db137
e0745e9
99db137
d4eeee5
 
ab96db1
cbcc76e
99db137
2fd0b7b
 
 
 
99db137
38afa13
bd9e79b
75876f1
bd9e79b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import gradio as gr
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image


#Load the model
model = load_model("keras_model.h5", compile=False)

# Load the labels
class_names = open("labels.txt", "r").readlines()


def predict_image(img):
    # Resize the image
    img = Image.fromarray(img.astype('uint8'), 'RGB')
    img = img.resize((224, 224), resample=Image.BILINEAR)

    # Preprocess the image
    img_array = image.img_to_array(img)
    img_array = preprocess_input(img_array)

    # Expand the dimensions to create a batch of size 1
    img_batch = tf.expand_dims(img_array, axis=0)

    # Predict the class probabilities
    preds = model.predict(img_batch)
    class_idx = tf.argmax(preds, axis=1)[0]
    class_name = class_names[class_idx].strip()
    confidence_score = float(preds[0][class_idx])  #convert to float
    
    if class_idx == 5:
        return "We couldn't detect anything from this image. Please try with a different image."
    elif confidence_score >= 0.70:
        return f"There is a {confidence_score*100:.2f}% chance for this image to be in {class_name}. Even though it has good accuracy, please consult a doctor for confirmation."
    elif 0.50 <= confidence_score < 0.70:
        return f"There is a {confidence_score*100:.2f}% chance for this image to be in {class_name}, but considering the accuracy, it's better to consult a doctor before using our service."
    else:
        return f"There is a {confidence_score*100:.2f}% chance for this image to be in {class_name}. Since the accuracy is very low, please consider a doctor's advice and we recommend you not to rely on our predictions."


# Launch the Gradio interfac
iface = gr.Interface(fn=predict_image, inputs="image", outputs="text", title="Bee4Med - Skin Disease Classifier",
                     description="""This is a machine learning model that predicts skin disease from an image(limited dataset). Which is 
-->Acne and Rosacea category
-->Eczema(most probably atopic dermatitis) category
-->Bullous Disease category
-->Eczema category
-->Alopecia, Fungus, and other Nail Diseases category
However, please note that there are chances that the predictions may go wrong, and we strongly recommend you to consult a doctor for confirmation. Please provide a closer pic for better accuracy""")

# Launch the interface
iface.launch()