Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
|
6 |
+
import joblib
|
7 |
+
import gdown
|
8 |
+
|
9 |
+
# Google Drive model URLs
|
10 |
+
KNN_MODEL_URL = 'https://drive.google.com/uc?id=1TJ0KbzFw-2NfuJf67xvp-32uaYLIqpj3'
|
11 |
+
EXTRACTOR_URL = 'https://drive.google.com/uc?id=1HR2Qc8Fji6RzbtG_K_sqSoiG0AQnvyZa'
|
12 |
+
|
13 |
+
# Download the model files
|
14 |
+
st.write("Downloading models...")
|
15 |
+
gdown.download(KNN_MODEL_URL, 'knn_pharyngitis_model.pkl', quiet=False)
|
16 |
+
gdown.download(EXTRACTOR_URL, 'mobilenetv2_feature_extractor.h5', quiet=False)
|
17 |
+
st.write("Models downloaded successfully!")
|
18 |
+
|
19 |
+
# Load the saved models
|
20 |
+
knn = joblib.load('knn_pharyngitis_model.pkl')
|
21 |
+
feature_extractor = load_model('mobilenetv2_feature_extractor.h5')
|
22 |
+
|
23 |
+
# Function to preprocess the uploaded image
|
24 |
+
def preprocess_image(image):
|
25 |
+
img = image.resize((224, 224)) # Resize to match MobileNetV2 input size
|
26 |
+
img_array = np.array(img)
|
27 |
+
img_array = preprocess_input(img_array) # Apply MobileNetV2 preprocessing
|
28 |
+
return np.expand_dims(img_array, axis=0)
|
29 |
+
|
30 |
+
# Function to classify the image
|
31 |
+
def classify_image(image):
|
32 |
+
processed_image = preprocess_image(image)
|
33 |
+
features = feature_extractor.predict(processed_image)
|
34 |
+
prediction = knn.predict(features)
|
35 |
+
return "Pharyngitis" if prediction[0] == 1 else "No Pharyngitis"
|
36 |
+
|
37 |
+
# Streamlit app UI
|
38 |
+
st.title("Pharyngitis Classification App")
|
39 |
+
st.write("Upload an image to classify it as 'Pharyngitis' or 'No Pharyngitis'.")
|
40 |
+
|
41 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
42 |
+
|
43 |
+
if uploaded_file is not None:
|
44 |
+
# Load the uploaded image
|
45 |
+
image = Image.open(uploaded_file)
|
46 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
47 |
+
|
48 |
+
# Classify the image
|
49 |
+
st.write("Classifying...")
|
50 |
+
prediction = classify_image(image)
|
51 |
+
st.write(f"Prediction: **{prediction}**")
|