import os import sys os.environ["KERAS_BACKEND"] = "tensorflow" import numpy as np import keras from keras.applications.mobilenet_v2 import ( MobileNetV2, decode_predictions, preprocess_input, ) image_path = sys.argv[1] if len(sys.argv) > 1 else "sample.jpg" model = MobileNetV2(weights="imagenet") image = keras.utils.load_img( image_path, color_mode="rgb", target_size=(224, 224), ) image_array = keras.utils.img_to_array(image) image_batch = np.expand_dims(image_array, axis=0) model_input = preprocess_input(image_batch.copy()) predictions = model.predict(model_input, verbose=0) print(f"model input shape: {model_input.shape}") print(f"prediction shape: {predictions.shape}") for rank, (_, label, score) in enumerate( decode_predictions(predictions, top=3)[0], start=1, ): print(f"{rank}. {label}: {score:.3f}")