Detecting pupils
We are going to take a different approach here. Pupils are too generic to take the Haar cascade approach. We will also get a sense of how to detect things based on their shape. Following is what the output will look like:

Let's see how to build the pupil detector:
import math import cv2 import numpy as np img = cv2.imread('input.jpg') scaling_factor = 0.7 img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) cv2.imshow('Input', img) gray = cv2.cvtColor(~img, cv2.COLOR_BGR2GRAY) ret, thresh_gray = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) for contour in contours: area = cv2.contourArea(contour) rect = cv2.boundingRect(contour) x, y, width, height = rect radius = 0.25 * (width + height) area_condition = (100 <= area <= 200) symmetry_condition = (abs(1 - float(width)/float(height)) <= 0.2) fill_condition...