Keras
nxt-shreyash commited on
Commit
949bf24
·
verified ·
1 Parent(s): efe4679

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from keras.models import load_model
3
+ from PIL import Image
4
+ import tensorflow as tf
5
+
6
+ # Load the model
7
+ model = load_model("model_img_pro.keras") # Update with your actual model path
8
+
9
+ # Function to preprocess the image
10
+ def preprocess_image(image_path, target_size):
11
+ # Load and resize the image
12
+ image = Image.open(image_path).convert('RGB')
13
+ image = image.resize(target_size)
14
+
15
+ # Convert image to array and normalize
16
+ image_array = np.array(image) / 255.0
17
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
18
+
19
+ return image_array
20
+
21
+ # Define target size based on your model
22
+ target_size = (224, 224) # Example size; update based on your model's expected input
23
+
24
+ # Path to the image you want to classify
25
+ image_path = "path_to_your_image.jpg" # Update with your image path
26
+
27
+ # Preprocess the image
28
+ processed_image = preprocess_image(image_path, target_size)
29
+
30
+ # Make predictions
31
+ predictions = model.predict(processed_image)
32
+
33
+ # Print the predictions
34
+ print(predictions)
35
+
36
+ # Optionally, decode the predictions if needed
37
+ # For binary classification (cat vs dog), you might have a single output neuron
38
+ # Example: if predictions[0][0] > 0.5, it's a dog; otherwise, it's a cat
39
+ if predictions[0][0] > 0.5:
40
+ print("Dog")
41
+ else:
42
+ print("Cat")