-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (56 loc) · 2.28 KB
/
app.py
File metadata and controls
70 lines (56 loc) · 2.28 KB
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
import streamlit as st
from PIL import Image
import numpy as np
import joblib
import os
from skimage.feature import hog
IMAGE_SIZE = (128, 128)
MODEL_DIR = 'model'
# Load models
try:
models = {
'SVM': joblib.load(os.path.join(MODEL_DIR, 'svm.pkl')),
'Random Forest': joblib.load(os.path.join(MODEL_DIR, 'rf.pkl')),
'XGBoost': joblib.load(os.path.join(MODEL_DIR, 'xgb.pkl')),
'KNN': joblib.load(os.path.join(MODEL_DIR, 'knn.pkl')),
}
except FileNotFoundError as e:
st.error(f"Model file not found: {e}")
st.stop()
def extract_hog_features(image):
image = image.resize(IMAGE_SIZE).convert('L')
image_np = np.array(image)
features = hog(image_np,
orientations=9,
pixels_per_cell=(8, 8),
cells_per_block=(2, 2),
block_norm='L2-Hys')
if len(features) > 7056:
features = features[:7056]
return features.reshape(1, -1)
# UI
st.title("🧠 Autism Detection from Face Image")
uploaded_file = st.file_uploader("📤 Upload a face image", type=['jpg', 'jpeg', 'png'])
selected_model_name = st.selectbox("🔍 Choose a model for prediction:", list(models.keys()))
if uploaded_file is not None and selected_model_name:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_container_width=True)
with st.spinner(f'Extracting features and predicting with {selected_model_name}...'):
try:
features = extract_hog_features(image)
model = models[selected_model_name]
if selected_model_name == 'SVM':
# SVM doesn't support predict_proba
score = model.decision_function(features)[0]
prediction = 1 if score >= 0 else 0
st.info(f"📈 SVM decision score: {score:.2f}")
else:
prob = model.predict_proba(features)[0][1]
prediction = 1 if prob >= 0.5 else 0
st.info(f"📊 Predicted probability: {prob:.2f}")
if prediction == 1:
st.success("🔴 The model predicts **Autistic**.")
else:
st.success("🟢 The model predicts **Non-Autistic**.")
except Exception as e:
st.error(f"Error during prediction: {e}")