Vinit710 commited on
Commit
4560638
·
verified ·
1 Parent(s): da0a36d
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -1,19 +1,25 @@
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
 
6
- # Load your model (using a relative path)
7
  model = load_model('cats_and_dogs_classifier.h5')
8
 
 
9
  def predict(image):
10
- img = image.resize((224, 224))
 
 
 
11
  img = img_to_array(img)
12
  img = np.expand_dims(img, axis=0)
 
13
  prediction = model.predict(img)
14
  class_label = 'cat' if prediction[0][0] < 0.5 else 'dog'
15
  return class_label
16
 
17
  # Create a Gradio interface
18
- interface = gr.Interface(fn=predict, inputs=gr.inputs.Image(type="pil"), outputs="text")
19
  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
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()