Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import numpy as np | |
| from PIL import Image | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras.applications.mobilenet_v2 import preprocess_input | |
| import joblib | |
| # Paths to the saved models | |
| KNN_MODEL_PATH = './knn_pharyngitis_model.pkl' | |
| EXTRACTOR_PATH = './mobilenetv2_feature_extractor.h5' | |
| # Display a welcome message and note | |
| st.title("Pharyngitis Classification App") | |
| st.write(""" | |
| **Please wait while the models are being loaded.** | |
| """) | |
| # Load the saved models | |
| with st.spinner("Please wait for a while..."): | |
| knn = joblib.load(KNN_MODEL_PATH) | |
| feature_extractor = load_model(EXTRACTOR_PATH) | |
| st.success("Models loaded successfully!") | |
| # Display additional information | |
| st.markdown(""" | |
| ### Note: | |
| - This application predicts whether the uploaded throat image shows signs of *pharyngitis* or not. | |
| - **Accuracy:** Approximately 80%. | |
| - **Disclaimer:** This tool is not a substitute for a medical professional's advice. | |
| Please consult a physician if you experience any throat-related issues. | |
| """) | |
| # Function to preprocess the uploaded image | |
| def preprocess_image(image): | |
| img = image.resize((224, 224)) # Resize to match MobileNetV2 input size | |
| img_array = np.array(img) | |
| img_array = preprocess_input(img_array) # Apply MobileNetV2 preprocessing | |
| return np.expand_dims(img_array, axis=0) | |
| # Function to classify the image | |
| def classify_image(image): | |
| processed_image = preprocess_image(image) | |
| features = feature_extractor.predict(processed_image) | |
| prediction = knn.predict(features) | |
| return "Pharyngitis" if prediction[0] == 1 else "No Pharyngitis" | |
| # Streamlit app UI | |
| st.write("### Upload an image to classify it as 'Pharyngitis' or 'No Pharyngitis'.") | |
| uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Load the uploaded image | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Classify the image | |
| st.write("### Classifying...") | |
| with st.spinner("Analyzing the image..."): | |
| prediction = classify_image(image) | |
| st.success(f"Prediction: **{prediction}**") | |
| # Footer with a link to your LinkedIn profile | |
| st.markdown(""" | |
| --- | |
| Made with ❤️ by [Haris](https://www.linkedin.com/in/h4r1s) | |
| """) | |