aboutsummaryrefslogtreecommitdiff
path: root/src/playback.cc
blob: 3203f47a54ddc6b98477e386f47122cfa27bcb52 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "playback.h"
#include "labeller.h"
#include <opencv2/opencv.hpp>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <thread>
#include <map>

namespace frontend {
    struct playback_impl {
        std::vector<cv::Mat> images;
        double fps;
        double reportedDuration;
        std::size_t frameNum = 0;
        std::map<std::string, cv::Scalar> names2colors;
    };

    playback::playback(const std::filesystem::path& video, std::size_t frameCap) {
        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(frameCap == 0 or data->images.size() < frameCap) {
            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;
    }

    void playback::display(const std::string& windowName, const std::vector<backend::label>& labels) const {
        auto curr = data->images[data->frameNum].clone();
        std::vector<cv::Scalar> colors = {cv::Scalar(255, 0, 0), cv::Scalar(0, 255, 0), cv::Scalar(0, 0, 255), cv::Scalar(255, 255, 0), cv::Scalar(255, 0, 255), cv::Scalar(0, 255, 255), cv::Scalar(255, 255, 255)};
        for(auto l : labels) {
            if(! data->names2colors.contains(l.name)) {
                data->names2colors.insert({l.name, colors[data->names2colors.size()%colors.size()]});
            }
            auto color = data->names2colors.at(l.name);
            auto r = l.location;
            if(r.nonzero()) {
                // Update with the rectangle
                cv::rectangle(curr, cv::Point(r.x1, r.y1), cv::Point(r.x2, r.y2), color, 2, cv::LINE_8);
            }
        }
        cv::imshow(windowName, curr);
    }

    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() / data->fps;
    }
    
    void playback::interFrameSleep() const {
        std::this_thread::sleep_for(std::chrono::milliseconds(int(1000/data->fps)));
    }

    std::size_t playback::getMaxFrame() const {
        return data->images.size() - 1;
    }

    double playback::getMaxTime() const {
        return getMaxFrame() / data->fps;
    }

    void playback::release() const {
    }
}