-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchitecture.py
43 lines (36 loc) · 1.67 KB
/
Architecture.py
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
42
43
import tensorflow as tf
import cv2
import numpy as np
import os
# Function to load and preprocess an image
def preprocess_image(image_path):
if os.path.exists(image_path):
image = cv2.imread(image_path)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (224, 224))
image_preprocessed = tf.keras.applications.mobilenet_v2.preprocess_input(image_resized)
return image_preprocessed
else:
print(f"File {image_path} does not exist or path is incorrect.")
return None
# Load the pre-trained MobileNetV2 model
model = tf.keras.applications.MobileNetV2(weights="imagenet")
# Function to predict landmarks and display results
def predict_landmarks(image_path):
image = preprocess_image(image_path)
if image is not None:
# Make predictions using the model
predictions = model.predict(np.expand_dims(image, axis=0))
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=5)[0]
# Display the top 5 predicted landmarks
for i, (imagenet_id, label, score) in enumerate(decoded_predictions):
print(f"{i + 1}: {label} ({score:.2f})")
# Display the image with annotations
image_display = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
cv2.imshow("Landmark Recognition", image_display)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Example usage
if __name__ == "__main__":
image_path = "C:\\Users\\acer\\OneDrive\\Desktop\\Bnmit\\Projects & Assignments\\Landmark detection of a place\\t.jpg"
predict_landmarks(image_path)