matlab2019a使用深度学习对网络摄像机图像进行分类(Deep Learning Toolbox系列篇3)

12 篇文章 23 订阅

此示例说明如何使用预训练的深度卷积神经网络 GoogLeNet 实时对来自网络摄像机的图像进行分类。

使用 MATLAB®、普通的网络摄像机和深度神经网络来识别周围环境中的对象。此示例使用 GoogLeNet,它是预训练的深度卷积神经网络(CNN 或 ConvNet),已基于超过一百万个图像进行训练,可以将图像分为 1000 个对象类别(例如键盘、咖啡杯、铅笔和多种动物)。您可以下载 GoogLeNet 并使用 MATLAB 实时连续处理照相机图像。

GoogLeNet 已基于大量图像学习了丰富的特征表示。它以图像作为输入,然后输出图像中对象的标签以及每个对象类别的概率。您可以用周围的物品进行试验,以了解 GoogLeNet 对图像进行分类的准确程度。要了解有关网络对象分类的详细信息,可以实时显示排名前五的类的分数,而不是只显示最终的类决策。

加载照相机和预训练网络
连接到照相机并加载预训练的 GoogLeNet 网络。您可以在此步骤使用任何预训练网络。该示例需要 MATLAB Support Package for USB Webcams,以及 Deep Learning Toolbox™ Model for GoogLeNet Network。如果没有安装所需的支持包,软件会提供下载链接。在报错的红字中有安装的路径(Add-On-Explorer),只需要点击进去登陆便可以下载此模块:

demo1;

camList = webcamlist

% Connect to the webcam.
cam = webcam(1)

preview(cam);

img = snapshot(cam);

% Display the frame in a figure window.
image(img);

for idx = 1:5
    img = snapshot(cam);
    image(img);
end

%% Once the connection is no longer needed, clear the associated variable.
% clear cam

当使用完摄像头之后,记得用 clear cam断开连接,不然会耗费资源和影响在此调用。

 demo2:

clear
close all
clc
camera = webcam;
net = googlenet;

%% 加载照相机和预训练网络
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)});


h = figure;

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

h = figure;
h.Position(3) = 2*h.Position(3);
ax1 = subplot(1,2,1);
ax2 = subplot(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)});

[~,idx] = sort(score,'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,'Probability')
yticklabels(ax2,classNamesTop)
ax2.YAxisLocation = 'right';

while ishandle(h)
    % 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 = string(classes(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

 

 

  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拦路雨g

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值