raj999 commited on
Commit
f5689ed
1 Parent(s): 0cef2bb

Create predict_copy.py

Browse files
Files changed (1) hide show
  1. predict_copy.py +110 -0
predict_copy.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
2
+ from tensorflow.keras.preprocessing import image
3
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
4
+ from tensorflow.keras.models import Model
5
+ import numpy as np
6
+ from scipy.spatial.distance import euclidean
7
+ from sklearn.metrics.pairwise import cosine_similarity
8
+ # Load VGG16 model + higher level layers
9
+ base_model = VGG16(weights='imagenet')
10
+ model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
11
+
12
+ # Define data augmentation
13
+ datagen = ImageDataGenerator(
14
+ rotation_range=20,
15
+ width_shift_range=0.2,
16
+ height_shift_range=0.2,
17
+ shear_range=0.2,
18
+ zoom_range=0.2,
19
+ horizontal_flip=True,
20
+ fill_mode='nearest'
21
+ )
22
+
23
+ def extract_features(img):
24
+ img = img.resize((224, 224)) # Ensure the image is resized to the input size expected by VGG16
25
+ img_data = image.img_to_array(img)
26
+ img_data = np.expand_dims(img_data, axis=0)
27
+ img_data = preprocess_input(img_data)
28
+ features = model.predict(img_data)
29
+ return features.flatten() # Flatten the features to a 1-D vector
30
+
31
+ def augment_image(img):
32
+ x = image.img_to_array(img)
33
+ x = x.reshape((1,) + x.shape) # Reshape to (1, height, width, channels)
34
+
35
+ # Generate batches of augmented images
36
+ augmented_images = []
37
+ for batch in datagen.flow(x, batch_size=1):
38
+ augmented_images.append(image.array_to_img(batch[0]))
39
+ if len(augmented_images) >= 5: # Generate 5 augmented images
40
+ break
41
+ return augmented_images
42
+
43
+ def extract_features_with_augmentation(img_path):
44
+ original_img = image.load_img(img_path)
45
+ augmented_images = augment_image(original_img)
46
+
47
+ # Extract features from the original image
48
+ features = [extract_features(original_img)]
49
+
50
+ # Extract features from augmented images
51
+ for aug_img in augmented_images:
52
+ features.append(extract_features(aug_img))
53
+
54
+ return np.mean(features, axis=0) # Return the average feature vector
55
+
56
+
57
+ def extract_features_with_augmentation_cp(img_path):
58
+ pil_img = pil_img.resize((224, 224)) # (224, 224)
59
+
60
+ # Convert the PIL image to a numpy array
61
+
62
+ augmented_images = augment_image(pil_img)
63
+
64
+ # Extract features from the original image
65
+ features = [extract_features(augmented_images)]
66
+
67
+ # Extract features from augmented images
68
+ for aug_img in augmented_images:
69
+ features.append(extract_features(aug_img))
70
+
71
+ return np.mean(features, axis=0) # Return the average feature vector
72
+
73
+
74
+
75
+ def compare_features(features1, features2):
76
+ # Euclidean distance
77
+ euclidean_dist = euclidean(features1, features2)
78
+
79
+ # Cosine similarity
80
+ cos_sim = cosine_similarity([features1], [features2])[0][0]
81
+
82
+ return euclidean_dist, cos_sim
83
+
84
+ def predict_similarity(features1, features2, threshold=0.5):
85
+ _, cos_sim = compare_features(features1, features2)
86
+ similarity_score = cos_sim
87
+ # print(similarity_score)
88
+
89
+ if similarity_score > threshold:
90
+ return True
91
+ else:
92
+ return False
93
+
94
+ if __name__ == '__main__':
95
+ # Example usage
96
+ img_path1 = "D:/Downloads/image/rose.jpg"
97
+ img_path2 = "D:/Downloads/image/rose3.jpg"
98
+
99
+ # Extract features
100
+ features1 = extract_features_with_augmentation(img_path1)
101
+ features2 = extract_features_with_augmentation(img_path2)
102
+
103
+ # Compare features
104
+ euclidean_dist, cos_sim = compare_features(features1, features2)
105
+ print(f'Euclidean Distance: {euclidean_dist}')
106
+ print(f'Cosine Similarity: {cos_sim}')
107
+
108
+ # Predict similarity
109
+ is_similar = predict_similarity(features1, features2, threshold=0.8)
110
+ print(f'Are the images similar? {"Yes" if is_similar else "No"}')