Skip to content

Commit 4c34800

Browse files
committed
run transnetv2
1 parent d5ad9ac commit 4c34800

10 files changed

Lines changed: 224 additions & 223 deletions

include/frame_timecode.h

Lines changed: 63 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,48 @@
99
constexpr int32_t HOUR_MAX = 10;
1010

1111
class Hour {
12-
public:
13-
explicit Hour(const int32_t hour) : hour_{hour} {}
14-
static std::optional<Hour> create_hour(const int32_t hour) {
15-
if (hour < 0 || hour >= HOUR_MAX) {
16-
std::cerr << "[Error] Hour should be 0 < x < 10, but got " << std::to_string(hour) << std::endl;
17-
return std::nullopt;
18-
}
19-
return Hour(hour);
12+
public:
13+
explicit Hour(const int32_t hour) : hour{hour} {}
14+
static std::optional<Hour> create_hour(const int32_t hour) {
15+
if (hour < 0 || hour >= HOUR_MAX) {
16+
std::cerr << "[Error] Hour should be 0 < x < 10, but got " << hour << std::endl;
17+
return std::nullopt;
2018
}
21-
const int32_t get_hour() const { return hour_; }
22-
23-
private:
24-
const int32_t hour_;
19+
return Hour(hour);
20+
}
21+
int32_t get_hour() const { return hour; }
22+
private:
23+
int32_t hour;
2524
};
2625

2726
class Minute {
28-
public:
29-
explicit Minute(const int32_t minute) : minute_{minute} {}
30-
static std::optional<Minute> create_minute(const int32_t minute) {
31-
if (minute < 0 || minute >= 60) {
32-
std::cerr << "[Error] Minute should be 0 < x < 60, but got " << std::to_string(minute) << std::endl;
33-
return std::nullopt;
34-
}
35-
return Minute(minute);
27+
public:
28+
explicit Minute(const int32_t minute) : minute{minute} {}
29+
static std::optional<Minute> create_minute(const int32_t minute) {
30+
if (minute < 0 || minute >= 60) {
31+
std::cerr << "[Error] Minute should be 0 < x < 60, but got " << minute << std::endl;
32+
return std::nullopt;
3633
}
37-
const int32_t get_minute() const { return minute_; }
38-
39-
private:
40-
const int32_t minute_;
34+
return Minute(minute);
35+
}
36+
int32_t get_minute() const { return minute; }
37+
private:
38+
int32_t minute;
4139
};
4240

4341
class Second {
44-
public:
45-
explicit Second(const float second) : second_{second} {}
46-
static std::optional<Second> create_second(const float second) {
47-
if (second < 0 || second >= 60) {
48-
std::cerr << "[Error] Second should be 0 < x < 60, but got " << std::to_string(second) << std::endl;
49-
return std::nullopt;
50-
}
51-
return Second(second);
42+
public:
43+
explicit Second(const float second) : second{second} {}
44+
static std::optional<Second> create_second(const float second) {
45+
if (second < 0 || second >= 60) {
46+
std::cerr << "[Error] Second should be 0 < x < 60, but got " << second << std::endl;
47+
return std::nullopt;
5248
}
53-
const float get_second() const { return second_; }
54-
55-
private:
56-
const float second_;
49+
return Second(second);
50+
}
51+
float get_second() const { return second; }
52+
private:
53+
float second;
5754
};
5855

5956
struct TimeStamp {
@@ -63,47 +60,39 @@ struct TimeStamp {
6360
};
6461

6562
class FrameTimeCode {
66-
public:
67-
explicit FrameTimeCode(const int32_t frame_num, const float fps);
68-
FrameTimeCode(const FrameTimeCode& timecode);
69-
70-
float get_framerate() const { return framerate_; }
71-
int32_t get_frame_num() const { return frame_num_; }
72-
73-
std::optional<int32_t> parse_timecode_string(const std::string& timecode_str) const;
74-
int32_t parse_timecode_number(const int32_t seconds) const;
75-
int32_t parse_timecode_number(const float seconds) const;
76-
std::string to_string() const;
77-
std::string to_string_second() const;
78-
79-
bool operator==(const FrameTimeCode& other) const;
80-
bool operator!=(const FrameTimeCode& other) const;
81-
bool operator<(const FrameTimeCode& other) const;
82-
bool operator>(const FrameTimeCode& other) const;
83-
bool operator<=(const FrameTimeCode& other) const;
84-
bool operator>=(const FrameTimeCode& other) const;
85-
86-
FrameTimeCode operator+(const FrameTimeCode& other) const;
87-
FrameTimeCode operator-(const FrameTimeCode& other) const;
88-
89-
static std::optional<FrameTimeCode> from_timecode_string(const std::string& timecode_str, const float fps);
90-
static std::optional<FrameTimeCode> from_frame_nums(const int32_t frame_num, const float fps);
91-
static std::optional<FrameTimeCode> from_seconds(const int32_t seconds, const float fps);
92-
static std::optional<FrameTimeCode> from_seconds(const float seconds, const float fps);
93-
94-
private:
95-
std::optional<TimeStamp> _parse_hrs_mins_secs_to_second(const std::string& timecode_str) const;
96-
float framerate_;
97-
int32_t frame_num_;
63+
public:
64+
explicit FrameTimeCode(const int32_t frame_num, const float fps);
65+
FrameTimeCode(const FrameTimeCode& timecode);
66+
float get_framerate() const { return framerate; }
67+
int32_t get_frame_num() const { return frame_num; }
68+
std::optional<int32_t> parse_timecode_string(const std::string& timecode_str) const;
69+
int32_t parse_timecode_number(const int32_t seconds) const;
70+
int32_t parse_timecode_number(const float seconds) const;
71+
std::string to_string() const;
72+
std::string to_string_second() const;
73+
bool operator==(const FrameTimeCode& other) const;
74+
bool operator!=(const FrameTimeCode& other) const;
75+
bool operator<(const FrameTimeCode& other) const;
76+
bool operator>(const FrameTimeCode& other) const;
77+
bool operator<=(const FrameTimeCode& other) const;
78+
bool operator>=(const FrameTimeCode& other) const;
79+
FrameTimeCode operator+(const FrameTimeCode& other) const;
80+
FrameTimeCode operator-(const FrameTimeCode& other) const;
81+
static std::optional<FrameTimeCode> from_timecode_string(const std::string& timecode_str, const float fps);
82+
static std::optional<FrameTimeCode> from_frame_nums(const int32_t frame_num, const float fps);
83+
static std::optional<FrameTimeCode> from_seconds(const int32_t seconds, const float fps);
84+
static std::optional<FrameTimeCode> from_seconds(const float seconds, const float fps);
85+
private:
86+
std::optional<TimeStamp> parse_hrs_mins_secs(const std::string& timecode_str) const;
87+
float framerate;
88+
int32_t frame_num;
9889
};
9990

10091
namespace frame_timecode {
101-
102-
constexpr float MIN_FPS_DELTA = 1.0 / 100000;
103-
constexpr float _SECONDS_PER_MINUTE = 60.0;
104-
constexpr float _SECONDS_PER_HOUR = 60.0 * _SECONDS_PER_MINUTE;
105-
constexpr float _MINUTES_PER_HOUR = 60.0;
106-
92+
constexpr float MIN_FPS_DELTA = 1.0f / 100000;
93+
constexpr float SECONDS_PER_MINUTE = 60.0f;
94+
constexpr float SECONDS_PER_HOUR = 60.0f * SECONDS_PER_MINUTE;
95+
constexpr float MINUTES_PER_HOUR = 60.0f;
10796
}
10897

10998
float calculate_total_seconds(const TimeStamp& timestamp);

include/frame_timecode_pair.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#include <tuple>
55

66
class FrameTimeCode;
7-
using FrameTimeCodePair = std::tuple<FrameTimeCode, FrameTimeCode>;
7+
using FrameTimeCodePair = std::pair<FrameTimeCode, FrameTimeCode>;
88

9-
#endif
9+
#endif

include/scene_manager.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,35 @@
55
#include "video_frame.h"
66
#include "frame_timecode.h"
77
#include "frame_timecode_pair.h"
8-
98
#include <opencv2/opencv.hpp>
109
#include <vector>
1110
#include <cstdint>
1211
#include <optional>
13-
#include <memory>
1412

1513
class VideoStream;
1614
template <typename T> class BlockingQueue;
15+
constexpr int32_t TRANSNET_WINDOW = 100;
1716

1817
class SceneManager {
19-
public:
20-
explicit SceneManager(TransNetV2& detector);
21-
void detect_scenes(VideoStream& video);
22-
std::optional<std::vector<FrameTimeCodePair>> get_scene_list() const;
18+
public:
19+
explicit SceneManager(TransNetV2& detector);
20+
void detect_scenes(VideoStream& video);
21+
std::optional<std::vector<FrameTimeCodePair>> get_scene_list() const;
22+
23+
private:
24+
void process_frame(VideoFrame& next_frame);
25+
void flush_buffer();
26+
void decode_thread(VideoStream& video, BlockingQueue<VideoFrame>& frame_queue);
27+
std::vector<FrameTimeCode> cutting_list_as_timecodes() const;
2328

24-
private:
25-
void _process_frame(VideoFrame& next_frame);
26-
void _decode_thread(VideoStream& video, BlockingQueue<VideoFrame>& frame_queue);
27-
std::vector<FrameTimeCode> _get_cutting_list() const;
29+
TransNetV2& detector;
30+
float framerate = 0.0f;
31+
std::optional<FrameTimeCode> start = std::nullopt;
32+
std::optional<FrameTimeCode> end = std::nullopt;
33+
std::vector<int32_t> cutting_list;
2834

29-
cv::Mat previous_frame_;
30-
std::vector<int32_t> cutting_list_;
31-
TransNetV2& detector_;
32-
float framerate_ = 0.0f;
33-
std::optional<FrameTimeCode> start_ = std::nullopt;
34-
std::optional<FrameTimeCode> end_ = std::nullopt;
35+
std::vector<uint8_t> frame_buffer;
36+
std::vector<uint32_t> frame_num_buffer;
3537
};
3638

3739
#endif

include/transnetv2.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TransNetV2 {
1010
std::vector<float> predict(const std::vector<uint8_t>& frames, int T);
1111

1212
private:
13-
Ort::Env env_;
14-
Ort::Session session_;
15-
Ort::MemoryInfo memory_info_;
13+
Ort::Env env;
14+
Ort::Session session;
15+
Ort::MemoryInfo memory_info;
1616
};

include/video_stream.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@
22
#define VIDEO_STREAM_H
33

44
#include "frame_timecode.h"
5-
65
#include <opencv2/opencv.hpp>
76
#include <string>
87
#include <filesystem>
98
#include <optional>
109

1110
class VideoStream {
12-
public:
13-
explicit VideoStream(const std::string& input_path, const float framerate, cv::VideoCapture& cap);
14-
FrameTimeCode position() const;
15-
bool is_end_frame() const;
16-
17-
int32_t width() const;
18-
int32_t height() const;
19-
bool set_time(const std::optional<std::string>& start, const std::optional<std::string>& end,
20-
const std::optional<std::string>& duration);
21-
cv::VideoCapture& get_cap() { return cap_; }
22-
float get_framerate() const { return framerate_; }
23-
const FrameTimeCode& get_start() const { return start_; }
24-
const FrameTimeCode& get_end() const { return end_; }
25-
bool seek(const int32_t frame_num);
26-
static std::optional<VideoStream> initialize_video_stream(const std::filesystem::path& input_path);
11+
public:
12+
explicit VideoStream(const std::string& input_path, const float framerate, cv::VideoCapture& cap);
13+
FrameTimeCode position() const;
14+
bool is_end_frame() const;
15+
int32_t width() const;
16+
int32_t height() const;
17+
bool set_time(const std::optional<std::string>& start, const std::optional<std::string>& end,
18+
const std::optional<std::string>& duration);
19+
cv::VideoCapture& get_cap() { return cap; }
20+
float get_framerate() const { return framerate; }
21+
const FrameTimeCode& get_start() const { return start; }
22+
const FrameTimeCode& get_end() const { return end; }
23+
bool seek(const int32_t frame_num);
24+
static std::optional<VideoStream> initialize_video_stream(const std::filesystem::path& input_path);
2725

28-
private:
29-
const std::string input_path_;
30-
const float framerate_;
31-
cv::VideoCapture cap_;
32-
FrameTimeCode start_;
33-
FrameTimeCode end_;
26+
private:
27+
const std::string input_path;
28+
const float framerate;
29+
cv::VideoCapture cap;
30+
FrameTimeCode start;
31+
FrameTimeCode end;
3432
};
3533

3634
#endif

0 commit comments

Comments
 (0)