- A shared cell array “taskQueue” can be used to store tasks. Each cell entry consists of an image and its corresponding filename.
- When workers 2 and 3 capture an image, they append the image and filename to the “taskQueue”.
- Workers 4 and 5 check the queue for available tasks. If a task is found, they process it by saving the image and removing the task from the queue.
- A pause can be added to minimize the CPU usage when the queue is empty.
Assign different tasks to each worker in the SPMD block for multi-camera image capture and saving.
4 views (last 30 days)
Show older comments
I have two cameras and I can created 2 worker in spmd and capture/save image. But I want to create 5 workers through spmd and assign different tasks to them. Worker 1 receives position measurements. Worker 2 and 3 capture images, and worker 4 and 5 receive data from worker 1 for file name and save images captured by worker 2 and 3.
Is there a feasible solution? If not, can I have 4 workers. 2 captue image and 2 save image?
My codes was based on the 'example_videoinput_spmd_logtodisk.m'
if ~isempty(gcp('nocreate'))
delete(gcp('nocreate'));
end
%% camera connection
%Create connection to the device using the specified adaptor with the specified format.
%% Create a parallel pool with two workers, one per camera
if isempty(gcp('nocreate'))
parpool(2)
end
%% Disconnect from all cameras from main MATLAB process and workers
delete(imaqfind);
spmd(2)
delete(imaqfind);
end
% Create videoinput objects (one camera per worker)
spmd(2)
% labBarrier ensures that the camera detection code is called
% by only one worker at a time.
for idx = 1:spmdSize
if idx == spmdIndex
imaqreset
% Configure acquisition to not stop if dropped frames occur
% imaqmex('feature', '-gigeDisablePacketResend', true);
% Detect cameras
gigeinfo = imaqhwinfo('gentl');
numCamerasFound = numel(gigeinfo.DeviceIDs);
fprintf('Worker %d detected %d cameras.\n', ...
spmdIndex, numCamerasFound);
end
spmdBarrier
end
cameraID = spmdIndex;
v = videoinput('gentl', cameraID,'RGB8');
v.ReturnedColorspace = "rgb";
s = v.Source;
% Configure properties common for both cameras
% s.PacketSize = 4000;
% s.ExposureTimeAbs = 10000;
% v.FramesPerTrigger = 1000;
s.AcquisitionFrameRate = 2;
end
T = 5; % minutes
ti = datetime('now');
tf = datetime('now');
spmd(2)
while (minutes(tf-ti) < T)
img = getsnapshot(v);
t = datetime(datetime('now','Format','dd-MM-yyyy_HH_mm_ss_SSS'));
file_name = string(t) + ".png";
imwrite(img,file_name);
tf = datetime('now');
end
end
0 Comments
Answers (1)
Ayush
on 30 Sep 2024
I understand you need to create 5 workers through “spmd” and assign different tasks to them.
Some key modifications which can be done:
I’ve attached modified code for your reference:
if ~isempty(gcp('nocreate'))
delete(gcp('nocreate'));
end
if isempty(gcp('nocreate'))
parpool(5);
end
delete(imaqfind);
spmd(5)
delete(imaqfind);
end
taskQueue = cell(0, 2); % Column 1: Image, Column 2: Filename
spmd(5)
if spmdIndex == 1
% Worker 1: Position measurement (mockup)
while true
position = rand(1, 2);
disp(['Worker 1: Position = ', num2str(position)]);
pause(1);
end
elseif spmdIndex == 2 || spmdIndex == 3
% Workers 2 and 3: Capture images
imaqreset;
gigeinfo = imaqhwinfo('gentl');
cameraID = spmdIndex;
v = videoinput('gentl', cameraID, 'RGB8');
v.ReturnedColorspace = "rgb";
s = v.Source;
s.AcquisitionFrameRate = 2;
while true
img = getsnapshot(v);
t = datetime('now', 'Format', 'dd-MM-yyyy_HH_mm_ss_SSS');
file_name = sprintf('img_%d_%s.png', spmdIndex, char(t));
% Schedule task by adding to the queue
taskQueue{end+1, 1} = img; % Add image
taskQueue{end, 2} = file_name; % Add filename
disp(['Worker ', num2str(spmdIndex), ': Captured ', file_name]);
% Optionally, you can add a short pause
pause(1);
end
elseif spmdIndex == 4 || spmdIndex == 5
% Workers 4 and 5: Save images
while true
if ~isempty(taskQueue)
% Process the next task in the queue
img = taskQueue{1, 1}; % Get the first image
file_name = taskQueue{1, 2}; % Get the corresponding filename
% Remove the processed task from the queue
taskQueue(1, :) = [];
% Save the image
imwrite(img, file_name);
disp(['Worker ', num2str(spmdIndex), ': Saved ', file_name]);
else
% If no tasks are available, pause
pause(1); % Avoid busy waiting
end
end
end
end
You can read more about “imwrite” function here: https://www.mathworks.com/help/matlab/ref/imwrite.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Distributed Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!