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:

Detecting pupils

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, ...

Get OpenCV with Python By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.