MATLABからRaspberry Piを利用し、物体認​識を行う際のWebカ​メラの利用について

7 次查看(过去 30 天)
Identify Objects Within Live Video Using ResNet-50 on Raspberry Pi Hardware を参考にRaspberry Piを用いた物体認識を行っています。
参考サイトに載っていたコードをそのまま実行すると問題なく結果がリアルタイムに表示されるのですが、ResNet-50をAlexNetに変更して利用するとカメラのキャプチャ画面が真っ暗で表示され、ラベルも同じもので固定されます。値はたまに変化しているのでプログラムが固まっている訳ではなさそうです。
実行時にエラーが表示されるなどもありません。
下記に示すコードの変更点はResNet-50をAlexNetに変更し、それに伴い入力画像サイズを変更したのみになっているつもりです。
原因が分かる方がいらっしゃれば教えていただきたいです。
変更後のソースコード
function raspi_webcam_alexnet() %変更点
%#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('alexnet'); %変更点
inputSize = [227, 227,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
変更前のソースコード(参考サイトのソースコード)
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

采纳的回答

Tohru Kikawada
Tohru Kikawada 2020-5-28
上記のAlexNetのコードを使って私のRaspberry Pi 4では動作しました。
可能性として考えられるのがResNetのモデルを実行した後に、AlexNetのモデルを実行しますと、前のプロセスがカメラをつかんでいるため画像が真っ黒になることがありました。
MATLABの画面ではエラーは確認できないですが、Raspberry Piで直接実行ファイルを起動すると下記のようなエラーが確認できました。
Examples/R2020a/raspberrypiio/IdentifyObjectsInVideoUsingResNet50OnRaspberryPiExample $ ./raspi_webcam_alexnet.elf
**** Starting the application ****
Entering into while loop.
Error: /dev/video0 is not available for I/O. System returned (16): Device or resource busy.Make sure that device is not used by another application.
Error opening camera.
この場合には前のプロセスが残っている場合があるためプロセス番号を調べて停止させる必要がありました。
pi@raspberrypi-n2rkJSh4mu:~ $ ps -x | grep rasp
3393 pts/0 Rl+ 2:09 ./raspi_webcam_alexnet.elf
3436 pts/1 S+ 0:00 grep --color=auto rasp
pi@raspberrypi-n2rkJSh4mu:~ $ sudo kill -9 3393
ご参考になれば幸いです。
  1 个评论
Tomoki Monden
Tomoki Monden 2020-5-28
ありがとうございます!!
Raspberry Pi上から実行したところ同一のエラーが表示され、プロセスをkillしたところ正しく動作しました!!
Raspberry Pi上から実行することでエラーメッセージが表示されることも学ぶことができました。
ありがとうございます。

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!