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.
w = webcam
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
[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.
- Category:
- Data Science,
- Deep learning,
- Machine learning