MATLAB學習help之——Classify Webcam Images Using Deep Learning from Neural Network Toolbox

這個例子是利用已經學習好的Alexnet 對攝像頭中圖像進行實時識別

Step1.
加載網絡以及網絡攝像頭, 由于沒有網絡攝像頭暫時用筆記本上攝像頭代替

camera = webcam;
net = alexnet;

Step 2.
拍照并識別

inputSize = net.Layers(1).InputSize(1:2)

figure
im = snapshot(camera);
image(im)
im = imresize(im,inputSize);
[label,score] = classify(net,im);
title({char(label),num2str(max(score),2)});

效果如下


圖片.png

可能由于像素等原因,識別的結果有些偏差

Step3
連續(xù)識別,讓攝像頭一直連續(xù)拍照并識別,并顯示識別概率靠前的5個的概率值

classNames = net.Layers(end).ClassNames;

h = figure;
h.Position(3) = 2*h.Position(3);
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
ax2.ActivePositionProperty = 'position';

keepRolling = true;
set(gcf,'CloseRequestFcn','keepRolling = false; closereq');

while keepRolling
    % Display and classify the image
    im = snapshot(camera);
    image(ax1,im)
    im = imresize(im,inputSize);
    [label,score] = classify(net,im);
    title(ax1,{char(label),num2str(max(score),2)});

    % Select the top five predictions
    [~,idx] = sort(score,'descend');
    idx = idx(5:-1:1);
    scoreTop = score(idx);
    classNamesTop = classNames(idx);

    % Plot the histogram
    barh(ax2,scoreTop)
    title(ax2,'Top 5')
    xlabel(ax2,'Probability')
    xlim(ax2,[0 1])
    yticklabels(ax2,classNamesTop)
    ax2.YAxisLocation = 'right';

    drawnow
end

效果如下


圖片.png
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容