O'Reilly logo

OpenCV with Python By Example by Prateek Joshi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required