Spaces:
Sleeping
Sleeping
updated
Browse files
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
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
# Load your model
|
| 7 |
model = load_model('cats_and_dogs_classifier.h5')
|
| 8 |
|
|
|
|
| 9 |
def predict(image):
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 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()
|