| | import tensorflow as tf
|
| | import numpy as np
|
| | from PIL import Image
|
| |
|
| |
|
| | model = tf.keras.models.load_model(
|
| | "saved_model/InceptionV3_Dogs_and_Cats_Classification.h5",
|
| | compile=False
|
| | )
|
| |
|
| |
|
| | CLASS_NAMES = ["Cat", "Dog"]
|
| |
|
| | def preprocess_image(img: Image.Image, target_size=(256, 256)):
|
| |
|
| | img = img.convert("RGB")
|
| | img = img.resize(target_size)
|
| | img = np.array(img).astype("float32") / 255.0
|
| | img = np.expand_dims(img, axis=0)
|
| | return img
|
| |
|
| | def predict(img: Image.Image):
|
| |
|
| | input_tensor = preprocess_image(img)
|
| |
|
| |
|
| | prob = float(model.predict(input_tensor)[0][0])
|
| |
|
| |
|
| | if prob >= 0.5:
|
| | label = CLASS_NAMES[1]
|
| | else:
|
| | label = CLASS_NAMES[0]
|
| |
|
| |
|
| | confidence = prob if label == CLASS_NAMES[1] else 1 - prob
|
| | prob_dict = {
|
| | CLASS_NAMES[0]: 1 - prob,
|
| | CLASS_NAMES[1]: prob
|
| | }
|
| |
|
| | return label, confidence, prob_dict |