clc
close all
%% Download the video from AOLME website: https://aolme.unm.edu/

gTruth.LabelDefinitions              % Provide the label definition table
gTruthInterval = gTruth.LabelData;   % Label data for each ROI and scene label
%gTruthInterval = gTruth.LabelData(timerange('00:02:30','00:02:38'),:);
video_source = gTruth.DataSource.Source;   % Obtain video path

%%
v = VideoReader(video_source);
framesPerSecond = get(v,'FrameRate'); % Frame rate
secPerFrame = 1/framesPerSecond;
allTime = gTruthInterval.Time;        % Time data

%% According to the label definition table, get position of the crop rectangle
recPosition = gTruthInterval.Student_1;  % Remember to change the label name here

new_v = VideoWriter('NewVideo1.mp4'); % Frame rate is 30 (default)
%new_v.FrameRate = 10;               % Change the frame rate
open(new_v);
height = 100;
width = 60;
%% Display rectangle and crop image
for i = 1:length(allTime)
   temTime = datenum(allTime(i))*3600*24;
   temRecData = cell2mat(recPosition(i));
   
   if isempty(temRecData)
       disp('No data');
   else       
       v.CurrentTime = temTime;   % set current time
       temFrame = readFrame(v);
       rec = imcrop(temFrame,temRecData);
       %ratio = round(temRecData(3)/temRecData(4),2);
       rec = imresize(rec, [height, width]); 
       figure(1), subplot(2,1,1), imshow(imresize(temFrame, 0.25)), hold on,
       rectangle('Position',temRecData/4,'EdgeColor', 'r', 'LineWidth', 1, 'LineStyle','-');
       subplot(2,1,2), imshow(rec);
       %figure(1), imshow(rec);
       %figure(2), imshow(imresize(temFrame, 0.25)), hold on,
       
       %temTypingData(3)/temTypingData(4)
       writeVideo(new_v, rec);
       
   end
       
end
close(new_v)