Change language

Python | Haar cascades for object detection

| | |

What are Haar Cascades?
Haar Cascade Classifiers are an effective way of detecting objects. This method was proposed by Paul Viola and Michael Jones in their article " cascade" a href = https://www.researchgate.net/publication/3940582_Rapid_Object_Detection_using_a_Boosted_Cascade_of_Simple_Features rel = noopener target = _blank> simple functions. Haar cascade — it is a machine learning approach that uses many positive and negative images to train the classifier.

  • Positive images — these images contain images that we want our classifier to identify.
  • Negative images — images of everything else that do not contain the object we want to detect.

Requirements

  • Make sure you have python, Matplotlib and OpenCV (all latest versions) installed on your machine.
  • The haar cascade files can be downloaded from OpenCV Github repository .

Implementation

# Import all required packages

import cv2

import numpy as np

import matplotlib.pyplot as plt % matplotlib inline

 

 
# Read in cascading face and eye classifiers

face_cascade = cv2.CascadeClassifier ( ’. ./DATA / haarcascades / haarcascade_frontalface_default.xml’ )

eye_cascade = cv2.CascadeClassifier ( ’../ DATA / haarcascades / haarcascade_eye.xml’ )

  

  

  
# create a function for face detection

def adjusted_detect_face ( img):

 

face_img = img.copy ()

 

face_rect = face_cascade.detectMultiScale (face_img, 

scaleFactor = 1.2

minNeighbors = < / code> 5 )

 

for (x, y, w, h) in face_rect:

cv2.rectangle-method/">rectangle (face_img, (x, y), 

(x + w, y + h), ( 255 , 255 , 255 ), 10 )

  

return face_img 

 

 
# create a function to detect eyes

def detect_eyes (img):

 

eye_img = img.copy () 

eye_rect = eye_cascade.detectMultiScale (eye_img, 

scaleFactor = 1.2

minNeighbors = 5

  for (x, y, w, h) in eye_rect:

  cv2.rectangle-method/">rectangle (eye_img, (x, y), 

(x + w, y + h ), ( 255 , 255 , 255 ), 10

return eye_img

  
# Read on image and make copies

img = cv2.imread ( ’../ sachin.jpg’ )

img_copy1 = img.copy ()

img_copy2 = img.copy ()

img_copy3 = img.copy ()

 
# Face detection

face = adjusted_detect_face (img_copy)

plt.imshow (face)

Code: Eye Detection < / p>

eyes = detect_eyes (img_copy2)

plt.imshow (eyes)

Code: face and eye detection

eyes_face = adjusted_detect_face (img_copy3)

eyes_face = detect_eyes (eyes_face)

plt.imshow (eyes_face)

Haar cascades can be used to detect any type of object if you have an appropriate XML file for that. You can even create your own XML files from scratch to discover any type of object you want.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically