0% found this document useful (0 votes)
7 views

Image Recognition

gggggggggggfddddddddddddd

Uploaded by

dineshkanna334
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Image Recognition

gggggggggggfddddddddddddd

Uploaded by

dineshkanna334
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import tensorflow as tf

from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,


preprocess_input, decode_predictions
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
from PIL import Image

# Load the pre-trained MobileNetV2 model


model = MobileNetV2(weights='imagenet')

# Function to recognize the image


def recognize_image(image_path):
try:
# Load and preprocess the image
img = load_img(image_path, target_size=(224, 224))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)

# Predict the class of the image


predictions = model.predict(img_array)
decoded_predictions = decode_predictions(predictions, top=3)

# Print the predictions


for i, (imagenet_id, label, score) in enumerate(decoded_predictions[0]):
print(f"{i + 1}. {label}: {score:.2%}")
except FileNotFoundError:
print("Image not found. Please check the path.")
except Exception as e:
print(f"An error occurred: {e}")
print("Image not recognized.")

# Function to upload and recognize an image (modified)


def upload_and_recognize():
image_path = input("Enter the full path to your image (including the filename
and extension): ")
if image_path:
recognize_image(image_path)

# Call the function to upload and recognize an image


upload_and_recognize()

You might also like