這個例子是利用已經(jīng)學習好的Alexnet 對攝像頭中圖像進行實時識別
Step1.
加載網(wǎng)絡以及網(wǎng)絡攝像頭昂灵, 由于沒有網(wǎng)絡攝像頭暫時用筆記本上攝像頭代替
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)});
效果如下
可能由于像素等原因,識別的結(jié)果有些偏差
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
效果如下