diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..fc9604c5bd905a7f7b0b2da5b6a81d1483dfff97 --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +from PIL import Image +import numpy as np +from tensorflow.keras.applications import VGG16 +from tensorflow.keras.models import Model +from tensorflow.keras.preprocessing.image import img_to_array +from sklearn.metrics.pairwise import cosine_similarity +from flask import Flask, request, render_template +import os + +# Constants +IMAGE_SIZE = (224, 224) +SIMILARITY_THRESHOLD = 0.8 # Default value + +def resize_image(image, size=IMAGE_SIZE): + return image.resize(size) + +def normalize_image(image): + return np.array(image) / 255.0 + +def extract_features(image): + base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) + model = Model(inputs=base_model.input, outputs=base_model.layers[-1].output) + image = img_to_array(image) + image = np.expand_dims(image, axis=0) + return model.predict(image).flatten() + +def find_most_similar(input_features, image_features): + similarities = cosine_similarity([input_features], image_features) + similar_indices = [i for i, score in enumerate(similarities[0]) if score >= SIMILARITY_THRESHOLD] + return similar_indices + +def compare_images(input_image, comparison_images): + input_image = resize_image(input_image) + input_image = normalize_image(input_image) + input_features = extract_features(input_image) + + comparison_features = [] + image_names = [] + + static_folder = os.path.join('static') + if not os.path.exists(static_folder): + os.makedirs(static_folder) + + for i, image in enumerate(comparison_images): + image_name = f"image_{i}.png" + image_path = os.path.join(static_folder, image_name) + + # Save the image using PIL Image object + img = resize_image(image) + img.save(image_path) + + img_array = normalize_image(img) + comparison_features.append(extract_features(img_array)) + image_names.append(image_name) + + most_similar_indices = find_most_similar(input_features, comparison_features) + similar_images = [image_names[index] for index in most_similar_indices] + + return similar_images + +app = Flask(__name__) + +@app.route('/', methods=['GET', 'POST']) +def upload_file(): + global SIMILARITY_THRESHOLD + + if request.method == 'POST': + try: + # Read similarity threshold from form + SIMILARITY_THRESHOLD = float(request.form.get('similarity_threshold', 0.8)) + + input_image = request.files['input_image'] + comparison_images = request.files.getlist('comparison_images') + + # Ensure input image is not empty + if input_image.filename == '': + return "No selected file for input image." + + # Read and process input image + input_image = Image.open(input_image.stream) + + # Process comparison images + comparison_images_pil = [Image.open(img.stream) for img in comparison_images] + + similar_images = compare_images(input_image, comparison_images_pil) + + return render_template('result.html', images=similar_images) + + except Exception as e: + # Print the error details for debugging + print(f"Error: {e}") + return "An error occurred. Please try again." + + return render_template('index.html') + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000000000000000000000000000000000000..5417646f8a676f0aed87bf1adab76f812f98abe5 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,76 @@ + + + + + + Image Comparison + + + +
+

Upload Images for Comparison

+
+ + + + + + +
+ + +
+ + +
+
+ + diff --git a/templates/result.html b/templates/result.html new file mode 100644 index 0000000000000000000000000000000000000000..70d899bc42a3c73c474c9ab49a45ed939c318412 --- /dev/null +++ b/templates/result.html @@ -0,0 +1,20 @@ + + + + + + Similar Images + + + +

Similar Images

+ + Try Again + + diff --git a/test_folder/download (1).jpeg b/test_folder/download (1).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..9ea85e9e8ceef1629f34dd4de7ee5ae20d20b3ad Binary files /dev/null and b/test_folder/download (1).jpeg differ diff --git a/test_folder/download (10).jpeg b/test_folder/download (10).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..97d5fd92fd804699e75238302062d26b4e461185 Binary files /dev/null and b/test_folder/download (10).jpeg differ diff --git a/test_folder/download (11).jpeg b/test_folder/download (11).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..35e777e68f8e9470e3ce8bc1469ae74e4bc1fd27 Binary files /dev/null and b/test_folder/download (11).jpeg differ diff --git a/test_folder/download (12).jpeg b/test_folder/download (12).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..a7dc83e57c0009bc7ea334fa9c4fd1e6e2f76559 Binary files /dev/null and b/test_folder/download (12).jpeg differ diff --git a/test_folder/download (13).jpeg b/test_folder/download (13).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..0c03c0e903df273563086de3c8a2dab8b32ecae7 Binary files /dev/null and b/test_folder/download (13).jpeg differ diff --git a/test_folder/download (14).jpeg b/test_folder/download (14).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..341ef469873dff5408b62c47e5ddc67fddc7bee2 Binary files /dev/null and b/test_folder/download (14).jpeg differ diff --git a/test_folder/download (15).jpeg b/test_folder/download (15).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..11a68b44cc72269eef1bcf24387d17c97e58f2a8 Binary files /dev/null and b/test_folder/download (15).jpeg differ diff --git a/test_folder/download (16).jpeg b/test_folder/download (16).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e22efa07956c3e5fa1e81394d929522d37a76de2 Binary files /dev/null and b/test_folder/download (16).jpeg differ diff --git a/test_folder/download (17).jpeg b/test_folder/download (17).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..5e07f59d351de8153fac0e57610a27a00dc7371a Binary files /dev/null and b/test_folder/download (17).jpeg differ diff --git a/test_folder/download (18).jpeg b/test_folder/download (18).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..aa62663b2c62047200a6ed6667e1e15a90a3232d Binary files /dev/null and b/test_folder/download (18).jpeg differ diff --git a/test_folder/download (19).jpeg b/test_folder/download (19).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..444f77a66e96162e3d4ddc9835dd7178816b8fb6 Binary files /dev/null and b/test_folder/download (19).jpeg differ diff --git a/test_folder/download (2).jpeg b/test_folder/download (2).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..b4b5f413f5f67bea3f7a87a9b9e036410c9e29ed Binary files /dev/null and b/test_folder/download (2).jpeg differ diff --git a/test_folder/download (20).jpeg b/test_folder/download (20).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..342bbd12fcc42a2e5cfca4ec3f8249c18bc1844c Binary files /dev/null and b/test_folder/download (20).jpeg differ diff --git a/test_folder/download (21).jpeg b/test_folder/download (21).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..048ba6ac46d395784e0ce3551e24e8bc353c3ca4 Binary files /dev/null and b/test_folder/download (21).jpeg differ diff --git a/test_folder/download (22).jpeg b/test_folder/download (22).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..37be0061b062a05b797e5a02cfb26d4063701b82 Binary files /dev/null and b/test_folder/download (22).jpeg differ diff --git a/test_folder/download (23).jpeg b/test_folder/download (23).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..48a7168890bb3b9e5cb9397829099a228677587a Binary files /dev/null and b/test_folder/download (23).jpeg differ diff --git a/test_folder/download (24).jpeg b/test_folder/download (24).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..50f8defc0e067a3a9f87d292a0c019b07390e6c4 Binary files /dev/null and b/test_folder/download (24).jpeg differ diff --git a/test_folder/download (25).jpeg b/test_folder/download (25).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..d8560045f9a4607a8ccdd2ebdb145b067555fb58 Binary files /dev/null and b/test_folder/download (25).jpeg differ diff --git a/test_folder/download (26).jpeg b/test_folder/download (26).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..01ce2fbc237bdb8e531449e9e8bbf35749c56d7a Binary files /dev/null and b/test_folder/download (26).jpeg differ diff --git a/test_folder/download (27).jpeg b/test_folder/download (27).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..b8b642f14ccbb4256ff481769a9507660af3cd79 Binary files /dev/null and b/test_folder/download (27).jpeg differ diff --git a/test_folder/download (28).jpeg b/test_folder/download (28).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..5e64cf98b847af36bb904279522d2a606a75c22b Binary files /dev/null and b/test_folder/download (28).jpeg differ diff --git a/test_folder/download (29).jpeg b/test_folder/download (29).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..dac549ee41ff0410b2ef424de539fa80646132a9 Binary files /dev/null and b/test_folder/download (29).jpeg differ diff --git a/test_folder/download (3).jpeg b/test_folder/download (3).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..28549132dcc73e9c98b4c65561bf6b096bd5da1c Binary files /dev/null and b/test_folder/download (3).jpeg differ diff --git a/test_folder/download (30).jpeg b/test_folder/download (30).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..94d31cf65d4808f0977cf4a2434a62398ac6f663 Binary files /dev/null and b/test_folder/download (30).jpeg differ diff --git a/test_folder/download (4).jpeg b/test_folder/download (4).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..4099e670b32e4bcfd52cdff8b27b26979f158b26 Binary files /dev/null and b/test_folder/download (4).jpeg differ diff --git a/test_folder/download (5).jpeg b/test_folder/download (5).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..782fd773ffee7cd256fccb7d61f7c3cd7c2982c2 Binary files /dev/null and b/test_folder/download (5).jpeg differ diff --git a/test_folder/download (6).jpeg b/test_folder/download (6).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..aca016139340c123e83aacdd2b21ff5af8a8fd9d Binary files /dev/null and b/test_folder/download (6).jpeg differ diff --git a/test_folder/download (7).jpeg b/test_folder/download (7).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..6b034da632743afce69815b4d253e6c3d445be94 Binary files /dev/null and b/test_folder/download (7).jpeg differ diff --git a/test_folder/download (8).jpeg b/test_folder/download (8).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e1002022b0ee0931ee1eacbd6cab320edf6e837b Binary files /dev/null and b/test_folder/download (8).jpeg differ diff --git a/test_folder/download (9).jpeg b/test_folder/download (9).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..8d173aa904b23bdeb8975a6a94c4096bd237143b Binary files /dev/null and b/test_folder/download (9).jpeg differ diff --git a/test_folder/download.jpeg b/test_folder/download.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..a0e0cf08429c438864c68e313bba57fe57751bca Binary files /dev/null and b/test_folder/download.jpeg differ diff --git a/test_folder/flowers/download (10).jpeg b/test_folder/flowers/download (10).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..97d5fd92fd804699e75238302062d26b4e461185 Binary files /dev/null and b/test_folder/flowers/download (10).jpeg differ diff --git a/test_folder/flowers/download (11).jpeg b/test_folder/flowers/download (11).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..35e777e68f8e9470e3ce8bc1469ae74e4bc1fd27 Binary files /dev/null and b/test_folder/flowers/download (11).jpeg differ diff --git a/test_folder/flowers/download (12).jpeg b/test_folder/flowers/download (12).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..a7dc83e57c0009bc7ea334fa9c4fd1e6e2f76559 Binary files /dev/null and b/test_folder/flowers/download (12).jpeg differ diff --git a/test_folder/flowers/download (13).jpeg b/test_folder/flowers/download (13).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..0c03c0e903df273563086de3c8a2dab8b32ecae7 Binary files /dev/null and b/test_folder/flowers/download (13).jpeg differ diff --git a/test_folder/flowers/download (14).jpeg b/test_folder/flowers/download (14).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..341ef469873dff5408b62c47e5ddc67fddc7bee2 Binary files /dev/null and b/test_folder/flowers/download (14).jpeg differ diff --git a/test_folder/flowers/download (15).jpeg b/test_folder/flowers/download (15).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..11a68b44cc72269eef1bcf24387d17c97e58f2a8 Binary files /dev/null and b/test_folder/flowers/download (15).jpeg differ diff --git a/test_folder/flowers/download (16).jpeg b/test_folder/flowers/download (16).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e22efa07956c3e5fa1e81394d929522d37a76de2 Binary files /dev/null and b/test_folder/flowers/download (16).jpeg differ diff --git a/test_folder/flowers/download (6).jpeg b/test_folder/flowers/download (6).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..aca016139340c123e83aacdd2b21ff5af8a8fd9d Binary files /dev/null and b/test_folder/flowers/download (6).jpeg differ diff --git a/test_folder/flowers/download (7).jpeg b/test_folder/flowers/download (7).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..6b034da632743afce69815b4d253e6c3d445be94 Binary files /dev/null and b/test_folder/flowers/download (7).jpeg differ diff --git a/test_folder/flowers/download (8).jpeg b/test_folder/flowers/download (8).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..e1002022b0ee0931ee1eacbd6cab320edf6e837b Binary files /dev/null and b/test_folder/flowers/download (8).jpeg differ diff --git a/test_folder/flowers/download (9).jpeg b/test_folder/flowers/download (9).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..8d173aa904b23bdeb8975a6a94c4096bd237143b Binary files /dev/null and b/test_folder/flowers/download (9).jpeg differ diff --git a/test_folder/flowers/images (1).jpeg b/test_folder/flowers/images (1).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..3efda8cbf97d0c524fed63a496fe857e5ad2ff67 Binary files /dev/null and b/test_folder/flowers/images (1).jpeg differ diff --git a/test_folder/flowers/images (2).jpeg b/test_folder/flowers/images (2).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..9d5c387fb074a97373cd7fb543a5ecaa11f9f1c5 Binary files /dev/null and b/test_folder/flowers/images (2).jpeg differ diff --git a/test_folder/flowers/images.jpeg b/test_folder/flowers/images.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..126c219700fbed61a7454dfc3835466c69535d1d Binary files /dev/null and b/test_folder/flowers/images.jpeg differ diff --git a/test_folder/images (1).jpeg b/test_folder/images (1).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..3efda8cbf97d0c524fed63a496fe857e5ad2ff67 Binary files /dev/null and b/test_folder/images (1).jpeg differ diff --git a/test_folder/images (2).jpeg b/test_folder/images (2).jpeg new file mode 100644 index 0000000000000000000000000000000000000000..9d5c387fb074a97373cd7fb543a5ecaa11f9f1c5 Binary files /dev/null and b/test_folder/images (2).jpeg differ diff --git a/test_folder/images.jpeg b/test_folder/images.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..126c219700fbed61a7454dfc3835466c69535d1d Binary files /dev/null and b/test_folder/images.jpeg differ