|
from PIL import Image |
|
import tensorflow as tf |
|
import numpy as np |
|
|
|
|
|
|
|
|
|
model = tf.keras.models.load_model(model_path) |
|
|
|
def preprocess_image(image: Image.Image) -> np.ndarray: |
|
|
|
image = image.resize((256, 256)) |
|
|
|
img_array = np.array(image) / 255.0 |
|
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
return img_array |
|
|
|
def predict_image(image_path): |
|
img = Image.open(image_path) |
|
|
|
img_array = preprocess_image(img) |
|
predictions = model.predict(img_array) |
|
predicted_class = np.argmax(predictions) |
|
if predicted_class == 0: |
|
predict_label = "Clean" |
|
else: |
|
predict_label = "Carries" |
|
|
|
|
|
return predict_label,predictions.flatten() |
|
|
|
|
|
|
|
|
|
|
|
|
|
predict_label, logits = predict_image(image_path) |
|
print("Predicted class:", predict_label) |
|
print("Evaluate:", ', '.join(f"{logits*100:.4f}%" for logits in logits)) |