-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimg_classification.py
More file actions
29 lines (20 loc) · 854 Bytes
/
img_classification.py
File metadata and controls
29 lines (20 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from PIL import Image, ImageOps
import numpy as np
def teachable_machine_classification(img, weights_file):
# Load the model
model = keras.models.load_model(weights_file)
# Create the array of the right shape to feed into the keras model
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
image = img
#image sizing
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
#turn the image into a numpy array
image_array = np.asarray(image)
# Normalize the image
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
# Load the image into the array
data[0] = normalized_image_array
# run the inference
prediction = model.predict(data)
return np.argmax(prediction) # return position of the highest probability