0% found this document useful (0 votes)
296 views

Face Detection Using MATLAB and RPi

This document provides steps to perform face detection on images and video captured from a Raspberry Pi camera using MATLAB. It discusses two methods: 1) Capturing a continuous stream of images and processing them in a loop. 2) Using the video player to display the video stream and processing frames. Key steps include initializing the Pi camera, capturing images/video, detecting faces using a cascade detector, and displaying the output with annotated bounding boxes.

Uploaded by

KhoaDoan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
296 views

Face Detection Using MATLAB and RPi

This document provides steps to perform face detection on images and video captured from a Raspberry Pi camera using MATLAB. It discusses two methods: 1) Capturing a continuous stream of images and processing them in a loop. 2) Using the video player to display the video stream and processing frames. Key steps include initializing the Pi camera, capturing images/video, detecting faces using a cascade detector, and displaying the output with annotated bounding boxes.

Uploaded by

KhoaDoan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Face Detection using MATLAB

on Raspberry Pi 3

Hardware & Software:


Raspberry Pi 3
RPi camera board
Micro USB cable
Ethernet cable
Micro SD card
USB SD card reader / SD card adapter
MATLAB and Simulink Student Suite

1. Install MATLAB support package for Raspberry Pi


Install MATLAB successfully and activate it with a valid license.
Step 1

Make sure your development machine is connected to the


internet to download the required support packages for RPi.
At the command prompt type: SupportPackageInstaller

Step 2

& hit Enter. MATLAB will now run the support package installer &
populate a list of available support packages from MATLAB.
On the left hand pane, browse for Raspberry Pi & select it.
Once selected, available packages are shown on the right hand

Step 3

panel. Select the support package from MATLAB and install it.
This will proceed to download the Wheezy image for RPi along
with the required support files
Once downloaded, insert a freshly formatted microSD card with

Step 4

an SD card adapter or USB SD card reader into the host PC.


Proceed with Yes to format & erase any data already present
on the card.

NOTE: If the memory card is not detected, MATLAB will need to


be restarted with administrator privileges. (Right click on
MATLAB executable file from Windows explorer and Run as
administrator... If this asks for a password, an admin password
needs to be supplied.
Once the image is written to the SD card, remove it from the
host and insert it into the RPi and reboot.

At the command prompt in MATLAB, type: mypi = raspi();


Step 5

If the output of this command shows the IP address of the RPi


along with few other details such as I2C address, clock frequency
etc, then your RPi is now successfully connected to MATLAB.

2. Create new MATLAB script file


The logic to create this script is to continuously capture images & send

them over to MATLAB for processing OR use the video player to capture
video from the RPi camera & then process it in MATLAB.
Method 1: Continuous stream of images:
Create a new script file & type:
Step 1

mypi = raspi()

The MATLAB support package provides a raspi() function to


create a serial connection to the RPi & returns a variable to
access all the peripherals of the Pi. The camera board can also be
accessed in a similar way using the cameraboard() function from
the MATLAB package.
mycam = cameraboard(mypi, Resolution, 640x480, Brightness, 65);

Capture an image using the snapshot() function from the support


Step 2

package.
mysnap = snapshot(mycam);

The captured image can be displayed using the imshow()


function with mysnap as the argument.

Step 3

Use the Cascade Object Detector function from the Computer


Vision Toolbox to detect faces in the captured video along with
the step() function to process the captured image.
fD = vision.CascadeObjectDetector();
bbox = step(fD, mysnap);

Once the processing is done, generate an output image with a


Step 4

rectangle added as an annotation around the detected face.


imageOut = insertObjectAnnotation(mysnap, rectangle, bbox, Face);

Step 5

Display the processed image using imshow(imageOut);

Optionally insert a title to the Figure window using:


Step 6

title(Detected Face);

Finally, use the drawnow function to generate the output.


Step 7

Drawnow
Save this script with a required name & run the script on RPi over serial
connection.

To avoid creating repeatedly used variables again & again, use


Step 8

clearvars with an except as below;


clearvars -except mypi mycam flag

Create the above 3 variables outside the while loop to avoid


repeated initialization

Step 9

To make it an infinite loop, declare a variable called flag &


initialise it to 1 & put all the above code in a while loop:
flag = 1;
while flag
<All the above code goes here>
end

Full Code
clearvars
mypi = raspi();
mycam = cameraboard(mypi, 'Resolution', '640x480', 'Brightness', 65);
mysnap = snapshot(mycam);
flag = 1;
while flag
clearvars -except mypi mycam flag
mysnap = snapshot(mycam);
imshow(mysnap);
hold on
fD = vision.CascadeObjectDetector
bbox = step(fD, mysnap);
imageOut = insertObjectAnnotation(mysnap, 'rectangle', bbox, 'Face');
imshow(imageOut);
title('Detected face');
drawnow
end

Method 2: Using the video player:


Create the mypi & mycam objects as described in Method 1.
Step 1

You can also capture an image using the snapshot() function.


mysnap = snapshot(mycam);

There is a video player supplied in the Computer Vision Toolbox


that can be used to display the video captured from the camera
board.
myPlayer = vision.VideoPlayer;

Plot the video player & the acquired image using:


Step 2

step(myPlayer, mysnap);

We will use the CascadeObjectDetector function from the


computer vision toolbox to process the frames in this case too.
fD = vision.CascadeObjectDetector();
bbox = step(fD, mysnap);

Once the processing is done, generate an output image with a


Step 3

rectangle added as an annotation around the detected face.


imageOut = insertObjectAnnotation(mysnap, rectangle, bbox, Face);

Instead of using a flag to drive an infinite loop, we can also use


the isOpen() function to keep the script running until the
video player is closed.
while isOpen(myPlayer)
<All code goes here>
end

Full Code:
mypi = raspi();
myCam = cameraboard(mypi,'Resolution','640x480');
mySnap = snapshot(myCam);
myPlayer = vision.VideoPlayer;
step(myPlayer,mySnap);
while isOpen(myPlayer)
clearvars -except mypi myCam myPlayer
mySnap = snapshot(myCam);
step(myPlayer,mySnap);
fD = vision.CascadeObjectDetector();
bbox = step(fD, mySnap);
imageOut = insertObjectAnnotation(mySnap,'rectangle',bbox,'Face');
step(myPlayer,imageOut);
end

Final Result:

You might also like