Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,21 +5,26 @@ from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
|
5 |
import gradio as gr
|
6 |
|
7 |
# Define class names for the model
|
8 |
-
class_names = ["
|
9 |
|
10 |
# Load the pre-trained model
|
11 |
-
|
|
|
12 |
|
13 |
# Function to preprocess the image and make predictions
|
14 |
def classify_image(image):
|
15 |
-
#
|
16 |
-
img = load_img(image, target_size=(150, 150))
|
17 |
img_array = img_to_array(img) / 255.0 # Normalize pixel values
|
18 |
img_array = img_array.reshape((1, 150, 150, 3)) # Reshape for the model
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Create a Gradio interface
|
25 |
iface = gr.Interface(fn=classify_image,
|
@@ -29,5 +34,4 @@ iface = gr.Interface(fn=classify_image,
|
|
29 |
description="Upload an image to classify as either Boy or Girl.")
|
30 |
|
31 |
# Run the Gradio app
|
32 |
-
if
|
33 |
-
iface.launch(share=True) # Set share=True to create a public link
|
|
|
5 |
import gradio as gr
|
6 |
|
7 |
# Define class names for the model
|
8 |
+
class_names = ["Girl", "Boy"] # Adjusted to represent binary classification
|
9 |
|
10 |
# Load the pre-trained model
|
11 |
+
model_path = 'best_model.h5' # Update this path
|
12 |
+
model = load_model(model_path)
|
13 |
|
14 |
# Function to preprocess the image and make predictions
|
15 |
def classify_image(image):
|
16 |
+
# Load and preprocess the image
|
17 |
+
img = load_img(image, target_size=(150, 150)) # Ensure this matches the input size of your model
|
18 |
img_array = img_to_array(img) / 255.0 # Normalize pixel values
|
19 |
img_array = img_array.reshape((1, 150, 150, 3)) # Reshape for the model
|
20 |
+
|
21 |
+
# Log the shape of the image array for debugging
|
22 |
+
print(f"Image shape for prediction: {img_array.shape}")
|
23 |
+
|
24 |
+
# Get prediction
|
25 |
+
prediction = model.predict(img_array)
|
26 |
+
predicted_class = class_names[1] if prediction[0][0] > 0.5 else class_names[0] # Interpret output
|
27 |
+
return predicted_class
|
28 |
|
29 |
# Create a Gradio interface
|
30 |
iface = gr.Interface(fn=classify_image,
|
|
|
34 |
description="Upload an image to classify as either Boy or Girl.")
|
35 |
|
36 |
# Run the Gradio app
|
37 |
+
iface.launch(share=True) # Set share=True if you want to share the link
|
|