Vinit710 commited on
Commit
20aca7b
·
verified ·
1 Parent(s): aac2601
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -1,25 +1,32 @@
1
  import gradio as gr
2
  from tensorflow.keras.models import load_model
3
- from tensorflow.keras.preprocessing.image import img_to_array
4
- from PIL import Image
5
  import numpy as np
 
6
 
7
- # Load your model
8
- model = load_model('cats_and_dogs_classifier.h5')
 
9
 
10
- # Define prediction function
11
  def predict(image):
12
- # Convert Gradio PIL Image to numpy array
13
- img = np.array(image)
14
- # Resize and preprocess image
15
- img = np.array(Image.fromarray(img).resize((224, 224)))
16
- img = img_to_array(img)
17
- img = np.expand_dims(img, axis=0)
18
- # Predict class (0 for cat, 1 for dog)
19
- prediction = model.predict(img)
20
- class_label = 'cat' if prediction[0][0] < 0.5 else 'dog'
21
  return class_label
22
 
23
  # Create a Gradio interface
24
- interface = gr.Interface(fn=predict, inputs="image", outputs="text")
25
- interface.launch()
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.preprocessing.image import img_to_array, load_img
 
4
  import numpy as np
5
+ from PIL import Image
6
 
7
+ # Load your model (using a relative path)
8
+ model_path = "cats_and_dogs_classifier.h5"
9
+ model = load_model(model_path)
10
 
 
11
  def predict(image):
12
+ # Resize image to match model's expected sizing
13
+ img = image.resize((150, 150))
14
+ # Convert image to array and expand dimensions to fit model input requirements
15
+ img_array = img_to_array(img)
16
+ img_array = np.expand_dims(img_array, axis=0)
17
+ # Predict using loaded model
18
+ prediction = model.predict(img_array)
19
+ # Convert prediction to label
20
+ class_label = 'Cat' if prediction[0][0] < 0.5 else 'Dog'
21
  return class_label
22
 
23
  # Create a Gradio interface
24
+ iface = gr.Interface(fn=predict,
25
+ inputs=gr.inputs.Image(type='pil', label="Upload a photo of a cat or dog"),
26
+ outputs="text",
27
+ title="Cat or Dog Classifier",
28
+ description="Upload an image of a cat or dog to classify it.")
29
+
30
+ if __name__ == "__main__":
31
+ iface.launch()
32
+