Face & Eyes Detection with OpenCV Raspberry Pi Camera
Face & Eyes Detection System with Raspberry Pi 4 Camera using OpenCV & Python Code; Haar Cascades Computer Vison object detection method
- Suitable forB.Tech/Diploma Raspberry Pi students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
Overview
This project covers the
Face
&
Eyes Detection
System with
OpenCV
installation on
Raspberry Pi 4
Digital Image Processing
and
Computer Vision
are intertwined areas in the world of tech. At its core, Image Processing is all about
refining
and
adjusting
images. The result is usually another
enhanced image
. In contrast, Computer Vision goes a step further—it doesn’t just process an image; it interprets it.
Computer Vision algorithms
extract crucial details or features from images, offering a more comprehensive analysis of the visual input.
In the vast world of image-related tools,
OpenCV
has cemented its position as a frontrunner. It is not just versatile, but its extensive documentation and the support of a thriving community make it an invaluable resource. In this guide, we highlight a hands-on
application of OpenCV
. We will walk you through the steps of
detecting faces
and
eyes
from images taken with the
Raspberry Pi camera
. With the help of
Haar Cascades
, an object detection method based on
machine learning
, we’ll be pinpointing these features with impressive accuracy.
By the end of this exploration, you will have a deeper understanding of the synergy between
Image Processing
and
Computer Vision
, and the myriad of possibilities they open up in today’s tech landscape.
Components Required
We need the following components for this project. You can purchase all items from given links:
S.N.
Components
Quantity
Purchase Link
Raspberry Pi 4
Amazon
SunFounder
Raspberry Pi Camera
Amazon
SunFounder
SD Card 16/32 GB
Amazon
SunFounder
5V, 3A DC Adapter for RPi
Amazon
SunFounder
LCD Display (Optional)
Amazon
SunFounder
Mouse & Keyboard (Optional)
Amazon
SunFounder
Raspberry Pi Camera Connection
The
Raspberry Pi Camera
is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add
video/photo capabilities
to Raspberry Pi projects.
For this project, we can use a
5 mega-pixel
Raspberry Pi Camera.
Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.
To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing
sudo raspi-config
in the terminal. Navigate to
Interfacing Options
>
Camera
and enable it.
Concept & Algorithm Behind Face & Eyes Detection
The primary aim of the project is to
identify
and
highlight
faces
and
eyes
within a continuous video stream.
For this, we have used the
OpenCV library
. OpenCv is a widely used computer vision library for various image processing tasks, including
object detection
. For face and eye detection we are using
Haar Cascades Model
. The Haar Cascades is a
machine learning-based approach
where a cascade function is trained to detect objects in images.
It is a particularly efficient method for
object detection
and has been widely used for tasks like face detection in real-time scenarios. OpenCV provides
pre-trained Haar Cascades
for
face detection
, making it easily accessible for many developers.
The
Algorithm
for Face & Eyes Detection using Raspberry Pi and OpenCV can be explained as follows.
Initialization
:
Load the Haar cascades meant for face and eye detection.
Configure the Raspberry Pi camera for the desired video resolution and frame rate.
Capture Frames Continuously
: The video stream is captured frame by frame for real-time processing.
Pre-processing
:
Each captured frame is converted to grayscale. The grayscale representation simplifies the image, eliminating color nuances, which often makes object detection faster and more accurate.
Face Detection
:
Faces within the grayscale frame are identified.
Highlight each detected face by drawing a rectangle around it.
Eye Detection
:
For every identified face, define a region of interest (typically, this is the area of the face) where eyes are most likely to be located.
Within this region, detect the eyes.
Highlight each detected eye by drawing a rectangle around it.
Display
:
The processed frame, now with rectangles around detected faces and eyes, is displayed in real time to the user.
User Interaction
:
Provide the user with an option to exit the detection and end the program.
Raspberry Pi Setup, Libraries & Dependencies Installation
OpenCV
is required for
face
and
eye detection
, and other image processing tasks present in the code. Hence you need to install the OpenCV first. Follow the following guide to
install OpenCV
in your system:
How to Install & Setup OpenCV on Raspberry Pi 4
The next step is to install
picamera
. Therefore install it using
pip
pip3
install
picamera
Create a folder in the
Home directory
of your Raspberry Pi with any name such as “
Face Recognition
“.
The code references two
XML files
(
haarcascade_frontalface_default.xml
and
haarcascade_eye.xml
) which are Haar cascades used for face and eye detection respectively.
Download these files from the following links:
Download
:
haarcascade_frontalface_default.xml
Download
:
haarcascade_eye.xml
Add these files to the folder you created above.
The setup part is complete now. We can move to the Face & Eyes Detection Project using Raspberry Pi & OpenCV.
Raspberry Pi Python Code for Face & Eyes Detection
Now let’s develop a
Python Code
that helps in the detection of eye and face using the
OpenCV library
and
Raspberry Pi Camera
Python Code
Open
Thonny IDE
and paste the following code to the Thonny Editor. Save this file with the name “
face_detection.py
” to the folder that we created earlier. Ensure that these
XML files
are present in the directory mentioned in the code (
/home/mypi/Face Recognition/
).
import
cv2
import
numpy
as
np
import
picamera
import
picamera
array
def
main
(
)
:
# Load the face and eye detection cascades
face_cascade
=
cv2
CascadeClassifier
(
'/home/mypi/Face Recognition/haarcascade_frontalface_default.xml'
)
eye_cascade
=
cv2
CascadeClassifier
(
'/home/mypi/Face Recognition/haarcascade_eye.xml'
)
# You'll need the XML file for eye detection
with
picamera
PiCamera
(
)
as
camera
:
camera
resolution
=
(
)
camera
framerate
=
with
picamera
array
PiRGBArray
(
camera
size
=
(
)
)
as
stream
:
for
frame
in
camera
capture_continuous
(
stream
format
=
'bgr'
use_video_port
=
True
)
:
image
=
frame
array
# Convert the image to grayscale for face and eye detection
gray
=
cv2
cvtColor
(
image
cv2
COLOR_BGR2GRAY
)
# Detect faces
faces
=
face_cascade
detectMultiScale
(
gray
1.1
)
for
(
y
w
h
)
in
faces
:
cv2
rectangle
(
image
(
y
)
(
+
w
y
+
h
)
(
)
)
# Region of interest in the grayscale image for eyes detection
roi_gray
=
gray
[
y
:
y
+
h
:
+
w
]
# Region of interest in the color image for drawing rectangles around eyes
roi_color
=
image
[
y
:
y
+
h
:
+
w
]
# Detect eyes within the region of interest (i.e., the detected face)
eyes
=
eye_cascade
detectMultiScale
(
roi_gray
1.03
minSize
=
(
)
)
for
(
ex
ey
ew
eh
)
in
eyes
:
cv2
rectangle
(
roi_color
(
ex
ey
)
(
ex
+
ew
ey
+
eh
)
(
)
)
# Resize the frame for displaying in a larger window
display_frame
=
cv2
resize
(
image
(
)
)
# Display the frame
cv2
imshow
(
'Face and Eye Detection'
display_frame
)
# Clear the stream for the next frame
stream
truncate
(
)
# Close the window when 'q' is pressed
if
cv2
waitKey
(
)
&
0xFF
==
ord
(
'q'
)
:
break
cv2
destroyAllWindows
(
)
if
__name__
==
'__main__'
:
main
(
)
Code Explanation
Let’s break down the code and explain each section:
import
cv2
import
numpy
as
np
import
picamera
import
picamera
array
cv2
: This is the OpenCV library, a powe
This project covers the
Face
&
Eyes Detection
System with
OpenCV
installation on
Raspberry Pi 4
Digital Image Processing
and
Computer Vision
are intertwined areas in the world of tech. At its core, Image Processing is all about
refining
and
adjusting
images. The result is usually another
enhanced image
. In contrast, Computer Vision goes a step further—it doesn’t just process an image; it interprets it.
Computer Vision algorithms
extract crucial details or features from images, offering a more comprehensive analysis of the visual input.
In the vast world of image-related tools,
OpenCV
has cemented its position as a frontrunner. It is not just versatile, but its extensive documentation and the support of a thriving community make it an invaluable resource. In this guide, we highlight a hands-on
application of OpenCV
. We will walk you through the steps of
detecting faces
and
eyes
from images taken with the
Raspberry Pi camera
. With the help of
Haar Cascades
, an object detection method based on
machine learning
, we’ll be pinpointing these features with impressive accuracy.
By the end of this exploration, you will have a deeper understanding of the synergy between
Image Processing
and
Computer Vision
, and the myriad of possibilities they open up in today’s tech landscape.
Components Required
We need the following components for this project. You can purchase all items from given links:
S.N.
Components
Quantity
Purchase Link
Raspberry Pi 4
Amazon
SunFounder
Raspberry Pi Camera
Amazon
SunFounder
SD Card 16/32 GB
Amazon
SunFounder
5V, 3A DC Adapter for RPi
Amazon
SunFounder
LCD Display (Optional)
Amazon
SunFounder
Mouse & Keyboard (Optional)
Amazon
SunFounder
Raspberry Pi Camera Connection
The
Raspberry Pi Camera
is a peripheral device developed by the Raspberry Pi Foundation to be used with their series of Raspberry Pi single-board computers. The camera module provides a way to add
video/photo capabilities
to Raspberry Pi projects.
For this project, we can use a
5 mega-pixel
Raspberry Pi Camera.
Simply connect the Camera Module to the Raspberry Pi 4 Board using the Camera Connector.
To use the Camera you need to enable the Camera Module first. Open the Raspberry Pi Configuration Tool by typing
sudo raspi-config
in the terminal. Navigate to
Interfacing Options
>
Camera
and enable it.
Concept & Algorithm Behind Face & Eyes Detection
The primary aim of the project is to
identify
and
highlight
faces
and
eyes
within a continuous video stream.
For this, we have used the
OpenCV library
. OpenCv is a widely used computer vision library for various image processing tasks, including
object detection
. For face and eye detection we are using
Haar Cascades Model
. The Haar Cascades is a
machine learning-based approach
where a cascade function is trained to detect objects in images.
It is a particularly efficient method for
object detection
and has been widely used for tasks like face detection in real-time scenarios. OpenCV provides
pre-trained Haar Cascades
for
face detection
, making it easily accessible for many developers.
The
Algorithm
for Face & Eyes Detection using Raspberry Pi and OpenCV can be explained as follows.
Initialization
:
Load the Haar cascades meant for face and eye detection.
Configure the Raspberry Pi camera for the desired video resolution and frame rate.
Capture Frames Continuously
: The video stream is captured frame by frame for real-time processing.
Pre-processing
:
Each captured frame is converted to grayscale. The grayscale representation simplifies the image, eliminating color nuances, which often makes object detection faster and more accurate.
Face Detection
:
Faces within the grayscale frame are identified.
Highlight each detected face by drawing a rectangle around it.
Eye Detection
:
For every identified face, define a region of interest (typically, this is the area of the face) where eyes are most likely to be located.
Within this region, detect the eyes.
Highlight each detected eye by drawing a rectangle around it.
Display
:
The processed frame, now with rectangles around detected faces and eyes, is displayed in real time to the user.
User Interaction
:
Provide the user with an option to exit the detection and end the program.
Raspberry Pi Setup, Libraries & Dependencies Installation
OpenCV
is required for
face
and
eye detection
, and other image processing tasks present in the code. Hence you need to install the OpenCV first. Follow the following guide to
install OpenCV
in your system:
How to Install & Setup OpenCV on Raspberry Pi 4
The next step is to install
picamera
. Therefore install it using
pip
pip3
install
picamera
Create a folder in the
Home directory
of your Raspberry Pi with any name such as “
Face Recognition
“.
The code references two
XML files
(
haarcascade_frontalface_default.xml
and
haarcascade_eye.xml
) which are Haar cascades used for face and eye detection respectively.
Download these files from the following links:
Download
:
haarcascade_frontalface_default.xml
Download
:
haarcascade_eye.xml
Add these files to the folder you created above.
The setup part is complete now. We can move to the Face & Eyes Detection Project using Raspberry Pi & OpenCV.
Raspberry Pi Python Code for Face & Eyes Detection
Now let’s develop a
Python Code
that helps in the detection of eye and face using the
OpenCV library
and
Raspberry Pi Camera
Python Code
Open
Thonny IDE
and paste the following code to the Thonny Editor. Save this file with the name “
face_detection.py
” to the folder that we created earlier. Ensure that these
XML files
are present in the directory mentioned in the code (
/home/mypi/Face Recognition/
).
import
cv2
import
numpy
as
np
import
picamera
import
picamera
array
def
main
(
)
:
# Load the face and eye detection cascades
face_cascade
=
cv2
CascadeClassifier
(
'/home/mypi/Face Recognition/haarcascade_frontalface_default.xml'
)
eye_cascade
=
cv2
CascadeClassifier
(
'/home/mypi/Face Recognition/haarcascade_eye.xml'
)
# You'll need the XML file for eye detection
with
picamera
PiCamera
(
)
as
camera
:
camera
resolution
=
(
)
camera
framerate
=
with
picamera
array
PiRGBArray
(
camera
size
=
(
)
)
as
stream
:
for
frame
in
camera
capture_continuous
(
stream
format
=
'bgr'
use_video_port
=
True
)
:
image
=
frame
array
# Convert the image to grayscale for face and eye detection
gray
=
cv2
cvtColor
(
image
cv2
COLOR_BGR2GRAY
)
# Detect faces
faces
=
face_cascade
detectMultiScale
(
gray
1.1
)
for
(
y
w
h
)
in
faces
:
cv2
rectangle
(
image
(
y
)
(
+
w
y
+
h
)
(
)
)
# Region of interest in the grayscale image for eyes detection
roi_gray
=
gray
[
y
:
y
+
h
:
+
w
]
# Region of interest in the color image for drawing rectangles around eyes
roi_color
=
image
[
y
:
y
+
h
:
+
w
]
# Detect eyes within the region of interest (i.e., the detected face)
eyes
=
eye_cascade
detectMultiScale
(
roi_gray
1.03
minSize
=
(
)
)
for
(
ex
ey
ew
eh
)
in
eyes
:
cv2
rectangle
(
roi_color
(
ex
ey
)
(
ex
+
ew
ey
+
eh
)
(
)
)
# Resize the frame for displaying in a larger window
display_frame
=
cv2
resize
(
image
(
)
)
# Display the frame
cv2
imshow
(
'Face and Eye Detection'
display_frame
)
# Clear the stream for the next frame
stream
truncate
(
)
# Close the window when 'q' is pressed
if
cv2
waitKey
(
)
&
0xFF
==
ord
(
'q'
)
:
break
cv2
destroyAllWindows
(
)
if
__name__
==
'__main__'
:
main
(
)
Code Explanation
Let’s break down the code and explain each section:
import
cv2
import
numpy
as
np
import
picamera
import
picamera
array
cv2
: This is the OpenCV library, a powe
Technologies used
Hardware components
- Raspberry Pi
- PIR
- SD Card
Software & tools
- Thonny
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.