Spaces:
Sleeping
Sleeping
Create prd2.py
Browse files
prd2.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
model = from_pretrained_keras("okeowo1014/catsanddogs")
|
| 7 |
+
|
| 8 |
+
# Load the saved model
|
| 9 |
+
# model = tf.keras.models.load_model('cat_dog_classifier.keras') # Replace with your model filename
|
| 10 |
+
img_width, img_height = 224, 224 # VGG16 expects these dimensions
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Function to preprocess an image for prediction
|
| 14 |
+
def preprocess_image(img_path):
|
| 15 |
+
img = cv2.imread(img_path) # Read the image
|
| 16 |
+
img = cv2.resize(img, (img_width, img_height)) # Resize according to model input size
|
| 17 |
+
img = img.astype('float32') / 255.0 # Normalize pixel values
|
| 18 |
+
img = np.expand_dims(img, axis=0) # Add a batch dimension (model expects batch of images)
|
| 19 |
+
return img
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Get the path to your new image
|
| 23 |
+
new_image_path = 'test1/11.jpg' # Replace with your image path
|
| 24 |
+
|
| 25 |
+
# Preprocess the image
|
| 26 |
+
preprocessed_image = preprocess_image(new_image_path)
|
| 27 |
+
|
| 28 |
+
# Make prediction
|
| 29 |
+
prediction = model.predict(preprocessed_image)
|
| 30 |
+
|
| 31 |
+
# Decode the prediction (assuming class 0 is cat, 1 is dog)
|
| 32 |
+
predicted_class = int(prediction[0][0] > 0.5) # Threshold of 0.5 for binary classification
|
| 33 |
+
class_names = ['cat', 'dog'] # Adjust class names according to your model
|
| 34 |
+
|
| 35 |
+
print(f"Predicted class: {class_names[predicted_class]}")
|