Hello there! Welcome to another blog. In this blog you are going to learn to detect iris using OpenCv python.
Here is the video in case you missed it.
So, let's get started. We will start by importing the necessary libraries.
import cv2
import numpy as np
Now, let us import the face and eye classifier files and set the camera resolution as follows.
eye = cv2.CascadeClassifier('haarcascade_eye.xml')
face = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
Kernal = np.ones((3, 3), np.uint8) #Declare kernal for morphology
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320) ##Set camera resolution
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
In a while loop let us capture an image frame, flip it(in case your camera captures inverted images) and convert it into a gray scale image.
ret, frame = cap.read() ##Read image frame
frame = cv2.flip(frame, +1) ##Flip the image in case your camera capures inverted images
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) ##Convert image to grayscale
Now let's find the face in the complete image using the classifier we imported above.
detect_face = face.detectMultiScale(gray, 1.2, 1) ##Detect Face
detectMultiScale() requires three arguments. The first is the source image, the second is the scale factor which specifies how much the image size is reduced at each image scale and the third is the minimum neighbors which specify how many neighbors each candidate rectangle should have to retain it. This method returns the coordinates of the rectangle detecting a face. If you draw a bounding box using the coordinates, the output will be something as shown below.
We will now use this face region and use it as the region of interest to detect eyes. This can be done using the following lines of code.
for(face_x, face_y, face_z, face_h) in detect_face:
img2 = gray[face_y:face_y+face_h, face_x:face_x+face_z]
detect_eye = eye.detectMultiScale(img2, 1.2, 1)
img2 contains only the face image.detect_eye has the coordinates of all the eyes found in the face region(ideally 2 eyes). If you draw a binding box using the coordinates, the output image will be as below.
Now using the eye as the region of interest we have to detect iris. To do that let us use binary thresholding to convert the grayscale eye image into a binary image.
for (eye_x, eye_y, eye_z, eye_h) in detect_eye:
eye1 = gray[face_y+eye_y:face_y+eye_y+eye_h, face_x+eye_x:face_x+eye_x+eye_z]
ret, binary = cv2.threshold(eye1, 60, 255, cv2.THRESH_BINARY_INV)
eye1 contains the image of only the eye extracted from the face image. binary contains the binary image of the eye1. 60 is the threshold value. This might vary as per the light conditions. 60 worked best for me. The output of the above code is shown below.
As you can see in the above image, We have the iris as well as the eyebrow. We are only interested in the iris. So let us remove the eyebrow by cropping out the top 40% of the image.
width, height = binary.shape
binary = binary[int(0.4 * height):height, :] ##Crop top 40%of the image
The new output is shown below.
To remove the noises in this image let us use image morphology.
opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, Kernal) ##Opening Morphology
dilate = cv2.morphologyEx(opening, cv2.MORPH_DILATE, Kernal) ##Dilate Morphology
The output of the above code is as follows.
Now we have the proper image of the iris. Let us now detect contours in the above image and then draw a circle around it.
contours, hierarchy = cv2.findContours(dilate, cv2.RETR_TREE, ##Find contours
cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
cnt = contours[0]
M1 = cv2.moments(cnt)
Cx1 = int(M1['m10'] / M1['m00']) ##Find center of the contour
Cy1 = int(M1['m01'] / M1['m00'])
croppedImagePixelLength = int(0.4*height) ##Number of pixels we cropped from the image
center1 = (int(Cx1+face_x+eye_x), int(Cy1+face_y + eye_y + croppedImagePixelLength)) ##Center coordinates
cv2.circle(frame, center1, 2, (0, 255, 0), 2)
Cx1 and Cy1 are the coordinates of the center of the contour with reference to the dilated image of the eye. center1 contains the coordinates of the center of the contour with reference to the original image(frame).
cv2.circle draws the circle around the iris in the original image.
Here is the final output.
That is it. Happy coding...!!!!
For full code click here.
For the classifier, files click here.
Hmm, I'm getting these errors and don't know why. Have any ideas? I've manually imported the library as well as tried updating through pycharm but no dice.
ReplyDeleteERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)
ERROR: No matching distribution found for cv2