So I have been working on testing some SLAM methods in specific ORB-SLAM2 and in order to make it run on a webcam one needs to pass among the frame in open CV a timestamp for the frame. In theory it should be dead easy, just call on the CV::VideoCapture class the Get method with the “CV_CAP_PROP_POS_MSEC” property according to the OpenCV documentation. But turns out this is half implemented so for live streams it just doesnt work. According to this it turns out there is a way to patch the OpenCV Library and implement this, but I was just needing to get a timestamp really quick in order to feed to my SLAM method. So what I did was this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <opencv2/core/core.hpp> #include <opencv2/opencv.hpp> //somewhere wherever you query your frame cv::VideoCapture cap; if(!cap.open(0)) { cout << "Couldnt open camera stream. ending." << endl; return 0; } for(;;) { cv::Mat frame; //count the time right now #ifdef COMPILEDWITHC11 std::chrono::steady_clock::time_point tf1 = std::chrono::steady_clock::now(); #else std::chrono::monotonic_clock::time_point tf1 = std::chrono::monotonic_clock::now(); #endif //query the frame cap >> frame; //count the time after querying #ifdef COMPILEDWITHC11 std::chrono::steady_clock::time_point tf2 = std::chrono::steady_clock::now(); #else std::chrono::monotonic_clock::time_point tf2 = std::chrono::monotonic_clock::now(); #endif //substract both times and there you go, your quick and dirty timestamp ;-) double timestamp = std::chrono::duration_cast<std::chrono::duration<double> >(tf2 - tf1).count(); cout << "Timestamp: " << timestamp << endl; } |
I took this piece of code from the same ORB-SLAM2 repo and accommodated to count the time to query a frame from the webcam. Its a quick and dirty trick but does its job 🙂