forked from mw-shiremat/MATLAB-Demo-ADI-ToF
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToFrunloop.m
77 lines (67 loc) · 2.69 KB
/
ToFrunloop.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
%% Copyright 2020 The MathWorks, Inc.
% Updated Rock-Paper-Scissors demo to work with Analog Devices ToF Depth Camera
% This requires the ToF camera to be connected to an R-Pi. The R-Pi must
% be connected to the computer running MATLAB by Ethernet. On the R-Pi, you
% must run "aditof-server.sh" before accessing the camera from MATLAB.
%% Set Region of Interest Rectangle
% This might need to change depending on what's going on in the background
% of your setup. This rectangle is setup to crop out thermal noise due to
% a halogen light that showed up at the top of the image.
rect = [1 121 640 480];
% These are needed later to position text overlays
position1 = [(rect(3)-rect(1)-160) (rect(4)-rect(2)-49)];
position2 = [(rect(3)-rect(1)-160) (rect(4)-rect(2)-19)];
%% Register the ADI ToF adapter
hwinfo= imaqhwinfo;
if ~any(strcmp(hwinfo.InstalledAdaptors, 'aditofadapter'))
imaqregister([pwd, '\aditofadapter.dll']);
imaqreset;
end
%% Open a connection to the ADI ToF camera. Be careful to not delete this
% You cannot open a second connection to the camera. You must close this
% connection before you can open another one. Be careful not to delete this
% variable. Also make sure to update the IP address as appropriate for
% your setup.
answer = questdlg('Choose the mode of communication between MATLAB and Raspberry Pi:', ...
'Camera Connection', ...
'Ethernet','USB', 'Ethernet');
% Handle response
switch answer
case 'Ethernet'
connect =1;
case 'USB'
connect = 2;
end
if connect == 1
prompt = 'Please input the IP address of your device:';
ip_addr = inputdlg(prompt,'',1,{'IP Address goes here'});
%Setup camera connection over Ethernet
depthVid = videoinput('aditofadapter', 1, ip_addr{1});
else
%Setup camera connection over USB
depthVid = videoinput('aditofadapter', 1);
end
%% Start camera input and detect hand gestures
viewer = vision.DeployableVideoPlayer();
noHandImage = zeros(rect(4)-rect(2)+1,rect(3)-rect(1)+1,3);
noHandImage = insertText(noHandImage, position1, 'No hand present');
viewer(noHandImage);
depthVid.FramesPerTrigger = 1;
depthVid.TriggerRepeat = inf;
triggerconfig(depthVid,'manual');
start(depthVid);
while isOpen(viewer)
trigger(depthVid);
depthMap = getdata(depthVid); % hand must be upright
I = imcrop(depthMap, rect);
%Use the function detectHandGestureToF to identify the correct hand
%gesture (rock -paper-scissors) in the frame.
valid = detectHandGestureToF(I,viewer,position1,position2);
if ~valid
viewer(noHandImage);
end
end
%% End camera input
stop(depthVid);
release(viewer);
delete(depthVid);