Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,43 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from keras.models import load_model # TensorFlow is required for Keras to work
|
2 |
+
from PIL import Image, ImageOps # Install pillow instead of PIL
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Disable scientific notation for clarity
|
6 |
+
np.set_printoptions(suppress=True)
|
7 |
+
|
8 |
+
# Load the model
|
9 |
+
model = load_model("keras_Model.h5", compile=False)
|
10 |
+
|
11 |
+
# Load the labels
|
12 |
+
class_names = open("labels.txt", "r").readlines()
|
13 |
+
|
14 |
+
# Create the array of the right shape to feed into the keras model
|
15 |
+
# The 'length' or number of images you can put into the array is
|
16 |
+
# determined by the first position in the shape tuple, in this case 1
|
17 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
18 |
+
|
19 |
+
# Replace this with the path to your image
|
20 |
+
image = Image.open("<IMAGE_PATH>").convert("RGB")
|
21 |
+
|
22 |
+
# resizing the image to be at least 224x224 and then cropping from the center
|
23 |
+
size = (224, 224)
|
24 |
+
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
|
25 |
+
|
26 |
+
# turn the image into a numpy array
|
27 |
+
image_array = np.asarray(image)
|
28 |
+
|
29 |
+
# Normalize the image
|
30 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
|
31 |
+
|
32 |
+
# Load the image into the array
|
33 |
+
data[0] = normalized_image_array
|
34 |
+
|
35 |
+
# Predicts the model
|
36 |
+
prediction = model.predict(data)
|
37 |
+
index = np.argmax(prediction)
|
38 |
+
class_name = class_names[index]
|
39 |
+
confidence_score = prediction[0][index]
|
40 |
+
|
41 |
+
# Print prediction and confidence score
|
42 |
+
print("Class:", class_name[2:], end="")
|
43 |
+
print("Confidence Score:", confidence_score)
|