File size: 3,573 Bytes
34b2cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d41e47
 
 
 
34b2cbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import tensorflow as tf 
import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
# Load your trained model and label map 
path="skinDiseaseDetection (1).h5"
model = load_model(path)  
label_map={0: 'pigmented benign keratosis',
 1: 'melanoma',
 2: 'vascular lesion',
 3: 'actinic keratosis',
 4: 'squamous cell carcinoma',
 5: 'basal cell carcinoma',
 6: 'seborrheic keratosis',
 7: 'dermatofibroma',
 8: 'nevus'}

def predict_image(image):
    """
    Predict the class of an uploaded image of a skin lesion.

    Parameters:
    - image: The uploaded image in PIL format.

    Returns:
    - predicted_class: The name of the predicted class or an error message.
    - confidence: The confidence score of the prediction (0 to 1) or None.
    - prediction_type: The type of prediction or an error message.
    """
    if image is None:
        return "Please upload an image.", None, "No Diagnosis"

    # Preprocess the image
    image = image.resize((100, 75))  # Resize to model's expected input size
    image_array = np.asarray(image)
    image_array = (image_array - np.mean(image_array)) / np.std(image_array)  # Normalize
    image_array = np.expand_dims(image_array, axis=0)  # Add batch dimension

    # Make prediction using the loaded model
    predictions = model.predict(image_array)
    predicted_index = np.argmax(predictions, axis=1)[0]
    confidence = np.max(predictions)
    predicted_class = label_map[predicted_index]

    # Define classes for benign and malignant lesions
    benign_classes = [
        'pigmented benign keratosis', 
        'vascular lesion', 
        'actinic keratosis',
        'seborrheic keratosis', 
        'dermatofibroma', 
        'nevus'
    ]
    malignant_classes = [
        'melanoma', 
        'squamous cell carcinoma', 
        'basal cell carcinoma'
    ]

    # Determine the type of prediction
    if predicted_class in benign_classes:
        prediction_type = 'Benign Neoplasm'
    elif predicted_class in malignant_classes:
        prediction_type = 'Malignant Neoplasm'
    else:
        prediction_type = 'Unknown Neoplasm'

    return predicted_class, float(f"{confidence:.2f}"), prediction_type  # Format confidence here

# Example images and their descriptions
examples = [
    ["bcc.jpg"],
    ["d.jpg"],
    ["m.PNG"],
    ["sck.jpg"]
]

# Set up the Gradio interface
iface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="pil", label="Upload Skin Lesion Image"),
    outputs=[
        gr.Label(label="Predicted Class"),
        gr.Number(label="Confidence Score", precision=2),
        gr.Textbox(label="Diagnosis Type", interactive=False)
    ],
    title="Skin Cancer Image Classification",
    description="Upload an image of a skin lesion to predict its type and determine if the diagnosis is Benign or Malignant.",
    theme="default",  # Use a valid theme like "default" or "compact"
    allow_flagging="never",  # Optional: Disable flagging for a cleaner UI
    examples=examples ,
    css="""
    .gradio-container {
        font-family: 'Arial', sans-serif;
        background-color: #ddebf7;
        border-radius: 10px;
        padding: 20px;
    }
    .gr-button {
        background-color: #4CAF50;
        color: white;
    }
    .gr-image {
        border: 2px solid #ddd;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    }
    .output-textbox {
        font-size: 1.2em;
        text-align: center;
        color: #333;
    }
    """,
)

# Launch the Gradio interface
iface.launch()