Upload 4 files
Browse files- app.py +107 -0
- best_automl_model.pkl +3 -0
- requirements.txt +6 -0
- style.css +39 -0
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pycaret.classification import load_model
|
| 5 |
+
|
| 6 |
+
# Load the saved anemia prediction model
|
| 7 |
+
model = load_model('best_automl_model')
|
| 8 |
+
|
| 9 |
+
# Function to calculate RGB percentages
|
| 10 |
+
def calculate_rgb_percentage(image):
|
| 11 |
+
"""
|
| 12 |
+
Calculate the Red, Green, and Blue pixel percentages from the cropped conjunctiva portion.
|
| 13 |
+
"""
|
| 14 |
+
# Convert image to numpy array
|
| 15 |
+
image_array = np.array(image)
|
| 16 |
+
|
| 17 |
+
# Extract R, G, B channels
|
| 18 |
+
red_channel = image_array[:, :, 0]
|
| 19 |
+
green_channel = image_array[:, :, 1]
|
| 20 |
+
blue_channel = image_array[:, :, 2]
|
| 21 |
+
|
| 22 |
+
# Calculate totals
|
| 23 |
+
total_red = np.sum(red_channel)
|
| 24 |
+
total_green = np.sum(green_channel)
|
| 25 |
+
total_blue = np.sum(blue_channel)
|
| 26 |
+
total_rgb = total_red + total_green + total_blue
|
| 27 |
+
|
| 28 |
+
# Avoid division by zero
|
| 29 |
+
if total_rgb == 0:
|
| 30 |
+
return 0, 0, 0
|
| 31 |
+
|
| 32 |
+
# Calculate percentages
|
| 33 |
+
red_percentage = (total_red / total_rgb) * 100
|
| 34 |
+
green_percentage = (total_green / total_rgb) * 100
|
| 35 |
+
blue_percentage = (total_blue / total_rgb) * 100
|
| 36 |
+
|
| 37 |
+
return round(red_percentage, 2), round(green_percentage, 2), round(blue_percentage, 2)
|
| 38 |
+
|
| 39 |
+
# Function to process image and predict anemia
|
| 40 |
+
def process_image_and_predict(image, sex):
|
| 41 |
+
"""
|
| 42 |
+
Process the uploaded eye image, calculate RGB percentages, and predict anemia.
|
| 43 |
+
"""
|
| 44 |
+
# Ensure the image is in RGB format
|
| 45 |
+
image = image.convert("RGB")
|
| 46 |
+
|
| 47 |
+
# Crop image interactively (default cropping for now)
|
| 48 |
+
cropped_image = image.crop((50, 50, 250, 250)) # Modify coordinates if necessary
|
| 49 |
+
|
| 50 |
+
# Calculate RGB percentages
|
| 51 |
+
red_percent, green_percent, blue_percent = calculate_rgb_percentage(cropped_image)
|
| 52 |
+
|
| 53 |
+
# Prepare input for anemia prediction
|
| 54 |
+
sex_encoded = 1 if sex == "Male" else 0 # Encode Male as 1, Female as 0
|
| 55 |
+
input_data = pd.DataFrame({
|
| 56 |
+
'Red Pixel': [red_percent],
|
| 57 |
+
'Green Pixel': [green_percent],
|
| 58 |
+
'Blue Pixel': [blue_percent],
|
| 59 |
+
'Sex': [sex_encoded]
|
| 60 |
+
})
|
| 61 |
+
|
| 62 |
+
# Predict anemia and probability
|
| 63 |
+
prediction = model.predict(input_data)[0]
|
| 64 |
+
probability = model.predict_proba(input_data)[0][1]
|
| 65 |
+
|
| 66 |
+
# Convert prediction result to human-readable format
|
| 67 |
+
prediction_label = "Yes" if prediction == 1 else "No"
|
| 68 |
+
|
| 69 |
+
return (
|
| 70 |
+
cropped_image,
|
| 71 |
+
f"Red: {red_percent}%",
|
| 72 |
+
f"Green: {green_percent}%",
|
| 73 |
+
f"Blue: {blue_percent}%",
|
| 74 |
+
prediction_label,
|
| 75 |
+
f"{probability:.2%}"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Create Gradio Interface
|
| 79 |
+
interface = gr.Interface(
|
| 80 |
+
fn=process_image_and_predict,
|
| 81 |
+
inputs=[
|
| 82 |
+
gr.Image(type="pil", label="Upload or Capture Eye Image"),
|
| 83 |
+
gr.Radio(["Male", "Female"], label="Sex"),
|
| 84 |
+
],
|
| 85 |
+
outputs=[
|
| 86 |
+
gr.Image(type="pil", label="Cropped Conjunctiva"),
|
| 87 |
+
gr.Text(label="🔴 Red Pixel Percentage"),
|
| 88 |
+
gr.Text(label="🟢 Green Pixel Percentage"),
|
| 89 |
+
gr.Text(label="🔵 Blue Pixel Percentage"),
|
| 90 |
+
gr.Text(label="🩺 Anemia Prediction"),
|
| 91 |
+
gr.Text(label="📊 Probability of Anemia (%)"),
|
| 92 |
+
],
|
| 93 |
+
title="✨ Conjunctiva-Based Anemia Prediction App ✨",
|
| 94 |
+
description=(
|
| 95 |
+
"Upload or capture an eye image, crop the conjunctiva portion, calculate the percentages of Red, "
|
| 96 |
+
"Green, and Blue pixels, and predict anemia based on these percentages and sex."
|
| 97 |
+
),
|
| 98 |
+
theme="huggingface", # Use Hugging Face theme
|
| 99 |
+
css="style.css", # Link custom CSS for styling
|
| 100 |
+
examples=[
|
| 101 |
+
["example_eye.jpg", "Male"],
|
| 102 |
+
["example_eye.jpg", "Female"],
|
| 103 |
+
],
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Launch the app
|
| 107 |
+
interface.launch()
|
best_automl_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b2380025f163eeb045e823c58b5ce95106d54622ee3b460c6f7bfd89657c4d70
|
| 3 |
+
size 4375
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pycaret
|
| 3 |
+
pillow
|
| 4 |
+
numpy
|
| 5 |
+
pandas
|
| 6 |
+
scikit-learn
|
style.css
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Set background color */
|
| 2 |
+
body {
|
| 3 |
+
background-color: #f7f7f7;
|
| 4 |
+
font-family: "Arial", sans-serif;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
/* Style the title */
|
| 8 |
+
h1 {
|
| 9 |
+
color: #2c3e50;
|
| 10 |
+
text-align: center;
|
| 11 |
+
font-size: 2.5em;
|
| 12 |
+
margin-bottom: 10px;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
/* Style the description */
|
| 16 |
+
.gr-description {
|
| 17 |
+
text-align: center;
|
| 18 |
+
color: #34495e;
|
| 19 |
+
font-size: 1.2em;
|
| 20 |
+
margin-bottom: 20px;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
/* Customize the buttons */
|
| 24 |
+
button {
|
| 25 |
+
background-color: #3498db !important;
|
| 26 |
+
color: white !important;
|
| 27 |
+
border: none;
|
| 28 |
+
padding: 10px 20px !important;
|
| 29 |
+
border-radius: 5px !important;
|
| 30 |
+
font-size: 1em !important;
|
| 31 |
+
cursor: pointer;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
/* Style text outputs */
|
| 35 |
+
.gr-output-text {
|
| 36 |
+
font-size: 1.1em;
|
| 37 |
+
font-weight: bold;
|
| 38 |
+
color: #2c3e50;
|
| 39 |
+
}
|