-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
27 lines (21 loc) · 845 Bytes
/
predict.py
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
import tensorflow as tf
from tensorflow import keras
import numpy as np
import cv2
import os
model = keras.models.load_model('mobilenet.keras')
def preprocess_image(image_path, target_size=(224, 224)):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, target_size)
image = image.astype('float32') / 255.0
image = np.expand_dims(image, axis=0)
return image
def predict_image(image_path, class_names):
image = preprocess_image(image_path)
prediction = model.predict(image)[0]
return class_names[np.argmax(prediction)]
class_names = sorted(os.listdir('FastFoodClassificationV2/Train'))
image_path = 'FastFoodClassificationV2/Test/Taco/Taco-Test (14).jpeg'
predicted_class = predict_image(image_path, class_names)
print(f'predict: {predicted_class}')