主要内容

识别对象Within Live Video Using ResNet-50 on Raspberry Pi Hardware

此示例向您展示了如何通过使用MATLAB®支持软件包为Raspberry Pi硬件部署图像分类算法来预测Raspberry Pi™中实时视频流中的对象。金宝app该算法使用Resnet-50神经网络来识别由Raspberry Pi硬件连接的网络摄像头捕获的对象。

When you generate code for prediction, the Raspberry Pi support package builds the executable on the hardware. Using ResNet-50, the executable classifies images into 1000 object categories, such as keyboard, coffee mug, pencil, and animals. The executable then outputs a label for the object in the image along with the probabilities for each of the object categories.

您可以尝试周围环境中的对象,以查看Resnet-50对Raspberry Pi硬件上的图像进行分类。

注意:您无法使用MacOS在Raspberry Pi硬件上生成和部署深度学习代码。

需要硬件

先决条件

配置Raspberry Pi网络,使用硬件设置屏幕。在此过程中,请确保您下载Mathworks Raspbian图像以进行深度学习。如果您选择自定义硬件上的现有图像,则不要使用Mathworks Raspbian映像,请确保选择安装ARM Compute库的选项。

步骤1:连接覆盆子Pi硬件

Tip: Before you start this example, we recommend you complete the开始使用Raspberry Pi硬件的MATLAB支持包金宝app例子。

Connect the micro end of the USB cable to the Raspberry Pi and the regular end of the USB cable to the computer. Wait until the PWR LED on the hardware starts blinking.

要将视频设备连接到硬件,请执行以下任何一个:

  • If you have a webcam, connect the webcam to one of the USB ports on the hardware. Note that some webcams draw too much power and may require a powered USB hub.

  • 如果您有Raspberry Pi摄像头模块,请使用CSI电缆将摄像头模块连接到硬件。有关如何将相机模块连接到硬件的信息,请遵循制造商提供的手册中的说明。

In the MATLAB Command Window, create a connection to the Raspberry Pi hardware.

r = raspi;

步骤2:打开对象分类算法

此示例使用Resnet-50网络使用ARM Compute库显示图像分类,并返回输出中的预测分数。

有关该功能的更多信息,请在MATLAB命令窗口中输入此命令。

类型raspi_webcam_resnet
function raspi_webcam_resnet() %#codegen % Copyright 2020 The MathWorks, Inc. %Create raspi & webcam obj raspiObj = raspi(); cam = webcam(raspiObj,1); %Initialize DNN and the input size net = coder.loadDeepLearningNetwork('resnet50'); inputSize = [224, 224,3]; %net.Layers(1).InputSize; %Initialize text to display textToDisplay = '......'; % Main loop start = tic; fprintf('Entering into while loop.\n'); while true %Capture image from webcam img = snapshot(cam); elapsedTime = toc(start); %Process frames at 1 per second if elapsedTime > 1 %Resize the image imgSizeAdjusted = imresize(img,inputSize(1:2)); %Classify the input image [label,score] = net.classify(imgSizeAdjusted); maxScore = max(score); labelStr = cellstr(label); textToDisplay = sprintf('Label : %s \nScore : %f',labelStr{:},maxScore); start = tic; end %Display the predicted label img_label = insertText(img,[0,0],textToDisplay); displayImage(raspiObj,img_label); end end

您可以使用editcommand.

editraspi_webcam_resnet

Step 3:创建硬件配置对象

通过使用该硬件配置对象targetHardware在MATLAB命令窗口中函数。

board = targetHardware('覆盆子pi');

验证DeviceAddress,用户名, 和Passwordproperties listed in the output. If required, change the value of the properties by using the dot notation syntax. For example, to change the device address to172.18.185.123, enter,

board.DeviceAddress ='172.18.185.123'

Set the language of the generated code to C++ using theTargetLangproperty.

board.coderconfig.targetlang ='C++'
board =带有属性的Targethardware:名称:'Raspberry pi'deviceaddress:'172.18.182.234'用户名:'pi'密码:'***********'builddir:'/home/home/pi'enableRunonOnOnboot:0 buildAction:0 buildAction:0 buildAction:0“构建,加载和运行” CoderConfig:[1×1 Coder.codeconfig]

Create anarm-computedeep learning configuration object. Specify the architecture of Raspberry Pi.

dlcfg = coder.deeplearningconfig(“手臂计算”);dlcfg.armarchitecture ='armv7';

在MATLAB命令窗口中,执行此命令。此命令在Raspberry Pi上显示ARM计算库的版本。

R.System('字符串$ arm_computelib/lib/libarm_compute.so |GREP ARM_COMPUTE_VERSIO |切割-d \ -f 1')
ans ='arm_compute_version = v19.05'

复制版本编号并将其分配给ArmComputeVersionproperty of the deep learning configuration object.

dlcfg.ArmComputeVersion ='19 .05';

Set the深度学习property of the code generation configuration object to the deep learning configuration object.

board.coderconfig.deeplearningconfig = dlcfg
board =带有属性的Targethardware:名称:'Raspberry pi'deviceaddress:'172.18.182.234'用户名:'pi'密码:'***********'builddir:'/home/home/pi'enableRunonOnOnboot:0 buildAction:0 buildAction:0 buildAction:0“构建,加载和运行” CoderConfig:[1×1 Coder.codeconfig]

Step 4: Deploy the Function on the Hardware

Deploy the function as a standalone executable on the hardware by using thedeployfunction.

deploy(board,'raspi_webcam_resnet')
Deploying code. This may take a few minutes. Location of the generated elf : /home/pi/MATLAB_ws/R2020a/L/Work/raspi_example/Examples/raspberrypiio-ex56701774 Warning: Function 'raspi_webcam_resnet' does not terminate due to an infinite loop. Warning in ==> raspi_webcam_resnet Line: 42 Column: 1 Code generation successful (with warnings): View report

Thedeploy函数启动代码生成raspi_webcam_resnetfunction. Once the code generation is complete, MATLAB generates a code generation report. Use this report to debug theraspi_webcam_resnet在生成代码中的任何构建错误和警告的功能。

After successfully generating the code, the support package loads and runs the object classification algorithm as a standalone executable on the hardware. The executable starts classifying the objects in the acquired video, and displays the predicted labels and their associated probability. To view the Raspberry Pi screen, use a VNC viewer and perform a remote session on the hardware to get the display. You can alternatively connect a HDMI cable from the monitor to the hardware.

See Also

使用深度学习在Raspberry Pi上对静态图像进行分类