Detecting ears

Since we know how the pipeline works, let's just jump into the code:

import cv2 import numpy as np left_ear_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_leftear.xml') right_ear_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_mcs_rightear.xml') if left_ear_cascade.empty(): raise IOError('Unable to load the left ear cascade classifier xml file') if right_ear_cascade.empty(): raise IOError('Unable to load the right ear cascade classifier xml file') img = cv2.imread('input.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) left_ear = left_ear_cascade.detectMultiScale(gray, 1.3, 5) right_ear = right_ear_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in left_ear: cv2.rectangle(img, (x,y), (x+w,y+h), ...

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.