Amazing-Python-Scripts

Форк
0
31 строка · 1001.0 Байт
1
import tensorflow as tf
2
import tensorflow.keras.applications.mobilenet_v2 as mobilenet
3
from tensorflow.keras.preprocessing import image
4
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
5
import numpy as np
6

7

8
def classify_image(image_path):
9
    # Load the pre-trained MobileNetV2 model
10
    model = mobilenet.MobileNetV2(weights='imagenet')
11

12
    # Load and preprocess the image
13
    img = image.load_img(image_path, target_size=(224, 224))
14
    img_array = image.img_to_array(img)
15
    img_array = np.expand_dims(img_array, axis=0)
16
    processed_img = preprocess_input(img_array)
17

18
    # Perform image classification
19
    predictions = model.predict(processed_img)
20
    decoded_predictions = decode_predictions(predictions, top=3)[0]
21

22
    return decoded_predictions
23

24

25
# Example usage
26
image_path = "image.jpg"
27
predictions = classify_image(image_path)
28

29
# Print the top 3 predictions
30
for prediction in predictions:
31
    print(f"{prediction[1]}: {prediction[2]*100:.2f}%")
32

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.