Skip to content

Instantly share code, notes, and snippets.

@Abusheik008
Created April 13, 2022 05:10
Show Gist options
  • Save Abusheik008/1f1457668be37b8e94dc92ab65ea6a0b to your computer and use it in GitHub Desktop.
Save Abusheik008/1f1457668be37b8e94dc92ab65ea6a0b to your computer and use it in GitHub Desktop.
import time
import cv2
import shutil
from tqdm import tqdm
import numpy as np
import os
#Add the path of cropped folder in which all the classes folder presents
cropped_folder_path = r"D:\Abu\My Learnings\Classes Validation\DataBox\Classes Validation\cropped_images"
#Add the path of error folder in which the error classes will be stored
error_folder_path = r"D:\Abu\My Learnings\Classes Validation\DataBox\Classes Validation\Error"
folder_dic, master_dic_full, master_dic, Images_to_compare, Title, Original_Title = {}, {}, {}, [], [], []
percentage_of_similarity, Percentage_of_Non_similarity = 0, 0
#By using swift and flann we will find the image similarity
sift = cv2.SIFT_create()
index_params = dict(algorithm=0, trees=1)
search_params = dict()
#In this loop we will be taking the count or no of folders which presents inside the cropped folder
for count, folder in enumerate(os.listdir(cropped_folder_path)):
folder_dic[folder] = count
if not os.path.exists(os.path.join(error_folder_path, folder)):
os.mkdir((os.path.join(error_folder_path, folder)))
#In these loops we will be matching one class with all other classes and move the images to the error folder
for main_folder, index in folder_dic.items():
time.sleep(1)
print("Main Folder ->", main_folder)
for master_images_path in os.listdir(os.path.join(cropped_folder_path, main_folder)):
master_images = cv2.imread(cropped_folder_path + "/" + main_folder + "/" + master_images_path)
for check_folder in os.listdir(cropped_folder_path):
if folder_dic[check_folder] > index:
time.sleep(2)
for check_image_path in tqdm(os.listdir(os.path.join(cropped_folder_path, check_folder))):
check_image = cv2.imread(cropped_folder_path + "/" + check_folder + "/" + check_image_path)
if master_images.shape == check_image.shape:
difference = cv2.subtract(master_images, check_image)
b, g, r = cv2.split(difference)
if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
print("Similarity: 100%")
flann = cv2.FlannBasedMatcher()
kp_1, desc_1 = sift.detectAndCompute(master_images, None)
kp_2, desc_2 = sift.detectAndCompute(check_image, None)
matches = flann.knnMatch(desc_1, desc_2, k=2)
similar_images, Non_similar_images = [], []
for m, n in matches:
if m.distance < 0.75 * n.distance:
similar_images.append([m])
else:
Non_similar_images.append([m])
number_key_points = 0
if len(kp_1) <= len(kp_2):
number_key_points = len(kp_1)
else:
number_key_points = len(kp_2)
percentage_of_similarity = len(similar_images) / number_key_points * 100
Percentage_of_Non_similarity = len(Non_similar_images) / number_key_points * 100
if percentage_of_similarity > Percentage_of_Non_similarity:
print("Title: " + os.path.join(cropped_folder_path, master_images_path),
file=open("output.txt", "a"))
print("Original Title: " + os.path.join(cropped_folder_path, check_folder),
file=open(error_folder_path + "/" + check_folder + "/output.txt", "a"))
print("similarity:" + str(int(percentage_of_similarity)) + "%\n",
file=open(error_folder_path + "/" + check_folder + "/output.txt", "a"))
print("Non_similarity:" + str(int(Percentage_of_Non_similarity)) + "%\n",
file=open(error_folder_path + "/" + check_folder + "/output.txt", "a"))
print("Same classes occurred",
file=open(error_folder_path + "/" + check_folder + "/output.txt", "a"))
error_save_variable = error_folder_path + "/" + check_folder
if os.path.exists(os.path.join(error_save_variable, check_image_path)):
os.remove(os.path.join(error_save_variable, check_image_path))
else:
shutil.move((cropped_folder_path + "/" + check_folder + "/" + check_image_path),
error_save_variable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment