File size: 2,796 Bytes
663d702
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing import image
from keras.models import Model
import numpy as np
from scipy.spatial.distance import euclidean
from sklearn.metrics.pairwise import cosine_similarity
from PIL import Image 
from keras.applications.efficientnet import EfficientNetB0
# Load VGG16 model + higher level layers
base_model = VGG16(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
# Load EfficientNetB0 model + higher level layers
# base_model = EfficientNetB0(weights='imagenet')
# model = Model(inputs=base_model.input, outputs=base_model.get_layer('top_activation').output)

def extract_features_cp(pil_img: Image.Image) -> np.ndarray:
    # Resize the image to the target size
    pil_img = pil_img.resize((224, 224))  # (224, 224)
    
    # Convert the PIL image to a numpy array
    img_data = image.img_to_array(pil_img)
    
    # Expand dimensions to match the input shape required by the model
    img_data = np.expand_dims(img_data, axis=0)
    
    # Preprocess the image data
    img_data = preprocess_input(img_data)
    
    # Predict the features using the model
    features = model.predict(img_data)
    
    # Return the features as a flattened array
    return features.flatten()

def extract_features(img_path):
    img = image.load_img(img_path, target_size=(224, 224))  # (224, 224)
    img_data = image.img_to_array(img)
    img_data = np.expand_dims(img_data, axis=0)
    img_data = preprocess_input(img_data)
    features = model.predict(img_data)
    return features.flatten()  # Flatten the features to a 1-D vector

def compare_features(features1, features2):
    # Euclidean distance
    euclidean_dist = euclidean(features1, features2)
    
    # Cosine similarity
    cos_sim = cosine_similarity([features1], [features2])[0][0]
    
    return euclidean_dist, cos_sim

def predict_similarity(features1, features2, threshold=0.5):
    _, cos_sim = compare_features(features1, features2)
    similarity_score = cos_sim
    # print(similarity_score)
    
    if similarity_score > threshold:
        return True
    else:
        return False



if __name__ == '__main__':
    # Example usage
    img_path1 = "D:/Downloads/image/rose.jpg"
    img_path2 = "D:/Downloads/image/rose.jpg"

    # Extract features
    features1 = extract_features(img_path1)
    features2 = extract_features(img_path2)

    # Compare features
    euclidean_dist, cos_sim = compare_features(features1, features2)
    print(f'Euclidean Distance: {euclidean_dist}')
    print(f'Cosine Similarity: {cos_sim}')

    # Predict similarity
    is_similar = predict_similarity(features1, features2, threshold=0.8)
    print(f'Are the images similar? {"Yes" if is_similar else "No"}')