Code covered by the BSD License  

Highlights from
CameraGUI

image thumbnail

CameraGUI

by

Georg D (view profile)

 

access all connected cameras, take snapshots, record streams, access and process frames in realtime

RecordToAvi(camobj, capturetime, destfile, frameinterval)
%this function depends on unixtime.m


function [abstimestamps] = RecordToAvi(camobj, capturetime, destfile, frameinterval)

if nargin<2
   error('You have to choose a camera and capture time!')
end
if nargin<3 || isempty(destfile)
    curtime = clock();
    curfolder = fullfile(getenv('USERPROFILE'), 'Desktop');
    curfilename = strcat (date(),'_',num2str(curtime(4)),'-',num2str(curtime(5)),'-',num2str(curtime(6)),'.avi');
    curfilenametimestamps = strcat (date(),'_',num2str(curtime(4)),'-',num2str(curtime(5)),'-',num2str(curtime(6)),'.mat');
    destfile = fullfile(curfolder,curfilename);
    destfiletimestamps = fullfile(curfolder,curfilenametimestamps);
else
    [saveMainPath, name, ext, versn] = fileparts(destfile);
    destfiletimestamps = fullfile(saveMainPath,[name,'.mat']);
end
if nargin<4
    frameinterval = 1;
end

capturetime = abs(capturetime);
frameinterval = abs(frameinterval);

% set globals for capturing timestamps in callback function
global framecounter % counting frames that are stored in curtimestamps
global curframenr % the camera may skip frames; so, i rather store all the frame numbers that the camera actually gives me
global curtimestamps % timestamps that the camera returns
framecounter = 0;

framerate = measureFrameRate (camobj,20);
% e.g. if we want to compress 30 seconds into 3 seconds, so
% only acquire every tenth frame: set(vid,'FrameGrabInterval',10);
% acquiring always starts with 1st frame per trigger, then skip (framegrabinterval - 1) frames, then
% next frame, and so on
set (camobj,'FrameGrabInterval',frameinterval);
framerate = framerate / frameinterval; %'real' streamed frames

%available logging modes
loggingModes = set(camobj, 'LoggingMode');
disp ('available logging modes:')
disp (loggingModes)
disp ('---------------------------------------')

% Configure the logging mode to disk.
set(camobj, 'LoggingMode', 'disk&memory');

% Verify the configuration.
currentLoggingMode = get(camobj, 'LoggingMode');
disp ('current logging mode:')
disp (currentLoggingMode)
disp ('---------------------------------------')


%set capturing length
numframes = round(capturetime * framerate);
if numframes == 0
    numframes = 1; 
end

%set up avifile object
logfile = avifile(destfile);
logfile.compression = 'none';
%logfile.videoname = 'myfirstVideo';
if strcmp(camobj.ReturnedColorSpace, 'grayscale')==1
    logfile.colormap = gray(256);
end
logfile.fps = framerate;
set(camobj,'DiskLogger',logfile);

% set frames per callback (fpf): for avi we hardcode to ~1 sec
camobj.UserData = 1; %this is going to be our counter for FrameSave (per function callback, NOT per frame)
fpf = round(framerate); %how many frames per callback function (FrameSave) (choose framerate => ~1 second)
if fpf == 0
    fpf = 1; 
end
%capturing length has to be a multiple of fpf
numframes = fpf * ceil(numframes/fpf);

%preallocate memory for timestamps storing
curtimestamps = zeros(numframes*2,6);
curframenr = zeros(1,numframes*2);

set(camobj,'FramesAcquiredFcn',{@FrameSave});
set(camobj,'FramesAcquiredFcnCount',fpf);
set(camobj,'FramesPerTrigger',numframes);
% no need for 'set(camobj,'Timeout',1);'

%set up abort button
mygui = openfig('quickguismall.fig','new'); hmygui = guihandles(mygui);
set(hmygui.togglebutton1,'String','Abort Capturing');
set(hmygui.togglebutton1,'ForegroundColor',[1 0 0]);
set(hmygui.figure1,'Name','Stop Capturing');

%start capturing
start(camobj);
starttime = unixtime(clock());
endtime = starttime + capturetime;
disp(['capturetime (according to determined framerate): ',num2str(capturetime), ' seconds']);
abortcapture = false;
while unixtime(clock()) <= endtime && ~abortcapture
    timecounter = unixtime(clock()) - starttime;
    disp(['seconds passed: ', num2str(floor(timecounter)), ' of ',num2str(capturetime)])
    disp(['frames already acquired: ',num2str(camobj.FramesAcquired)])
    disp (strcat('frames successfully streamed to disk:',num2str(camobj.DiskLoggerFrameCount)))
    disp('-------------------------------------------------------------')
    pause(0.5);
    if get(hmygui.togglebutton1,'Value')==1
       abortcapture = true; 
    end
end
if abortcapture
    stop(camobj);
end
wait(camobj,Inf);
close (mygui);

counter = 0;
while (camobj.DiskLoggerFrameCount < camobj.FramesAcquired)
    pause(1)
    counter = counter + 1;
    if counter == 5
        break
    end
end


% we may need to fetch some  more timestamps
pause(0.2);
if abortcapture && camobj.FramesAvailable > 0
    [data,time,abstime] = getdata(camobj,camobj.FramesAvailable);
    curlen = (length(time));
    curfrcounter = framecounter;
    framecounter = framecounter + curlen;
    curtimestamps(curfrcounter+1:curfrcounter+curlen,:)=cell2mat({abstime(:).AbsTime}');
    curframenr(curfrcounter+1:curfrcounter+curlen)=cell2mat({abstime(:).FrameNumber});
end


set(camobj,'FramesAcquiredFcn',{},'FramesAcquiredFcnCount',0)


% Determine the number of frames acquired.
frsAcquired = camobj.FramesAcquired;
disp (strcat('total frames acquired:',num2str(frsAcquired)))

% Ensure that all acquired frames were written to disk.
frsSaved = camobj.DiskLoggerFrameCount;
disp (strcat('frames successfully streamed to disk:',num2str(frsSaved)))


%retrieve avi file object and close file
aviobj = get(camobj,'DiskLogger');
aviobj = close(aviobj);


%timestamps
%first we delete empty fields
curframenr(curframenr==0) = [];
curtimestamps(curtimestamps(:,1)==0,:) = [];
%now generate abstimestamps matrix
abstimestamps = [curframenr',unixtime(curtimestamps)];
if length(abstimestamps) < frsSaved
    disp(['Only ',num2str(size(abstimestamps,1)),' timestamps of ', num2str(frsSaved),' frames could be saved!'])
    msgbox(['Only ',num2str(size(abstimestamps,1)),' timestamps of ', num2str(frsSaved),' frames could be saved!']);
end
if size(abstimestamps,2) > frsSaved
    abstimestamps = abstimestamps(1:frsSaved,:);
end


save (destfiletimestamps, 'abstimestamps');



function FrameSave(camobj,event)
global framecounter
global curtimestamps
global curframenr
[data,time,abstime] = getdata(camobj,camobj.FramesAcquiredFcnCount);
curlen = (length(time));
curfrcounter = framecounter;
framecounter = framecounter + curlen;
curtimestamps(curfrcounter+1:curfrcounter+curlen,:)=cell2mat({abstime(:).AbsTime}');
curframenr(curfrcounter+1:curfrcounter+curlen)=cell2mat({abstime(:).FrameNumber});
camobj.UserData = camobj.UserData + 1; %we count, but dont use this counter in this script...

Contact us