Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,26 @@
|
|
1 |
import gradio as gr
|
2 |
-
import h2o
|
3 |
import numpy as np
|
4 |
import pandas as pd
|
5 |
import cv2
|
|
|
6 |
from skimage.color import rgb2hsv
|
7 |
from skimage.measure import shannon_entropy
|
8 |
from scipy.ndimage import generic_filter
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Feature extraction function
|
16 |
def extract_features(image):
|
@@ -76,16 +86,25 @@ def extract_features(image):
|
|
76 |
def predict(image, gender, age):
|
77 |
# Extract image features
|
78 |
features = extract_features(image)
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
features["age"] = age
|
81 |
|
82 |
# Convert features to DataFrame
|
83 |
features_df = pd.DataFrame([features])
|
84 |
-
features_h2o = h2o.H2OFrame(features_df)
|
85 |
|
86 |
-
#
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# Gradio Interface
|
91 |
interface = gr.Interface(
|
@@ -95,9 +114,9 @@ interface = gr.Interface(
|
|
95 |
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
|
96 |
gr.Slider(0, 100, step=1, label="Age"),
|
97 |
],
|
98 |
-
outputs="
|
99 |
title="Image-based Prediction App",
|
100 |
-
description="Upload an image, enter your gender and age, and get predictions using the pre-trained model."
|
101 |
)
|
102 |
|
103 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import cv2
|
5 |
+
import pickle
|
6 |
from skimage.color import rgb2hsv
|
7 |
from skimage.measure import shannon_entropy
|
8 |
from scipy.ndimage import generic_filter
|
9 |
|
10 |
+
# Load the pre-trained SVR model, MinMaxScaler, and LabelEncoder from pickle files
|
11 |
+
model_path = "svr_model.pkl" # Replace with the path to your pickle file
|
12 |
+
scaler_path = "minmax_scaler.pkl" # Replace with the path to your MinMaxScaler pickle file
|
13 |
+
encoder_path = "label_encoder.pkl" # Replace with the path to your LabelEncoder pickle file
|
14 |
+
|
15 |
+
# Load the pickle files
|
16 |
+
with open(model_path, 'rb') as f:
|
17 |
+
svr_model = pickle.load(f)
|
18 |
+
|
19 |
+
with open(scaler_path, 'rb') as f:
|
20 |
+
scaler = pickle.load(f)
|
21 |
+
|
22 |
+
with open(encoder_path, 'rb') as f:
|
23 |
+
label_encoder = pickle.load(f)
|
24 |
|
25 |
# Feature extraction function
|
26 |
def extract_features(image):
|
|
|
86 |
def predict(image, gender, age):
|
87 |
# Extract image features
|
88 |
features = extract_features(image)
|
89 |
+
|
90 |
+
# Encode gender using LabelEncoder
|
91 |
+
gender_encoded = label_encoder.transform([gender])[0] # Transform the gender to the correct encoded value
|
92 |
+
|
93 |
+
# Add gender and age to the feature dictionary
|
94 |
+
features["gender"] = gender_encoded
|
95 |
features["age"] = age
|
96 |
|
97 |
# Convert features to DataFrame
|
98 |
features_df = pd.DataFrame([features])
|
|
|
99 |
|
100 |
+
# Scale the features using MinMaxScaler
|
101 |
+
features_scaled = scaler.transform(features_df)
|
102 |
+
|
103 |
+
# Predict using the SVR model
|
104 |
+
prediction = svr_model.predict(features_scaled)
|
105 |
+
|
106 |
+
# Return the prediction (you can format this depending on the model output)
|
107 |
+
return prediction[0]
|
108 |
|
109 |
# Gradio Interface
|
110 |
interface = gr.Interface(
|
|
|
114 |
gr.Dropdown(choices=["Male", "Female"], label="Gender"),
|
115 |
gr.Slider(0, 100, step=1, label="Age"),
|
116 |
],
|
117 |
+
outputs="number",
|
118 |
title="Image-based Prediction App",
|
119 |
+
description="Upload an image, enter your gender and age, and get predictions using the pre-trained SVR model."
|
120 |
)
|
121 |
|
122 |
# Launch the app
|