Borrar filtros
Borrar filtros

Running function in parallel on App Designer

32 visualizaciones (últimos 30 días)
Gal Elias
Gal Elias el 29 de Mzo. de 2021
Comentada: Gal Elias el 29 de Mzo. de 2021
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 comentarios
Mario Malic
Mario Malic el 29 de Mzo. de 2021
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 el 29 de Mzo. de 2021
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Support Package for IP Cameras en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by