Spaces:
Sleeping
Sleeping
import streamlit as st | |
import tensorflow as tf | |
import numpy as np | |
import cv2 | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
from datetime import datetime | |
# Load your model | |
model = tf.keras.models.load_model("SkinCancer.h5") | |
class_names = [ | |
"Actinic keratoses and intraepithelial carcinomae", | |
"Basal Cell Carcinoma", | |
"Benign Keratosis-like Lesions", | |
"Dermatofibroma", | |
"Melanocytic Nevi", | |
"Pyogenic Granulomas and Hemorrhage", | |
"Melanoma" | |
] | |
def create_report_image(name, phone, age, email, prediction, confidence, uploaded_image): | |
plt.figure(figsize=(8, 10)) | |
# Create report text in a structured format | |
report_text = ( | |
f"Skin Disease Prediction Report\n" | |
f"Date: {datetime.now().strftime('%Y-%m-%d')}\n\n" | |
f"Patient Details:\n" | |
f"Name: {name}\n" | |
f"Phone Number: {phone}\n" | |
f"Age: {age}\n" | |
f"Email: {email}\n\n" | |
f"Predicted Disease: {prediction}\n" | |
f"Confidence: {confidence:.2f}%\n\n" | |
f"This report is generated by an AI model. Please consult a healthcare professional for a further diagnosis." | |
) | |
# Add report text to the top | |
plt.subplot(2, 1, 1) # Two rows, one column, first subplot | |
plt.axis('off') # Hide axes | |
plt.text(0.5, 0.5, report_text, fontsize=12, ha='center', va='center', wrap=True) | |
plt.title("Prediction Report", fontsize=16) | |
# Display the uploaded image at the bottom with a heading | |
plt.subplot(2, 1, 2) # Two rows, one column, second subplot | |
plt.axis('off') | |
plt.title("User's Uploaded Image", fontsize=14) | |
plt.imshow(uploaded_image) | |
plt.tight_layout() # Adjust layout to prevent overlap | |
plt.savefig("prediction_report.png", bbox_inches='tight') | |
plt.close() | |
def app(): | |
st.title("Skin Disease Prediction⏳") | |
# Display the header image | |
st.image("https://cdn.medicalfuturist.com/wp-content/uploads/2023/09/tmf_article_386.png", use_column_width=True) | |
# Image upload | |
uploaded_file = st.file_uploader("**Upload an image of the skin lesion** 👇🏻", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
image = Image.open(uploaded_file) | |
st.image(image, caption='Uploaded Image', use_column_width=True) | |
# User details input | |
st.subheader("Please enter your details:👇") | |
name = st.text_input("Name") | |
phone = st.text_input("Phone Number", max_chars=10) | |
age = st.number_input("Age", min_value=0, max_value=120) | |
email = st.text_input("Email") | |
if st.button("Predict"): | |
# Validation | |
if len(name) < 5: | |
st.error("Name must be at least 5 characters long.") | |
return | |
if not (phone.isdigit() and len(phone) >= 10): | |
st.error("Phone number must be exactly 10 digits.") | |
return | |
if "gmail.com" not in email: | |
st.error("Email must be a Gmail address.") | |
return | |
def predict_skin_disease(image, model): | |
image = np.array(image) | |
resized_image = cv2.resize(image, (28, 28)) | |
image_batch = np.expand_dims(resized_image, axis=0) | |
pred = model.predict(image_batch) | |
predicted_class = np.argmax(pred, axis=1) | |
confidence = np.max(pred) * 100 # Confidence percentage | |
return class_names[predicted_class[0]], confidence | |
prediction, confidence = predict_skin_disease(image, model) | |
st.success(f"Prediction: {prediction} (Confidence: {confidence:.2f}%)") | |
# Create columns for user details and report | |
col1, col2 = st.columns([1, 2]) # Adjust column widths as needed | |
with col1: | |
# Display user details | |
st.subheader("User Details:") | |
st.write(f"Name: {name}") | |
st.write(f"Phone Number: {phone}") | |
st.write(f"Age: {age}") | |
st.write(f"Email: {email}") | |
with col2: | |
# Provide report | |
st.subheader("Prediction Report📑") | |
st.markdown(f"The model predicts that the skin lesion is most likely: **{prediction}** with a confidence of **{confidence:.2f}%**.") | |
st.write("Please consult a healthcare professional for a further diagnosis.") | |
# Generate report image | |
create_report_image(name, phone, age, email, prediction, confidence, image) | |
# Center display of the report image | |
st.markdown("<h3 style='text-align: center;'>Prediction Report</h3>", unsafe_allow_html=True) | |
st.image("prediction_report.png", use_column_width=True) | |
if __name__ == "__main__": | |
app() | |