import tensorflow as tf
import numpy as np
from PIL import Image
import requests
import tensorflow_hub as hub
from google.colab import files
model_url = "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/classification/2"
model = hub.load(model_url)
def load_and_preprocess_image(img_path):
img = Image.open(img_path)
img = img.resize((224, 224))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0).astype(np.float32)
return img_array
uploaded = files.upload()
image_filename = list(uploaded.keys())[0]
image = load_and_preprocess_image(image_filename)
preds = model(image)
probabilities = tf.nn.softmax(preds, axis=-1)[0]
probable_index = np.argmax(probabilities)
labels_url = "https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt"
labels = requests.get(labels_url).text.splitlines()
print(f'Класс: {labels[probable_index]}')
print(f'Вероятность: {probabilities[probable_index] * 100:.2f}%')