-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_recognition.py
More file actions
41 lines (31 loc) · 1.27 KB
/
Copy pathimage_recognition.py
File metadata and controls
41 lines (31 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
import tensorflow as tf
from PIL import Image
def load_model_and_labels():
model = tf.keras.applications.MobileNetV2(weights='imagenet')
labels_path = tf.keras.utils.get_file(
'ImageNetLabels.txt',
'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
with open(labels_path) as file:
labels = file.read().splitlines()[1:]
return model, labels
def preprocess_image(image_path):
image = Image.open(image_path).resize((224, 224))
image_array = np.array(image) / 255.0
image_batch = np.expand_dims(image_array, axis=0)
return image_batch
def recognize_image(model, labels, image_path):
image_batch = preprocess_image(image_path)
predictions = model.predict(image_batch)
top_prediction = np.argmax(predictions[0])
label = labels[top_prediction]
confidence = round(predictions[0][top_prediction] * 100, 2)
return label, confidence
# Load the model and labels
model, labels = load_model_and_labels()
# Image path
image_path = "/home/levon/Downloads/P1001658.JPG"
# Perform image recognition
label, confidence = recognize_image(model, labels, image_path)
print(f"Recognized object: {label}")
print(f"Confidence: {confidence}%")