Frame Navigation instead of loading the entire video into memory.

master
sigonasr2 1 year ago
parent 67cf8515a8
commit 8dde87d40b
  1. 2
      OpenCVVideoParser/OpenCVVideoParser.vcxproj
  2. 27
      OpenCVVideoParser/main.cpp

@ -97,6 +97,7 @@
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Downloads\opencv\build\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -134,6 +135,7 @@
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>C:\Users\sigon\Downloads\opencv\build\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

@ -19,13 +19,19 @@ void extract_frames(const std::string &videoFilePath,std::vector<cv::Mat>& frame
if(!cap.isOpened()) // check if we succeeded
cv::error(CV_StsError,"Can not open Video file",__FUNCTION__,"C:\\Users\\sigon\\source\\repos\\OpenCVVideoParser\\OpenCVVideoParser\\main.cpp",14);
/*
//cap.get(CV_CAP_PROP_FRAME_COUNT) contains the number of frames in the video;
for(int frameNum = 0; frameNum < cap.get(cv::CAP_PROP_FRAME_COUNT);frameNum++)
{
cv::Mat frame;
cap >> frame; // get the next frame from video
frames.push_back(frame);
}
}*/
cap.set(cv::CAP_PROP_POS_FRAMES,100);
cv::Mat frame;
cap >> frame; // get the next frame from video
frames.push_back(frame);
}
catch( cv::Exception& e ){
std::cerr << e.msg << std::endl;
@ -33,6 +39,12 @@ void extract_frames(const std::string &videoFilePath,std::vector<cv::Mat>& frame
}
}
cv::Mat get_frame(cv::VideoCapture&video,int frameNumb){
video.set(cv::CAP_PROP_POS_FRAMES,frameNumb);
cv::Mat frame;
video >> frame;
return frame;
}
/*
It saves a vector of frames into jpg images into the outputDir as 1.jpg,2.jpg etc where 1,2 etc represents the frame number
@ -69,16 +81,17 @@ public:
public:
std::vector<cv::Mat>frames;
float frameView=0;
cv::VideoCapture video; // open the video file
bool OnUserCreate() override
{
// Called once at the start, so create things here
extract_frames("C:/users/sigon/Videos/test.mp4",frames);
video.open("C:/users/sigon/Videos/test.mp4");
return true;
}
void UpdateScreen(){
Clear(BLACK);
cv::Mat&targetFrame=frames[int(frameView)];
cv::Mat targetFrame=get_frame(video,std::clamp(int(frameView),0,int(video.get(cv::CAP_PROP_FRAME_COUNT))));
for(int y=0;y<targetFrame.rows;y++){
for(int x=0;x<targetFrame.cols;x++){
cv::Vec3b col=targetFrame.at<cv::Vec3b>(cv::Point{x,y});
@ -93,10 +106,18 @@ public:
bool OnUserUpdate(float fElapsedTime) override
{
// Called once per frame, draws random coloured pixels
if(GetKey(RIGHT).bPressed){
frameView+=1;
UpdateScreen();
}
if(GetKey(RIGHT).bHeld){
frameView+=fElapsedTime*60;
UpdateScreen();
}
if(GetKey(LEFT).bPressed){
frameView-=1;
UpdateScreen();
}
if(GetKey(LEFT).bHeld){
frameView-=fElapsedTime*60;
UpdateScreen();

Loading…
Cancel
Save