Haar-Like Features in Face Detection With Python

Real Python · Beginner ·💻 AI-Assisted Coding ·6y ago

Key Takeaways

Haar-like features are used for face detection in images, leveraging the contrast between different regions of the face, such as the eye region and the bridge of the nose, to determine the presence of facial features. The video demonstrates how to apply Haar-like features to detect edges and lines in images using Python.

Full Transcript

all human faces share some common similarities if you look at a photograph showing a person's face you'll see for example that the eye region is darker than the bridge of the nose the cheeks are also brighter than the eye region we can use these common properties to help us determine if an image contains facial features and ultimately a face a simple way to determine how bright or dark a portion of an images is to first convert it to grayscale and then add up the values of all the pixels within that portion remember lower values represent a darker pixel while higher ones represent a brighter pixel so if a specific sub-regions pixels add up to a low number it's a dark sub-region if it's a high number it's bright we can determine some features of the images such as lines and edges by comparing these pixel readings to readings in an adjacent area to do this we use what are called har like features these are ideal clusters of pixels that could represent a specific feature in the image such as an edge for example take a look at these har like features the first two are used to detect edges within a picture the third one here detects vertical lines and the fourth one detects horizontal features if our images were pure black and white then these horror light features would be able to identify where lines and edges are perfectly but like I said these features are ideal our pictures will never be all black and white that would be too easy instead they're usually varying shades of gray look at this example using Harlech features for finding edges the feature on the left represents an edge that's because there is a clear contrast between the dark and bright portions of the feature the feature on the right however is more realistic here the contrast between the two sides of our potential edge is still pretty distinct but not as distinct as on the left this contrast is called the features value and it's what lets us determine if what the feature represents like an edge a line or a facial feature exists at this location in the image to calculate this value we take the average of the white pixels and subtract from them the average of the black pixels if we get a result that is close to 255 then we've got a strong contrast and this feature is likely to represent the feature we are looking for it's telling us that whatever it represents like an edge or a line it most likely exists within this region if we get a result closer to zero then the clusters of pixels we are comparing are very close to one another and they're likely isn't any facial feature of interest here something like a wall would be likely to have little contrast as it's all one shade of brightness this example shows how hard light features can be applied to both the eye region and the bridge of the nose it picks up the darkness around the eyes and the bright bridge of the nose because the face has so many distinct areas in terms of brightness horror light features work great here the only problem is this pixel something has to be calculated for many different sub regions of the image at once and that becomes computationally expensive aka slow if only there was a way to speed this up

Original Description

All human faces share some similarities. If you look at a photograph showing a person’s face, you will see, for example, that the eye region is darker than the bridge of the nose. The cheeks are also brighter than the eye region. We can use these properties to help us understand if an image contains a human face. A simple way to find out which region is lighter or darker is to sum up the pixel values of both regions and comparing them. The sum of pixel values in the darker region will be smaller than the sum of pixels in the lighter region. This can be accomplished using Haar-like features. A Haar-like feature is represented by taking a rectangular part of an image and dividing that rectangle into multiple parts. They are often visualized as black and white adjacent rectangles. Click here to learn more: https://realpython.com/courses/traditional-face-detection-python/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Real Python · Real Python · 0 of 60

← Previous Next →
1 A better Python REPL – bpython vs python interpreter
A better Python REPL – bpython vs python interpreter
Real Python
2 Introducing large-type.com – A Utility Website
Introducing large-type.com – A Utility Website
Real Python
3 Reading Hacker News Without Wasting Tons of Time
Reading Hacker News Without Wasting Tons of Time
Real Python
4 Forward References and Python 3 Type Hints
Forward References and Python 3 Type Hints
Real Python
5 Using Sublime Text as your Git Editor
Using Sublime Text as your Git Editor
Real Python
6 Python Code Linting and Auto-Complete for Sublime Text
Python Code Linting and Auto-Complete for Sublime Text
Real Python
7 Make your Python Code More Readable with Custom Exceptions
Make your Python Code More Readable with Custom Exceptions
Real Python
8 Write Better Tests with Sublime Text's Split Layout Feature
Write Better Tests with Sublime Text's Split Layout Feature
Real Python
9 How to Use Sublime Text from the Command Line
How to Use Sublime Text from the Command Line
Real Python
10 Rename Variables with Multiple Selection in Sublime Text
Rename Variables with Multiple Selection in Sublime Text
Real Python
11 Sublime Text Settings for Writing PEP 8 Python
Sublime Text Settings for Writing PEP 8 Python
Real Python
12 Write Cleaner Python with Sublime Text's Indent Guides
Write Cleaner Python with Sublime Text's Indent Guides
Real Python
13 Sublime Text Whitespace Settings for Python Development
Sublime Text Whitespace Settings for Python Development
Real Python
14 Function Argument Unpacking in Python
Function Argument Unpacking in Python
Real Python
15 Python Code Review: Debugging and Refactoring "Conway's Game of Life" +  Automated Tests
Python Code Review: Debugging and Refactoring "Conway's Game of Life" + Automated Tests
Real Python
16 Using "get()" to Return a Default Value from a Python Dict
Using "get()" to Return a Default Value from a Python Dict
Real Python
17 A Python Shorthand for Swapping Two Variables
A Python Shorthand for Swapping Two Variables
Real Python
18 Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Python Code Review: Refactoring a Web Scraper, PEP 8 Style Guide Compliance, requirements.txt
Real Python
19 Click & Jump to Test Failures from the Command Line (iTerm2)
Click & Jump to Test Failures from the Command Line (iTerm2)
Real Python
20 Setting up Sublime Text for Python Developers
Setting up Sublime Text for Python Developers
Real Python
21 Sublime Text + Python Guide Overview
Sublime Text + Python Guide Overview
Real Python
22 Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Python Code Review: Adding Pytest Tests to an Existing Python Web Scraper
Real Python
23 Type-Checking Python Programs With Type Hints and mypy
Type-Checking Python Programs With Type Hints and mypy
Real Python
24 A Shorthand for Merging Dictionaries in Python 3.5+
A Shorthand for Merging Dictionaries in Python 3.5+
Real Python
25 Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Python Code Review Flask Web Security Tutorial + Virtualenvs, requirements.txt
Real Python
26 My Python Code Looks Ugly and Confusing – Help!
My Python Code Looks Ugly and Confusing – Help!
Real Python
27 Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Setting Up a Programmer Portfolio/Developer Blog – How To Get Started
Real Python
28 Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Do I Need a GitHub/GitLab/Bitbucket Profile as a Developer?
Real Python
29 Programmer Portfolio – Example and Walkthrough
Programmer Portfolio – Example and Walkthrough
Real Python
30 How to Get Your 1st Speaking Gig at a Tech Conference
How to Get Your 1st Speaking Gig at a Tech Conference
Real Python
31 How to Build Your Public Speaking Skills as a Developer
How to Build Your Public Speaking Skills as a Developer
Real Python
32 The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
The Object-oriented Version of "Spaghetti Code" is "Lasagna Code" ?!
Real Python
33 Setting up Sublime Text for Python Developers – Lesson #1
Setting up Sublime Text for Python Developers – Lesson #1
Real Python
34 Cool New Features in Python 3.6
Cool New Features in Python 3.6
Real Python
35 "is" vs "==" in Python – What's the Difference? (And When to Use Each)
"is" vs "==" in Python – What's the Difference? (And When to Use Each)
Real Python
36 Emulating switch/case Statements in Python with Dictionaries
Emulating switch/case Statements in Python with Dictionaries
Real Python
37 Python Function Argument Unpacking Tutorial (* and ** Operators)
Python Function Argument Unpacking Tutorial (* and ** Operators)
Real Python
38 What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
What Code Should I Put On My GitHub/GitLab/BitBucket Profile?
Real Python
39 A Crazy Python Dictionary Expression ?!
A Crazy Python Dictionary Expression ?!
Real Python
40 String Conversion in Python: When to Use __repr__ vs __str__
String Conversion in Python: When to Use __repr__ vs __str__
Real Python
41 Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Method Types in Python OOP: @classmethod, @staticmethod, and Instance Methods
Real Python
42 Optional Arguments in Python With *args and **kwargs
Optional Arguments in Python With *args and **kwargs
Real Python
43 Python Context Managers and the "with" Statement (__enter__ & __exit__)
Python Context Managers and the "with" Statement (__enter__ & __exit__)
Real Python
44 Installing Python Packages with pip and virtualenv / venv
Installing Python Packages with pip and virtualenv / venv
Real Python
45 "For Each" Loops in Python with enumerate() and range()
"For Each" Loops in Python with enumerate() and range()
Real Python
46 Python Code Review: LibreOffice Automation and the Python Standard Library
Python Code Review: LibreOffice Automation and the Python Standard Library
Real Python
47 Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Managing Python Dependencies With Pip and Virtual Environments – Lesson #1
Real Python
48 Python Tutorial: List Comprehensions Step-By-Step
Python Tutorial: List Comprehensions Step-By-Step
Real Python
49 Leveraging Python's Implicit "return None" Statements
Leveraging Python's Implicit "return None" Statements
Real Python
50 What's the meaning of underscores (_ & __) in Python variable names?
What's the meaning of underscores (_ & __) in Python variable names?
Real Python
51 Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Python Data Structures: Sets, Frozensets, and Multisets (Bags)
Real Python
52 Writing automated tests for Python command-line apps and scripts
Writing automated tests for Python command-line apps and scripts
Real Python
53 How to find great Python packages on PyPI, the Python Package Repository
How to find great Python packages on PyPI, the Python Package Repository
Real Python
54 Immutable vs Mutable Objects in Python
Immutable vs Mutable Objects in Python
Real Python
55 PyPI vs Warehouse, the Next-Generation Python Package Repository
PyPI vs Warehouse, the Next-Generation Python Package Repository
Real Python
56 pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
pep8.org — The Prettiest Way to View the PEP 8 Python Style Guide
Real Python
57 My Experience at PyCon 2017 in Portland
My Experience at PyCon 2017 in Portland
Real Python
58 Pylint Tutorial – How to Write Clean Python
Pylint Tutorial – How to Write Clean Python
Real Python
59 "Reverse a List in Python" Tutorial: Three Methods & How-to Demos
"Reverse a List in Python" Tutorial: Three Methods & How-to Demos
Real Python
60 Python Refactoring: "while True" Infinite Loops & The "input" Function
Python Refactoring: "while True" Infinite Loops & The "input" Function
Real Python

This video teaches how to use Haar-like features for face detection in images using Python, including converting images to grayscale, calculating pixel values, and detecting edges and lines. By applying these techniques, viewers can determine the presence of facial features in an image.

Key Takeaways
  1. Convert an image to grayscale
  2. Calculate the average pixel value for different regions of the image
  3. Apply Haar-like features to detect edges and lines
  4. Calculate the contrast between different regions of the image
  5. Use the contrast value to determine the presence of facial features
💡 Haar-like features can be used to detect facial features in images by leveraging the contrast between different regions of the face

Related Reads

📰
5 Playwright Mistakes That Are Silently Breaking Your Scrapers
Learn to avoid common Playwright mistakes that can break your web scrapers and improve your web automation skills
Medium · Python
📰
The AI That Out-Forecasted Europe’s Best Supercomputers-Using a Single Chip
Learn how a single chip AI outperformed Europe's best supercomputers in weather forecasting, and why this matters for the future of AI and computing
Medium · AI
📰
Why Local AI is the Future of Developer Privacy: A WebGPU Deep Dive | 0709-0400
Learn how Local AI with WebGPU enhances developer privacy and performance, and why it's the future of AI development
Dev.to · TACiT
📰
Day 4 of 100 Days of GenAI for DevOps🚀
Learn what ChatGPT stands for and its significance in conversational AI, enabling you to better understand AI-powered tools in DevOps
Dev.to AI
Up next
Connect Claude AI to Canva & Create Stunning Designs in Minutes! Claude Ai Use case
Quick Tips - Web Desiign & Ai Tools
Watch →