Spaces:
Build error
Build error
Create predict.py
Browse files- predict.py +80 -0
predict.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.applications.vgg16 import VGG16, preprocess_input
|
| 2 |
+
from keras.preprocessing import image
|
| 3 |
+
from keras.models import Model
|
| 4 |
+
import numpy as np
|
| 5 |
+
from scipy.spatial.distance import euclidean
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from keras.applications.efficientnet import EfficientNetB0
|
| 9 |
+
# Load VGG16 model + higher level layers
|
| 10 |
+
base_model = VGG16(weights='imagenet')
|
| 11 |
+
model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
|
| 12 |
+
# Load EfficientNetB0 model + higher level layers
|
| 13 |
+
# base_model = EfficientNetB0(weights='imagenet')
|
| 14 |
+
# model = Model(inputs=base_model.input, outputs=base_model.get_layer('top_activation').output)
|
| 15 |
+
|
| 16 |
+
def extract_features_cp(pil_img: Image.Image) -> np.ndarray:
|
| 17 |
+
# Resize the image to the target size
|
| 18 |
+
pil_img = pil_img.resize((224, 224)) # (224, 224)
|
| 19 |
+
|
| 20 |
+
# Convert the PIL image to a numpy array
|
| 21 |
+
img_data = image.img_to_array(pil_img)
|
| 22 |
+
|
| 23 |
+
# Expand dimensions to match the input shape required by the model
|
| 24 |
+
img_data = np.expand_dims(img_data, axis=0)
|
| 25 |
+
|
| 26 |
+
# Preprocess the image data
|
| 27 |
+
img_data = preprocess_input(img_data)
|
| 28 |
+
|
| 29 |
+
# Predict the features using the model
|
| 30 |
+
features = model.predict(img_data)
|
| 31 |
+
|
| 32 |
+
# Return the features as a flattened array
|
| 33 |
+
return features.flatten()
|
| 34 |
+
|
| 35 |
+
def extract_features(img_path):
|
| 36 |
+
img = image.load_img(img_path, target_size=(224, 224)) # (224, 224)
|
| 37 |
+
img_data = image.img_to_array(img)
|
| 38 |
+
img_data = np.expand_dims(img_data, axis=0)
|
| 39 |
+
img_data = preprocess_input(img_data)
|
| 40 |
+
features = model.predict(img_data)
|
| 41 |
+
return features.flatten() # Flatten the features to a 1-D vector
|
| 42 |
+
|
| 43 |
+
def compare_features(features1, features2):
|
| 44 |
+
# Euclidean distance
|
| 45 |
+
euclidean_dist = euclidean(features1, features2)
|
| 46 |
+
|
| 47 |
+
# Cosine similarity
|
| 48 |
+
cos_sim = cosine_similarity([features1], [features2])[0][0]
|
| 49 |
+
|
| 50 |
+
return euclidean_dist, cos_sim
|
| 51 |
+
|
| 52 |
+
def predict_similarity(features1, features2, threshold=0.5):
|
| 53 |
+
_, cos_sim = compare_features(features1, features2)
|
| 54 |
+
similarity_score = cos_sim
|
| 55 |
+
# print(similarity_score)
|
| 56 |
+
|
| 57 |
+
if similarity_score > threshold:
|
| 58 |
+
return True
|
| 59 |
+
else:
|
| 60 |
+
return False
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
# Example usage
|
| 66 |
+
img_path1 = "D:/Downloads/image/rose.jpg"
|
| 67 |
+
img_path2 = "D:/Downloads/image/rose.jpg"
|
| 68 |
+
|
| 69 |
+
# Extract features
|
| 70 |
+
features1 = extract_features(img_path1)
|
| 71 |
+
features2 = extract_features(img_path2)
|
| 72 |
+
|
| 73 |
+
# Compare features
|
| 74 |
+
euclidean_dist, cos_sim = compare_features(features1, features2)
|
| 75 |
+
print(f'Euclidean Distance: {euclidean_dist}')
|
| 76 |
+
print(f'Cosine Similarity: {cos_sim}')
|
| 77 |
+
|
| 78 |
+
# Predict similarity
|
| 79 |
+
is_similar = predict_similarity(features1, features2, threshold=0.8)
|
| 80 |
+
print(f'Are the images similar? {"Yes" if is_similar else "No"}')
|