In this blog post, we will see how to do image classification based on images captured from web camera using matlab
Prerequisites
As a prerequisite, we will require the following add-ons, which can be installed from the add-ons manager. Once the installation is complete, you should be able to use the built-in alexnet
and webcam
functions.
- USB webcams add-on
- Alex deep learning toolbox
Image classification from web camera
% STEP-1: Start web camera and take a snapshot cam = webcam; im = snapshot(cam); imshow(im) % STEP-2: Initialize alex network and classify the image net=alexnet; I = imresize(im, [227,227]); sz = net.Layers(1).InputSize; I = I(1:sz(1),1:sz(2),1:sz(3)); label = classify(net,I)
Create a new script from New -> Script
& Let’s name it say, web_cam_example.m
and Execute it by clicking on run icon.
Snippet explanation
In the above snippet, we are loading the webcam
function and taking a snapshot then, we are loading alexnet
neural network and resizing the captured image. Please note that we have hard-coded the image size to 227 x 227 because that is the size of the neural network and the condition is that our image resolution should be equal to the size for the neural network. You can determine the size of the network by executing sz
, just type sz
in the matlab terminal.
Result
If you haven’t seen the previous blog post of this series please refer to this link
Matlab Facts
- Matlab was invented by Cleave moler at the University of New Mexico in 1984
- Originally written in Fortran, it was later re-written in C