Running function in parallel on App Designer

Hello,
I'm building an app which suppose to capture images from the camera while doing more things in parallel. I'm using 'parfeval' to run the function in the background and 'afterEach' to print it every time an image is captured.
Those are the functions:
methods (Access = private)
function getFrameFromCamera(app)
h = msgbox("Connecting to camera..", "Message");
app.Cam = gigecam(app.ip);
app.Cam.PixelFormat = 'BayerGB8';
app.Cam.Timeout = 15;
app.Cam.GevSCPSPacketSize = 1200;
app.Cam.Width = 1920;
app.Cam.Height = 1080;
app.Cam.CenterX = 'True';
app.Cam.CenterY = 'True';
delete(h);
while true
app.tmpImage = snapshot(app.Cam);
send(app.D, app);
end
end
function processDisp(app)
app.Image3.ImageSource = app.tmpImage;
end
end
The code in the button's function:
function ConnectButtonPushed(app, event)
app.ConnectedLamp.Color = 'yellow';
pause(0.05);
try
if ~isempty(app.CameraIPListBox.Value)
h = msgbox("Checking if the camera is available..", "Message");
app.ip = app.CameraIPListBox.Value;
gigecam(app.ip);
delete(h);
h = msgbox("Starting parallel pool (parpool) using the 'local' profile..", "Message");
imaqreset;
delete(gcp('nocreate'));
parpool('local', 1);
app.D = parallel.pool.DataQueue;
afterEach(app.D, @processDisp);
%app.Process = 0;
delete(h);
app.f = parfeval(@getFrameFromCamera, 0, app);
app.ConnectedLamp.Color = 'green';
end
catch
delete(h);
app.ConnectedLamp.Color = 'red';
msgbox("The camera is in use.", "Error");
end
end
For some reason, i get this warning twice and the functions don't excecute:
Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects
I tried to run in the background simple functions which only print and i still get the same results. I also tried to send every variable seperatly, and not the "app" variable - same results. Any idea how to solve this?

4 件のコメント

Mario Malic
Mario Malic 2021 年 3 月 29 日
編集済み: Mario Malic 2021 年 3 月 29 日
Hello,
What are app.d and app.f?
The save warning probably comes from this line, as the send function tries to save the app and pass it onto workers.
send(app.D, app);
It'll be hard to say what's actually an issue, as I do not completely understand the code nor the parallel computing stuff. I did something similar and it works. I think the issue is that workers do not have access to the handles that you pass to them, because each worker has its own MATLAB instance. But, you can actually put the image in the app with afterEach, because this function is ran on the MATLAB instance that the app is called from (probably). Remove processDisp method from the app and create a function file instead of it.
function processDisp(inputarguments)
persistent app
if isempty(app)
app = get(findall(groot, 'type', 'figure'), 'RunningAppInstance'); % getting app handle
end
app.Image3.ImageSource = inputarguments;
% or
% app.Image3.ImageSource = load some file on disk
end
If you don't have any other figures open while you run your app, getting app handle will be fine, otherwise, set it's name and filter with that as well.
You have to test out for yourself what are the input arguments actually. Performance with the app that I got this way, is not good at all, when I wanted to display some value, I would have to wait for 10-30 seconds, which is not good enough. I think that comes from passing too much data from all workers and then the queue data becomes to large to be updated immediately.
If you have some issues, try putting these in a separate function, outside of App Designer.
delete(gcp('nocreate'));
parpool('local', 1);
app.D = parallel.pool.DataQueue;
afterEach(app.D, @processDisp);
%app.Process = 0;
delete(h);
app.f = parfeval(@getFrameFromCamera, 0, app);
I know I haven't put everything I said in the code, but I hope it helps.
Gal Elias
Gal Elias 2021 年 3 月 29 日
Thanks a lot for your affort! I found out the problem and now it works :) it seems like if I put the functions under "method" or even if I put them in a seperate .m file, the DataQueue can't get the value. When I use seperate .m file for them, on the seperate matlab script I can get the image from the camera, but if I try to do the same from App Designer, i get an empty array. It seems like App Designer can't receive values from external functions when they run in parallel (maybe i'm wrong?). I solved it by putting "getFrameFromCamera" function inside the button function and used "poll" to get the image.
Mario Malic
Mario Malic 2021 年 3 月 29 日
Interesting. It's great that it works! It would be great if you could share the code as an answer, as I'm also interested in this.
Gal Elias
Gal Elias 2021 年 3 月 29 日
Sure :) I guess it's not the most efficient code but atleast it works. This is the code of the button you need to press to connect to a gigE camera from a list of IPs and it displays the input on an image object:
% Button pushed function: ConnectButton
function ConnectButtonPushed(app, event)
app.ConnectedLamp.Color = 'yellow';
pause(0.05);
try
if ~isempty(app.CameraIPListBox.Value)
h = msgbox("Checking if the camera is available..", "Message");
app.ip = app.CameraIPListBox.Value;
gigecam(app.ip);
delete(h);
h = msgbox("Starting parallel pool (parpool) using the 'local' profile..", "Message");
imaqreset;
delete(gcp('nocreate'));
parpool('local', 1);
app.D = parallel.pool.PollableDataQueue;
app.Process = 0;
app.f = parfeval(@getFrameFromCamera1, 0, app.D, app.ip);
pause(15);
delete(h);
app.ConnectedLamp.Color = 'green';
while ~app.Process
tmpImg = poll(app.D);
pause(0.01);
if isempty(tmpImg)
continue;
end
app.tmpImage = tmpImg;
app.Image3.ImageSource = app.tmpImage;
end
end
catch ME
delete(h);
msgbox(ME.message, "Error");
app.img1 = 0;
app.img2 = 0;
app.Process = 0;
app.tmpImage = 0;
delete(gcp('nocreate'));
imaqreset;
pause(0.01);
app.ConnectedLamp.Color = 'red';
end
function getFrameFromCamera1(D, ip)
g = gigecam(ip);
g.PixelFormat = 'BayerGB8';
g.Timeout = 15;
g.GevSCPSPacketSize = 1200;
g.Width = 1920;
g.Height = 1080;
g.CenterX = 'True';
g.CenterY = 'True';
while true
img = snapshot(g);
send(D, img);
end
end
app.Process is a variable which changes its state when i press a different button and all the pause functions is because it takes time to aquire an image.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB Support Package for IP Cameras についてさらに検索

製品

リリース

R2019a

質問済み:

2021 年 3 月 29 日

コメント済み:

2021 年 3 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by