diff options
author | Your Name <you@example.com> | 2021-05-13 17:20:26 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-13 17:20:26 -0400 |
commit | 5a6248518654ec97d95d2c463e3ffb4be7bbf456 (patch) | |
tree | a6ebb6bb26ba2a6b4abc3829a81c58ae97fd2e05 /src/playback.cc | |
download | annotator-5a6248518654ec97d95d2c463e3ffb4be7bbf456.tar.gz annotator-5a6248518654ec97d95d2c463e3ffb4be7bbf456.tar.bz2 annotator-5a6248518654ec97d95d2c463e3ffb4be7bbf456.zip |
Initial commit
Diffstat (limited to 'src/playback.cc')
-rw-r--r-- | src/playback.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/playback.cc b/src/playback.cc new file mode 100644 index 0000000..045abb6 --- /dev/null +++ b/src/playback.cc @@ -0,0 +1,69 @@ +#include "playback.h" +#include <opencv2/opencv.hpp> +#include <vector> +#include <stdexcept> +#include <iostream> +#include <thread> + +namespace frontend { + struct playback_impl { + std::vector<cv::Mat> images; + int fps; + double reportedDuration; + std::size_t frameNum = 0; + }; + + playback::playback(const std::filesystem::path& video) { + data = std::shared_ptr<playback_impl>(new playback_impl); + cv::VideoCapture cap(video); + if(!cap.isOpened()) { + throw std::runtime_error("Error loading video " + video.string()); + } + data->fps = cap.get(cv::CAP_PROP_FPS); + while(true) { + cv::Mat frame; + cap >> frame; + if(frame.empty()) break; + data->images.push_back(frame); + } + data->reportedDuration = cap.get(cv::CAP_PROP_POS_MSEC) / 1000.0; + std::cout << "Reported duration: " << data->reportedDuration << " seconds" << std::endl; + cap.release(); + } + + void playback::display(const std::string& windowName) const { + cv::imshow(windowName, data->images[data->frameNum]); + } + + bool playback::seekFrame(std::size_t frameNum) { + if(frameNum >= data->images.size()) { + return false; + } + data->frameNum = frameNum; + return true; + } + + std::size_t playback::getFrame() const { + return data->frameNum; + } + + bool playback::seekTime(double time) { + return seekFrame(std::size_t(time * data->fps)); + } + + double playback::getTime() const { + return getFrame() / (double) data->fps; + } + + void playback::interFrameSleep() const { + std::this_thread::sleep_for(std::chrono::milliseconds(1000/data->fps)); + } + + std::size_t playback::getMaxFrame() const { + return data->images.size() - 1; + } + + double playback::getMaxTime() const { + return getMaxFrame() / (double) data->fps; + } +} |