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
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
2021 年 3 月 29 日
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
2021 年 3 月 29 日
回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB Support Package for IP Cameras についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!