Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

webcam, tiledlayout, AI, and a Refrigerator !?!

Before the pandemic (actually a couple of years before), as I was trying to find a super easy way to show the power of a pre-trained network in MATLAB, I made this example from my desk in the office. Behind there is a "board" half of which is magnetic and the other half is cloth-covered so I can hang things with pushpins on it.
Here's a picture of me standing in front of this board, scowling at my computer screen for some reason.
And here's the code I was using. But I am not in my office now so you will see different pictures.

First set up my webcam

I first set up my webcam, and make sure it's working. You may be pleasantly surprised to learn that your webcam can be used in MATLAB Online.
And I import the pretrained network alexnet.
w = webcam
w =
webcam with properties: Name: 'Microsoft® LifeCam Cinema(TM)' AvailableResolutions: {'640x480' '640x360' '424x240' '352x288' '320x240' '176x144' '160x120' '1280x720' '960x544' '800x448' '800x600'} Resolution: '640x480' Exposure: -6 BacklightCompensation: 0 Brightness: 133 Sharpness: 25 Contrast: 5 WhiteBalance: 4500 Tilt: 0 Saturation: 83 Focus: 5 ExposureMode: 'auto' WhiteBalanceMode: 'auto' Zoom: 0 Pan: 0 FocusMode: 'auto'
I could try to preview the image next to be sure it's ok. I don't feel the need right now.
net = alexnet;

Next test taking a snapshot

I next test the camera by taking a snapshot
I = snapshot(w);
imshow(I)
So far, so good.

Use the pretrained network to identify the picture contents

I resize that image to be the correct dimensions for alexnet. I could crop or resize it.
[c,r] = size(I, [1, 2]);
width = 227;
height = 227;
rect = [floor((r-height)/2), floor((c-width)/2), width-1, height-1];
I227 = imcrop(I, rect);
[label, values] = classify(net, I227);
imshow(I227)
And put the identification label in the title.
imshow(I227)
title(string(label(1)))

Now let's make a collection of similar pictures

You may remember a time when you went somewhere with friends or family and used one of those photo booths to take a sequence of (often silly) pictures. Here's how I can do that in MATLAB using tiledlayout.
t = tiledlayout("flow");
t.TileSpacing = "none";
t.Padding = "none";
numpix = 6;
for ipic = 1:numpix
imshow(snapshot(w),"Parent",nexttile)
pause(2)
end
ylabel(t,"Silly pictures")

Clean up

Finally I clean up the camera device since I'm finished using it for now. This means deleting it from MATLAB, and then clearing the variable pointing to it from my workspace.
delete(w)
clear w

Back to the original picture

When I did the same thing with the original picture from my office, the outcome made me laugh. Here it is.

What are some of your experiences?

I believe it's the magnets on the board that are drawing the network's focus for the prediction. Have you taken advantage of your webcam to have fun in MATLAB? Any unexpected results. Let us know here.
Copyright 2021 The MathWorks, Inc.

  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.