Matlab——基于Deep Learning Toolboxs调用预训练模型实现简单的深度学习CV任务

1.工具箱准备

可以直接搜索名称在matlab中下载,大家只要登录账号即可免费下载,不需要edu邮箱哦,QQ邮箱就可。

  • USB摄像头工具箱:OS Generic Video Interface
  • Deep Learning Toolboxs
  • 预训练的深度学习模型(运行模型时按照提示下载即可)

大家可以直接运行代码,按照报错提示下载即可

2.摄像头使用

(1)调用摄像头并拍照显示

clear
close all
clc
vid = videoinput('winvideo',1);
preview(vid);
frame = getsnapshot(vid); % 拍照
figure;imshow(frame);

(2)调用摄像头并设置复杂参数

clc; 
clear all;
close all;
vid = videoinput('winvideo', 2, 'YUY2_640x480'); % 设置摄像头尺寸
set(vid,'ReturnedColorSpace','rgb'); % 
vidRes=get(vid,'VideoResolution');
width=vidRes(1);
height=vidRes(2);
nBands=get(vid,'NumberOfBands');
figure('Name', 'Matlab调用摄像头', 'NumberTitle', 'Off', 'ToolBar', 'None', 'MenuBar', 'None');
hImage=image(zeros(vidRes(2),vidRes(1),nBands));
preview(vid,hImage);

3.轻量代码调用预训练模型实现CV任务

(1)图像分类任务

clear
close all
clc
%% 初始变量定义
vid = videoinput('winvideo', 2, 'YUY2_640x480'); % Connect to the camera
set(vid,'ReturnedColorSpace','rgb'); % 设置为RGB格式,否则会默认为BGR格式

net = alexnet;   % 定义网络结构
%% 主程序
while true
    im = getsnapshot(vid); % 取当前帧
    image(im);            % 显示照片
    im = imresize(im,[227 227]); % 缩减图像尺寸至模型输入要求
    
    label = classify(net,im);    % 输入模型进行推理预测
    title(char(label));          % 展示预测的label
    drawnow
end

识别结果有点一言难尽。。。。反正大家用一下图个乐呵就行。
在这里插入图片描述

(2)目标检测任务

做一个小的人脸识别:

clear
close all
clc
%% 初始变量定义
vid = videoinput('winvideo', 2, 'YUY2_640x480'); % 定义摄像头参数
set(vid,'ReturnedColorSpace','rgb'); % 设置为RGB格式,否则会默认为BGR格式

faceDetector = vision.CascadeObjectDetector(); % 定义人脸识别工具
%% 主程序
while true
    im = getsnapshot(vid); % 取当前帧
    im = imresize(im,[227 227]); % 缩减图像尺寸
    
    bbox = step(faceDetector, im); % 将图片输入人脸检测器,返回识别结果的bounding box信息
    imgOut = insertObjectAnnotation(im,'rectangle',bbox,'Face'); % 给原图识别结果画框
    
    imgOut = imresize(imgOut,[640 480]); % 重新放回原尺寸,便于观察效果
    image(imgOut);            % 显示照片
end

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45779334/article/details/115382468