Skip to content

Comparing images in Python using CV and Numpy -Finding duplicates and displaying them | HOXFRAMEWORK

Posted in VIDEOS

Welcome back!

In this tutorial we will be comparing images using NumPy and CV2 modules along with pre-installed os and random modules.

So all we have to do is specify the directories in which images are located, and have some duplicates there (visually, the name of the file doesnt matter). This cannot be done with a hash of a file so we have to use CV2 and Numpy
In the code we double check everything because our .shape command makes the picture to be 3 numbers only. So once we confirm similarities there we are forwarding it to our if statement np.array_equal(b,a) which compares all numbers. Why? Well because first 3 numbers can be the same regardless of if the pictures are the same- meaning it will give false positives. This is how we deep-check it .

CODE:

to wipe duplicates (after download).

def checkmypics():
“””Checks pics u downloaded.”””
#
import numpy as np
import cv2
import os
import random
from itertools import combinations
#2 dirs:
animals_dir = [‘./doggos/’,’./cattos/’] #how many times to scan ALL; Put the same dir twice to can it twice
for directory in animals_dir:
dirlister = os.listdir(directory)
#For every pic in the directory
for a, b in combinations(dirlister, 2):
if a == b:
pass
else:
imagenameone = a
imagenametwo = b
imgnddirtwo = directory + b
a = directory + a
b = directory + b
#print(“Comparing: “,b ,” with “,a)
a = cv2.imread(a)
b = cv2.imread(b)
img1 = a.shape
img2 = b.shape
#print(img1,img2)
if img1 == img2:
#print(“Found image one’s shape:”,img1,”and it is similar with image two’s shape:”,img2)
#print(“[+] COPY FOUND ! The images are the same.\n”)
#
#now we gotta double check.. cause now we are working with 3 numbers
#and it gives false positives sometimes
#so with this one we will completely check the numbers
if np.array_equal(b,a):
print(imagenameone,” is a copy of “, imagenametwo)
print(“Deleting the second image…%s”% imgnddirtwo)
#################os.remove(imgnddirtwo)

                else:
                    #print("False positive. Skipping.")
                    pass
            else:
                #print("[-] The images are different.")
                pass
##        except Exception:
##            print("[---]error")

checkmypics()

And thats it ! Thank you so much for watching and have a nice day 🙂