MATLAB Answers

classifyVideo function does not work on Mac

2 views (last 30 days)
Ali Dar
Ali Dar on 10 Jun 2020
Answered: Prabhan Purwar on 15 Jul 2020
I am trying this example of Transfer Learning "Deep Learning: Transfer Learning in 10 lines of MATLAB Code".
All the given code works. But when I try to classify objects in live video using Macbook cam (as shown in the end of this video), the classifyVideo(myNet) function gives an error "Unrecognized function or variable 'classifyVideo'."
Can somebody tell me how I can classify objects using live cam on Macbook?

  0 Comments

Sign in to comment.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 15 Jul 2020
Hi,
You can recreate the functionality by writing your own function which takes as input the network obj and performs the following tasks:
  1. Initializes webcam
  2. Runs infinite while loop that exits when q is pressed
  3. Within while loop:
  4. Checks for 'q' keypress
  5. Captures and image and resizes the image
  6. Classifies the image
  7. Updates figure to display current image and classification label
A sample function "classifyVideo.m" is pasted below for your reference:
function [] = classifyVideo(net)
% Initialize webcam
cam = webcam;
% Initialize figure
hf = figure;
while 1
% compares the current character to q
% if 'q' is pressed exit
if strcmp(get(hf,'currentcharacter'),'q')
close(hf)
break
end
% Acquire a single image
rgbImage = snapshot(cam);
% Resize image to fit AlexNet dimensions
resizedImage = imresize(rgbImage, [227 227]);
% Classify Image
label = classify(net,resizedImage);
% Display the image.
imshow(rgbImage);
% Display label
title(label);
% Update figure
hold on;
drawnow
% force the event queue to flush
figure(hf)
drawnow
end
% Clear camera object
clear('cam');
end
You can quickly test the function using the following:
>> net = alexnet;
>> classifyVideo(net) ;
You will have to install the "MATLAB Support Package for USB Webcams" from Add-On Explorer to be able to use the "webcam" command in the function.
More information about webcam use with MATLAB can be found here:

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!