Skip to main content

Iris Detection | Python | OpenCv

 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.







Comments

  1. 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.
    ERROR: Could not find a version that satisfies the requirement cv2 (from versions: none)
    ERROR: No matching distribution found for cv2

    ReplyDelete

Post a Comment

Popular posts from this blog

Object Distance Calculation Using Contour Area Method In Python - Opencv

Today we will discuss how you can find the distance of an object from the camera using python OpenCV. Check out the video below. Before we continue, you should know how to detect a colored object. Click this link to check out my previous blog on object detection and tracking. I hope after checking out my previous blog, you are able to write your own code to detect and track objects. We will take forward the Object detection and tracking code to find the distance of an object from the camera. So let's start. Let us first understand the principle using which we will find the distance of the object from the camera. Principle:- Area enclosed by the contours of an object decreases as the object moves farther from the camera. This simply means that, if your object is near to the camera, the object will appear bigger. Thus the pixel area occupied by the object will be very large. As you move the object farther from the camera, the object size in the image will start to d...

Object Detection And Tracking using Python - Opencv

Let us discuss today how you can detect and track an object in real-time. We will be using Python language and Opencv library for this purpose. Check out the video below. If you have read my previous blogs, you can directly skip down to the contour part. As usual, we need to make a few assumptions for the proper working of this application. This background is always static i.e. there is no addition or subtraction of objects in the background scene. The background-color is always constant. It does not change with time. The object that will be used for writing/painting is of a different color than the background to give us sufficient contrast between foreground and background. We are ready to begin now. Let us start by installing necessary python libraries for our project using  pip install.  We will be needing  Numpy  and  Opencv  libraries. Now create a python project and create a new script. Import the required libraries into python script as...