Fingerprint Detection in Python
Detection of fingerprints has become one of the most popular ways of maintaining security systems in today’s world. So, accurate identification of a person through his fingerprints is quite important. In this tutorial, we shall learn how to carry out fingerprint detection in Python. We shall use a database of a few fingerprints and try to match a particular fingerprint with that so that we can identify the person.
You can download the database from here and the test file from here. So as you might have noticed, all the files are in TIF format and we would like to keep it that way as they are flexible and adaptable. Now, we shall dive into the code and while we do so, we shall also explain the involved concepts along the way.
Importing the necessary libraries and modules
import cv2 import numpy as np import os
Viewing the test file (fingerprint to be matched)
test_original = cv2.imread("TEST_1.tif") cv2.imshow("Original", cv2.resize(test_original, None, fx=1, fy=1)) cv2.waitKey(0) cv2.destroyAllWindows()
Matching with the database
We are going to use SIFT (Scale-Invariant Feature Transform) algorithm here. To learn more about this topic, you can visit this site. We use it to extract key-points and detect descriptors for the best-retained features. So, here we shall use it to identify the key-points and descriptors of both the test image and database images.
for file in [file for file in os.listdir("database")]: fingerprint_database_image = cv2.imread("./database/"+file) sift = cv2.xfeatures2d.SIFT_create() keypoints_1, descriptors_1 = sift.detectAndCompute(test_original, None) keypoints_2, descriptors_2 = sift.detectAndCompute(fingerprint_database_image, None)
After the detection and the computation are over, we start the matching algorithm. We shall use the FlannBasedMatcher() functionality here about which you can learn from here. So, this interface helps us in performing a quick but efficient matching using the Cluster and Search algorithm. We shall maintain an array of such points that are given as output in the match.
matches = cv2.FlannBasedMatcher(dict(algorithm=1, trees=10), dict()).knnMatch(descriptors_1, descriptors_2, k=2) match_points =  for p, q in matches: if p.distance < 0.1*q.distance: match_points.append(p)
Detecting the ID for the fingerprint matched
In the end, we need to define the keypoint detection margin and then visualize the output after drawing the matching keypoints. So here we shall use the cv2.drawMatches() function for this purpose.
keypoints = 0 if len(keypoints_1) <= len(keypoints_2): keypoints = len(keypoints_1) else: keypoints = len(keypoints_2) if (len(match_points) / keypoints)>0.95: print("% match: ", len(match_points) / keypoints * 100) print("Figerprint ID: " + str(file)) result = cv2.drawMatches(test_original, keypoints_1, fingerprint_database_image, keypoints_2, match_points, None) result = cv2.resize(result, None, fx=2.5, fy=2.5) cv2.imshow("result", result) cv2.waitKey(0) cv2.destroyAllWindows() break;
% match: 100.0 Figerprint ID: 1_1.tif
Hence, this concludes our tutorial on fingerprint detection in Python. But, your task is not over yet. You can try changing the keypoint detection and matching algorithms and then notice the variations.
For further learning on object detection in Python, you can check out the following: