Spaces:
Runtime error
Runtime error
okeowo1014
commited on
Commit
•
c6cc42e
1
Parent(s):
ca0e1b5
Update predictor2.py
Browse files- predictor2.py +37 -0
predictor2.py
CHANGED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2 # Assuming you have OpenCV installed
|
2 |
+
import numpy as np
|
3 |
+
from tensorflow.keras.preprocessing import image
|
4 |
+
import tensorflow as tf
|
5 |
+
from huggingface_hub import from_pretrained_keras
|
6 |
+
|
7 |
+
model = from_pretrained_keras("okeowo1014/catsanddogs")
|
8 |
+
|
9 |
+
|
10 |
+
# Load the saved model
|
11 |
+
# model = tf.keras.models.load_model('cat_dog_classifier.keras') # Replace with your model filename
|
12 |
+
img_width, img_height = 224, 224 # VGG16 expects these dimensions
|
13 |
+
|
14 |
+
|
15 |
+
# Function to preprocess an image for prediction
|
16 |
+
def preprocess_image(img_path):
|
17 |
+
img = cv2.imread(img_path) # Read the image
|
18 |
+
img = cv2.resize(img, (img_width, img_height)) # Resize according to model input size
|
19 |
+
img = img.astype('float32') / 255.0 # Normalize pixel values
|
20 |
+
img = np.expand_dims(img, axis=0) # Add a batch dimension (model expects batch of images)
|
21 |
+
return img
|
22 |
+
|
23 |
+
|
24 |
+
# Get the path to your new image
|
25 |
+
new_image_path = 'test1/11.jpg' # Replace with your image path
|
26 |
+
|
27 |
+
# Preprocess the image
|
28 |
+
preprocessed_image = preprocess_image(new_image_path)
|
29 |
+
|
30 |
+
# Make prediction
|
31 |
+
prediction = model.predict(preprocessed_image)
|
32 |
+
|
33 |
+
# Decode the prediction (assuming class 0 is cat, 1 is dog)
|
34 |
+
predicted_class = int(prediction[0][0] > 0.5) # Threshold of 0.5 for binary classification
|
35 |
+
class_names = ['cat', 'dog'] # Adjust class names according to your model
|
36 |
+
|
37 |
+
print(f"Predicted class: {class_names[predicted_class]}")
|