szili2011 commited on
Commit
4255e30
1 Parent(s): 75b4669

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
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 = ["Boy", "Girl"] # Classes to classify
9
 
10
  # Load the pre-trained model
11
- model = load_model('best_model.h5')
 
12
 
13
  # Function to preprocess the image and make predictions
14
  def classify_image(image):
15
- # Resize the image to 150x150
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
- predictions = model.predict(img_array) # Get prediction
20
- predicted_class_index = np.argmax(predictions[0]) # Get the index of the highest prediction
21
- predicted_class = class_names[predicted_class_index] # Get the class name
22
- return predicted_class # Return the class name
 
 
 
 
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 __name__ == "__main__":
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