Computer vision is a field of artificial intelligence that trains computers to interpret and understand the visual world. Using digital images from cameras and videos and deep learning models, machines can accurately identify and classify objects — and then react to what they “see”.
Computer vision tasks include methods for acquiring, processing, analyzing and understanding digital images
Computer Vision in Image processing is mainly focused on processing the raw input images to enhance them or preparing them to do other tasks. Computer vision is focused on extracting information from the input images or videos to have a proper understanding of them to predict the visual input like the human brain.
The purpose of this project is to detect tampering/fraud of PAN cards using computer vision. This project will help the different organizations in detecting whether the Id i.e. the PAN card provided to them by their employees or customers or anyone is original or not.
For this project we will calculate the structural similarity of the original PAN card and the PAN card uploaded by the user – This is the soul of this project we will discuss it thoroughly later in this blog.
Similarly in this project with the help of image processing involving the techniques of computer vision we are going to detect that whether the given image of the PAN card is original or tampered (fake) PAN card.
1. Import necessary libraries
2. Scraping the tampered and original pan card from the website
3. Scaling down the shape of the tampered image as the original image
4. Read original and tampered image
5. Converting an image into a grayscale image
6. Applying Structural Similarity Index (SSIM) technique between the two images
7. Calculate Threshold and contours and
8. Experience real-time contours and threshold on images
from skimage.metrics import structural_similarity import imutils import cv2 from PIL import Image import requests
Make folders and sub-folders for storing images, you may create it manually it’s completely up to you (Optional).
!mkdir pan_card_tampering !mkdir pan_card_tampering/image
original = Image.open(requests.get('https://www.thestatesman.com/wp-content/uploads/2019/07/pan-card.jpg', stream=True).raw) tampered = Image.open(requests.get('https://assets1.cleartax-cdn.com/s/img/20170526124335/Pan4.png', stream=True).raw)
In the above code snippet, we are web scarping the images from different sources using the requests library.
As you can see in the above output, The original size of the original image and the original size of tampered image are different which will result in unwanted/false results while doing image processing, that’s why scaling down both the image to equal shape is prominently needed.
# Resize Image original = original.resize((250, 160)) print(original.size) original.save('pan_card_tampering/image/original.png')#Save image tampered = tampered.resize((250,160)) print(tampered.size) tampered.save('pan_card_tampering/image/tampered.png')#Saves image
Output :
(250, 160) (250, 160)
Now, if you will see the output the shape of both the images (Original image and tampered image) is scaled down to equal shape i.e. (250,160). Now the image processing will be smoother and more accurate than it was before.
# Change image type if required from png to jpg tampered = Image.open('pan_card_tampering/image/tampered.png') tampered.save('pan_card_tampering/image/tampered.png')#can do png to jpg
# Display original image original
Output :
# Display user given image tampered
Output :
# load the two input images original = cv2.imread('pan_card_tampering/image/original.png') tampered = cv2.imread('pan_card_tampering/image/tampered.png')
Now in the above code, we are reading both the images (Original and Tampered) using cv2’s imread() function.
# Convert the images to grayscale original_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY) tampered_gray = cv2.cvtColor(tampered, cv2.COLOR_BGR2GRAY)
In the above code, we have converted the original images (Original pan card and user given Pan card) to gray-scale images using cv2’s function cvtColor() which have parameter as cv2.COLOR_BGR2GRAY.
But why we need to convert them into grayscale? Here’s the reason why :
Hold on ! First we need to understand what is SSIM !
What is SSIM?
The Structural Similarity Index (SSIM) is a perceptual metric that quantifies the image quality degradation that is caused by processing such as data compression or by losses in data transmission.
How SSIM perform its function?
This metric is basically a full reference that requires 2 images from the same shot, this means 2 graphically identical images to the human eye. The second image generally is compressed or has a different quality, which is the goal of this index.
What is the real-world use of SSIM?
SSIM is usually used in the video industry but has as well a strong application in photography.
How SSIM helps in detection?
SSIM actually measures the perceptual difference between two similar images. It cannot judge which of the two is better: that must be inferred from knowing which is the original one and which has been exposed to additional processing such as compression or filters.
# Compute the Structural Similarity Index (SSIM) between the two images, # ensuring that the difference image is returned (score, diff) = structural_similarity(original_gray, tampered_gray, full=True) diff = (diff * 255).astype("uint8") print("SSIM Score is : {}".format(score*100)) if score >= 80: print ("The given pan card is original") else: print("The given pan card is tampered")
Output :
SSIM Score is : 31.678790332739425 The given pan card is tampered
Let’s break down what just happened in the above code!
Contours detection is a process that can be explained simply as a curve joining all the continuous points (along with the boundary), having the same color or intensity. The algorithm does indeed find edges of images but also puts them in a hierarchy.
# Calculating threshold and contours thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts)
Here we are using the threshold function of computer vision which applies an adaptive threshold to the image which is stored in the form array. This function transforms the grayscale image into a binary image using a mathematical formula.
Find contours works on binary image and retrieve the contours. These contours are a useful tool for shape analysis and recognition. Grab contours grabs the appropriate value of the contours.
# loop over the contours for c in cnts: # applying contours on image (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(original, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.rectangle(tampered, (x, y), (x + w, y + h), (0, 0, 255), 2)
Bounding rectangle helps in finding the ratio of width to height of the bounding rectangle of the object. We compute the bounding box of the contour and then draw the bounding box on both input images to represent where the two images are different or not.
#Display original image with contour print('Original Format Image') original_contour = Image.fromarray(original) original_contour.save("pan_card_tampering/image/original_contour_image.png") original_contour
Output :
Original format Image
Inference :
#Diplay tampered image with contour print('Tampered Image') tampered_contour = Image.fromarray(tampered) tampered_contour.save("pan_card_tampering/image/tampered_contours_image.png") tampered_contour
Output :
Tampered Image
Inference: This similarly goes with the tampered image but one can notice that some of the contours are missing in the tampered image.
# Display difference image with black print('Different Image') difference_image = Image.fromarray(diff) difference_image.save("pan_card_tampering/image/difference_image.png") difference_image
Output :
Different Image
Inference :
#Display threshold image with white print('Threshold Image') threshold_image = Image.fromarray(thresh) threshold_image.save("pan_card_tampering/image/threshold_image.png") threshold_image
Output :
Threshold Image
Inference: Everything here is just the same all we can see is the change in the role of color, here white color is showing the heated zone and the black color is showing the normal zone.
This project can be used in different organizations where customers or users need to provide any kind of id in order to get themselves verified. The organization can use this project to find out whether the ID is original or fake. Similarly, this can be used for any type of ID like Aadhar, voter id, etc.
This concludes my discussion for today 🙂
Excellent Blog... Really helpful.. 👍👍
What about the accuracy percentage?