esafwan
esafwan

Reputation: 18029

Can we use Haar Classifier to detect Iris of an eye as done for face and eyes?

I am currently trying to detect iris and subsequently pupil within a given image. All approaches seen online, detect the face, then the eyes and then use thresholding, hough transformation, and contouring to finally detect the pupil.

If we already have a large data set of pupils, can't we train a Haar Classifier as done in the case of Face and Eyes to detect iris of an eye? Is there any reason this shouldn't work?

Currently, I have a partially working solution using the non-haar based approach.

Example of one working code that

import math 
import cv2 

eye_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_eye.xml')

if eye_cascade.empty():
  raise IOError('Unable to load the eye cascade classifier xml file')

cap = cv2.VideoCapture(0)
ds_factor = 0.5
ret, frame = cap.read()
contours = []

while True: 
  ret, frame = cap.read() 
  frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA)
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  eyes = eye_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=1)
  for (x_eye, y_eye, w_eye, h_eye) in eyes:
    pupil_frame = gray[y_eye:y_eye + h_eye, x_eye:x_eye + w_eye]
    ret, thresh = cv2.threshold(pupil_frame, 80, 255, cv2.THRESH_BINARY)
    cv2.imshow("threshold", thresh)
    im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    print(contours)

    for contour in contours:
      area = cv2.contourArea(contour)
      rect = cv2.boundingRect(contour)
      x, y, w, h = rect
      radius = 0.15 * (w + h)

      area_condition = (100 <= area <= 200)
      symmetry_condition = (abs(1 - float(w)/float(h)) <= 0.2)
      fill_condition = (abs(1 - (area / (math.pi * math.pow(radius, 2.0)))) <= 0.4)
      cv2.circle(frame, (int(x_eye + x + radius), int(y_eye + y + radius)), int(1.3 * radius), (0, 180, 0), -1)

  cv2.imshow('Pupil Detector', frame)
  c = cv2.waitKey(1) 
  if c == 27: 
    break

cap.release() 
cv2.destroyAllWindows()

Example output: enter image description here

Upvotes: 2

Views: 1336

Answers (1)

shortcipher3
shortcipher3

Reputation: 1380

Yes, you could train a pupil detector directly with sample images on a Haar Cascade classifier. If you aren't constrained to small hardware at high throughput, then I would recommend using deep learning to train on the same dataset, you could expect better results.

Upvotes: 1

Related Questions