主要内容

Classify Webcam Images Using Deep Learning

This example shows how to classify images from a webcam in real time using the pretrained deep convolutional neural network GoogLeNet.

Use MATLAB®, a simple webcam, and a deep neural network to identify objects in your surroundings. This example uses GoogLeNet, a pretrained deep convolutional neural network (CNN or ConvNet) that has been trained on over a million images and can classify images into 1000 object categories (such as keyboard, coffee mug, pencil, and many animals). You can download GoogLeNet and use MATLAB to continuously process the camera images in real time.

GoogLeNet has learned rich feature representations for a wide range of images. It takes the image as input and provides a label for the object in the image and the probabilities for each of the object categories. You can experiment with objects in your surroundings to see how accurately GoogLeNet classifies images. To learn more about the network's object classification, you can show the scores for the top five classes in real time, instead of just the final class decision.

加载摄像头和预算网络

Connect to the camera and load a pretrained GoogLeNet network. You can use any pretrained network at this step. The example requires MATLAB Support Package for USB Webcams, and Deep Learning Toolbox™ Modelfor GoogLeNet Network. If you do not have the required support packages installed, then the software provides a download link.

相机= webcam; net = googlenet;

如果您想再次运行示例,请首先运行命令clear camerawhere相机是与网络摄像头的连接。否则,您会看到错误,因为您无法创建与同一网络摄像头的另一个连接。

Classify Snapshot from Camera

对图像进行分类,必须调整的put size of the network. Get the first two elements of the输入property of the image input layer of the network. The image input layer is the first layer of the network.

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

用预测的标签及其概率从相机显示图像。您必须在调用之前将图像大小调整到网络的输入大小分类.

图IM =快照(相机);image(im)im = Imresize(im,InputSize);[标签,分数] =分类(net,im);标题({char(label),num2str(max(score),2)});

连续地从相机分类图像

To classify images from the camera continuously, include the previous steps inside a loop. Run the loop while the figure is open. To stop the live prediction, simply close the figure. Use绘制在每次迭代结束时更新图。

h =图;尽管isHandle(h)im =快照(相机);image(im)im = Imresize(im,InputSize);[标签,分数] =分类(net,im);标题({char(label),num2str(max(score),2)});绘制end

显示顶部预测

The predicted classes can change rapidly. Therefore, it can be helpful to display the top predictions together. You can display the top five predictions and their probabilities by plotting the classes with the highest prediction scores.

Classify a snapshot from the camera. Display the image from the camera with the predicted label and its probability. Display a histogram of the probabilities of the top five predictions by using thescore输出分类function.

创建图形窗口。首先,调整窗口的大小,使其具有两倍的宽度,并创建两个子图。

h =图;h.position(3)= 2*h.position(3);AX1 =子图(1,2,1);AX2 =子图(1,2,2);

在左图中,一起显示图像和分类。

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 by selecting the classes with the highest scores.

[〜,idx] = stort(得分,'descend');IDX = IDX(5:-1:1);classes = net.Layers(end).Classes; classNamesTop = string(classes(idx)); scoreTop = score(idx);

将前五个预测显示为直方图。

barh(ax2,scoreTop) xlim(ax2,[0 1]) title(ax2,'Top 5') xlabel(ax2,'可能性') yticklabels(ax2,classNamesTop) ax2.YAxisLocation ='正确的';

Continuously Classify Images and Display Top Predictions

To classify images from the camera continuously and display the top predictions, include the previous steps inside a loop. Run the loop while the figure is open. To stop the live prediction, simply close the figure. Use绘制在每次迭代结束时更新图。

创建图形窗口。首先调整窗口的大小,具有两倍的宽度,并创建两个子图。为防止轴调整大小,设置PositionConstraintproperty to“内在”.

h =图;h.position(3)= 2*h.position(3);AX1 =子图(1,2,1);AX2 =子图(1,2,2);ax2. positionconstraint =“内在”;

连续显示和分类图像与前五名预测的直方图。

尽管ishandle(h)% Display and classify the imageim = 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] = stort(得分,'descend');IDX = IDX(5:-1:1);ScoreTop =得分(IDX);ClassNamestop = String(class(idx));%绘制直方图barh(ax2,scoreTop) title(ax2,'Top 5') xlabel(ax2,'可能性') xlim(ax2,[0 1]) yticklabels(ax2,classNamesTop) ax2.YAxisLocation ='正确的';绘制end

See Also

||

相关话题