Skip to content

Commit

Permalink
change to allow these to fetch images from petercorke.com if they are…
Browse files Browse the repository at this point in the history
… shipped without images/movies
  • Loading branch information
petercorke committed Feb 25, 2018
1 parent 62eccc6 commit 06bff8b
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 50 deletions.
136 changes: 99 additions & 37 deletions Movie.m
Expand Up @@ -8,6 +8,8 @@
% size Size of image
% close Close the image source
% char Convert the object parameters to human readable string
% skipttotime X
% skiptoframe X
%
% Properties::
% curFrame The index of the frame just read
Expand Down Expand Up @@ -40,7 +42,7 @@

properties

rate % frame rate at which movie was capture
rate % frame rate at which movie was captured

nframes;

Expand Down Expand Up @@ -83,17 +85,28 @@
% properties

% see if it exists on the MATLAB search path
p = fileparts( which('iread') );
pth = [ fullfile(p, 'images') path2cell(path)];
for p=pth
fname = fullfile(p{1}, filename);
if exist( fname ) > 0
m.fullfilename = fullfile(p{1}, filename);
m.movie = VideoReader(m.fullfilename);
break;
% p = fileparts( which('iread') );
% pth = [ fullfile(p, 'images') path2cell(path)];
% for p=pth
% fname = fullfile(p{1}, filename);
% if exist( fname ) > 0
% m.fullfilename = fullfile(p{1}, filename);
% m.movie = VideoReader(m.fullfilename);
% break;
% end
% end

if exist( filename, 'file' ) ~= 2
% file is not here, download it
if ~fileonserver(filename)
error('MVTB:badarg:nosuchfile', 'Can''t find file: %s', filename);
end
getfromserver(filename);
end

m.fullfilename = filename;
m.movie = VideoReader(m.fullfilename);

if isempty(m.movie)
error('MVTB:badarg:nosuchfile', 'Can''t find file: %s', filename);
end
Expand All @@ -102,11 +115,9 @@
m.height = m.movie.Height;
m.rate = m.movie.FrameRate;
m.totalDuration = m.movie.Duration;
m.nframes = m.movie.NumberOfFrames;

m.nframes = floor(m.totalDuration * m.rate);
end


function paramSet(m, varargin)
opt.skip = 1;

Expand Down Expand Up @@ -144,6 +155,7 @@ function close(m)
% Options::
% 'skip',S Skip frames, and return current+S frame
% 'frame',F Return frame F within the movie
% 'time',T Return frame at time T within the movie
%
% Notes::
% - If no output argument given the image is displayed using IDISP.
Expand All @@ -154,32 +166,24 @@ function close(m)
opt = tb_optparse(opt, varargin);


if isempty(opt.frame)
m.curFrame = m.curFrame + opt.skip;
else
m.curFrame = opt.frame;
if ~isempty(opt.frame)
m.movie.CurrentTime = m.curFrame + opt.skip;
end
if ~isempty(opt.time)
m.movie.CurrentTime = opt.time;
end
if isempty(opt.frame) & isempty(opt.time)
m.movie.CurrentTime = m.movie.CurrentTime + opt.skip / m.rate;
end

% read next frame from the file
if m.curFrame < m.nframes
try
data = read(m.movie, m.curFrame);
catch me
% it seems that the test above is not enough to prevent reading past
% the end of file...
if strcmp(me.identifier, 'MATLAB:audiovideo:VideoReader:invalidFrameVarFrameRate')
out = [];
return;
else
rethrow(me);
end
end
if m.hasframe
data = readFrame(m.movie);
else
out = [];
return;
end


if (numel(data) > 3*m.width*m.height)
warning('Movie: dimensions do not match data size. Got %d bytes for %d x %d', numel(data), m.width, m.height);
end
Expand All @@ -202,6 +206,14 @@ function close(m)
end
end

function skiptotime(m, t)
m.movie.CurrentTime = t;
end

function skiptoframe(m, n)
m.movie.CurrentTime = n / m.rate;
end

function s = char(m)
%Movie.char Convert to string
%
Expand All @@ -217,12 +229,62 @@ function close(m)
end


function c = path2cell(s)
remain = s;
c = {};
while true
[str, remain] = strtok(remain, ':');
if isempty(str), break; end
c = [c str];
% test if the file is on the server in root folder
function v = fileonserver(filename)
% list of all the files associated with the toolbox, test here rather than pester my server
filelist = [
"LeftBag.mpg"
"traffic_sequence.mpg"
];

v = ~isempty( intersect(filelist, filename) );
end

% works like loadimage
function getfromserver(filename)

% get data from server
fprintf('downloading from server...');
movie = webread( fullfile('http://petercorke.com/files/images', filename) );
fprintf('\n');

mvtb = fileparts( which('iread') );
mkdir_p(mvtb, fullfile('images', filename) );

filename = fullfile(mvtb, 'images', filename);

% save it to local file
fprintf('saving locally to %s\n', filename);
fp = fopen(filename, 'w');
fwrite(fp, movie);
fclose(fp);
end

% works like mkdir -p
% starting at base, it creates all the folders needed for filename
function mkdir_p(base, filename)

[pth,fname] = pathlist(filename);

dir = base;
for i=1:length(pth)
dir = fullfile(dir, pth{i});
if exist( dir ) ~= 7
% folder doesn't exist, create it
mkdir(dir);
end
end
end

% convert a file path into a cell array of path components and the filename (with
% extension)
function [pth,fname] = pathlist(filename)
[p,f,e] = fileparts(filename);
fname = [f e];

if isempty(p)
pth = {};
else
pth = strsplit(p, filesep);
end
end

0 comments on commit 06bff8b

Please sign in to comment.