This example shows how to build a smile detector by using the OpenCV Importer. The detector estimates the intensity of the smile on a face image. Based on the estimated intensity, the detector identifies an appropriate emoji from its database, and then places the emoji on the smiling face.
First import an OpenCV function into Simulink by using the OpenCV Code Import Wizard. The wizard creates a Simulink® library that contains a subsystem and a C Caller block for the specified OpenCV function. The subsystem is then used in a preconfigured Simulink model to accept the face image for smile detection. You can generate C++ code from the model, and then deploy the code on your target hardware.
You learn how to:
Import an OpenCV function into a Simulink library.
Use blocks from a generated library in a Simulink model.
Generate C++ code from a Simulink model.
Deploy the model on the Raspberry Pi hardware.
Computer Vision Toolbox™ Interface for OpenCV in Simulink
Computer Vision Toolbox
Embedded Coder® (for deployment)
To build the OpenCV libraries, identify a compatible C++ compiler for your operating
system, as described in Compiler Used to Build OpenCV Libraries. Configure the
identified compiler by using the
mex -setup c++ command. For more
information, see Choose a C++ Compiler.
In this example, a smile detector is implemented by using the Simulink model
In this model, the
subsystem_slwrap_detectAndDraw subsystem resides
Smile_Detect_Lib library. You create the
subsystem_slwrap_detectAndDraw subsystem by using the OpenCV
Importer. The subsystem accepts a face image from the Image From
Workspace block and provides these output values.
|out||Face image with a circle|
|intensity||Intensity of the smile|
|x||x coordinate of center of the circle|
|y||y coordinate of center of the circle|
|rd||Radius of the circle|
The MATLAB Function block accepts input from the
subsystem_slwrap_detectAndDraw subsystem block. The MATLAB
Function block has a set of emoji images. The smile intensity of the emoji
in these images ranges from low to high. From the emoji images, the block identifies the
most appropriate emoji for the estimated intensity and places it on the face image. The
output is then provided to the Video Viewer blocks.
To access the path to the example folder, at the MATLAB® command line, enter:
Before proceeding with these steps, ensure that you copy the example folder to a
writable folder location and change your current working folder to
...example\SmileDetector. All your output files are saved to this
To start the OpenCV Importer app, click Apps
on the MATLAB Toolstrip. In the Welcome page, specify the Project
Smile_Detector. Make sure that the
project name does not contain any spaces. Click
In Specify OpenCV Library, specify these file locations, and then click Next.
Project root folder: Specify the path of your example folder. This path is the path to the writable project folder where you have saved your example files. All your output files are saved to this folder.
Source files: Specify the path of the
.cpp file located inside your project folder
Include files: Specify the path of the
.hpp header file located inside your project
Analyze your library to find the functions and types for import. Once the
analysis is complete, click Next. Select the
detectAndDraw function and click
From What to import, select the
I/O Type for
Input, and then click
In Create Simulink Library, verify the default values and click Next.
A Simulink library
Smile_Detector_Lib is created from your
OpenCV code into the project root folder. The library contains a subsystem and a
C Caller block. You can use any of these blocks for model simulation. In this
example, the subsystem
To use the generated subsystem
the Simulink model
In your MATLAB
Current Folder, right-click the model
smileDetect.slx and click
Open from the context menu. In the model,
delete the existing
subsystem and drag the generated subsystem
subsystem_slwrap_detectAndDraw from the
Smile_Detector_Lib library to the model. Connect the
subsystem to the MATLAB Function block.
Double-click the subsystem and specify these parameter values.
|Rows||512||Number of rows in the output image|
|Columns||512||Number of columns in the output image|
|Channels||3||Number of channels in the output image|
|Underlying Type||uint8||Underlying data type of OpenCV
|is Image||on||Whether input is an image or a matrix|
Click Apply, and then click OK.
On the Simulink Toolstrip, in the Simulation tab, click to simulate the model. After the simulation is complete, the Video Viewer block displays an image with an emoji on the face. The emoji represents the intensity of the smile.
Before you generate the code from the model, you must first ensure that you have write permission in your current folder.
To generate C++ code:
smileDetect_codegen.slx model from your MATLAB
To review the model settings:
On the Apps tab on the Simulink toolstrip, select Embedded Coder.
On the C++ Code tab in the
Settings drop-down list, click
Code generation settings to open the Configuration
Parameters dialog box and verify these settings:
In the Code Generation pane, under
Language is set to
In the Interface under
Code Generation, Array
layout in the Data exchange
interface category is set to
Connect the generated subsystem
subsystem_slwrap_detectAndDraw to the MATLAB Function
To generate C++ code, under the C++ Code tab, click
Generate Code drop-down list, and then click
Build. After the model finishes generating code,
the Code Generation Report opens. You can inspect the generated code. The
build process creates a zip file called
smileDetect_with_ToOpenCV.zip in your current MATLAB
Before you deploy the model, connect the Raspberry Pi to your computer. Wait until the PWR LED on the hardware starts blinking.
In the Settings drop-down list, click
Implementation to open the Configuration Parameters dialog box and
verify these settings:
Set the Hardware board to
Pi. The Device Vendor is set to
In the Code Generation pane, under
Target selection, Language is
C++. Under Build process,
Zip file name is set to
Toolchain settings, the
Toolchain is specified as
To deploy the code to your Raspberry Pi hardware:
From the generated zip file, copy these files to your Raspberry Pi hardware.
In Raspberry Pi, go to the location where you saved the files. To generate
elf file, enter this command:
make -f smileDetect.mk
Run the executable on Raspberry Pi. After successful execution, you see the output on Raspberry Pi with an emoji placed on the face image.