SOMHunter Core
dataset-frames.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  * Vit Skrhak <v.skrhak@gmail.com>
7  *
8  * SOMHunter is free software: you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License as published by the Free
10  * Software Foundation, either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef DATASET_FRAMES_H_
23 #define DATASET_FRAMES_H_
24 
25 #include <cassert>
26 #include <cstring>
27 #include <string>
28 #include <vector>
29 // ---
30 #include <nlohmann/json.hpp>
31 // ---
32 #include "common.h"
33 #include "utils.hpp"
34 
35 namespace sh {
36 struct VideoFrame;
37 
39 
40 struct VideoFrame {
41  VideoFrame() = default;
43  uint8_t weekday = 0 /* Default is Monday. */, uint8_t hour = 0 /* Default is midnight. */,
44  uint16_t year = 2021 /* Default is 2021. */)
45  : filename{ std::move(filename) },
46  video_ID{ video_ID },
47  shot_ID{ shot_ID },
49  frame_ID{ image_ID },
50  LSC_id{ "" },
51  weekday{ weekday },
52  hour{ hour },
53  year{ year } {}
54 
55  nlohmann::json to_JSON() const {
56  // clang-format off
57  return nlohmann::json{
58  { "videoId", video_ID },
59  { "shotId", shot_ID },
60  { "frameNumber", frame_number },
61  { "frameId", frame_ID },
62  { "filename", filename }
63  };
64  // clang-format on
65  }
66 
67  std::string filename;
72 
73  // *** LSC medadata ***
75  std::string LSC_id;
76 
79 
82 
85 };
86 
90 struct FrameRange {
91  std::vector<VideoFrame>::iterator _begin;
92  std::vector<VideoFrame>::iterator _end;
93 
94  FrameRange() = default;
95  FrameRange(std::vector<VideoFrame>::iterator b, std::vector<VideoFrame>::iterator e) : _begin(b), _end(e) {}
96 
97  size_t size() const { return _end - _begin; }
102  const VideoFrame& operator[](size_t idx) const {
103  // Iterator is random access so this is fine
104  return *(_begin + idx);
105  }
106  VideoFrame& operator[](size_t idx) {
107  // Iterator is random access so this is fine
108  return *(_begin + idx);
109  }
110 
111  std::vector<VideoFrame>::const_iterator begin() const { return _begin; }
112  std::vector<VideoFrame>::iterator begin() { return _begin; }
113 
114  std::vector<VideoFrame>::const_iterator end() const { return _end; }
115  std::vector<VideoFrame>::iterator end() { return _end; }
116 };
117 
122  std::vector<VideoFramePointer>::const_iterator _begin;
123  std::vector<VideoFramePointer>::const_iterator _end;
124  bool _valid{ false };
125 
126 public:
127  FramePointerRange() = default;
128  FramePointerRange(std::vector<VideoFramePointer>::const_iterator b,
129  std::vector<VideoFramePointer>::const_iterator e)
130  : _begin(b), _end(e), _valid(true) {}
131  FramePointerRange(const std::vector<VideoFramePointer>& v) : _begin(v.cbegin()), _end(v.cend()), _valid(true) {}
132 
133  void print_display() {
134  for (auto iter = begin(); iter != end(); iter++) std::cout << (*iter)->frame_ID << std::endl;
135  }
136 
141  bool valid() const { return _valid; }
142 
143  size_t size() const { return _end - _begin; }
148  const VideoFramePointer& operator[](size_t idx) const {
149  // Iterator is random access so this is fine
150  return *(_begin + idx);
151  }
152  const VideoFramePointer& operator[](size_t idx) {
153  // Iterator is random access so this is fine
154  return *(_begin + idx);
155  }
156 
157  std::vector<VideoFramePointer>::const_iterator begin() const { return _begin; }
158 
159  std::vector<VideoFramePointer>::const_iterator end() const { return _end; }
160 };
161 
164  std::vector<FrameRange> _video_ID_to_frame_range;
165  std::vector<VideoFrame> _frames;
166 
167  std::string frames_dir;
168  std::string thumbs_dir;
170 
171 public:
172  DatasetFrames(const Settings& config);
173 
174  std::string operator[](FrameId i) const { return frames_dir + std::string{ _frames.at(i).filename }; }
175 
176  std::vector<VideoFrame>::iterator end() { return _frames.end(); };
177  std::vector<VideoFrame>::iterator begin() { return _frames.begin(); };
178 
179  std::vector<VideoFrame>::const_iterator end() const { return _frames.end(); };
180  std::vector<VideoFrame>::const_iterator begin() const { return _frames.begin(); };
181 
182  size_t get_num_videos() const { return _frames.back().video_ID + 1; }
183 
184  VideoFrame& get_frame(FrameId i) { return _frames[i]; }
185 
186  const VideoFrame& get_frame(FrameId i) const { return _frames[i]; }
187 
188  VideoFrame* get_frame_ptr(FrameId i) { return _frames.data() + i; }
189 
190  const VideoFrame* get_frame_ptr(FrameId i) const { return _frames.data() + i; }
191 
192  std::vector<VideoFrame>::const_iterator get_frame_it(FrameId i) const { return _frames.begin() + i; }
193 
194  size_t size() const { return _frames.size(); }
195 
196  VideoId get_video_id(FrameId img_ID) const {
197  if (img_ID >= _frames.size()) {
198  return VIDEO_ID_ERR_VAL;
199  } else {
200  return get_frame(img_ID).video_ID;
201  }
202  }
203 
208  FrameRange get_all_video_frames(VideoId video_ID) const { return _video_ID_to_frame_range[video_ID]; }
209 
214  FrameRange get_shot_frames(VideoId video_ID, size_t frame_num_from, size_t frame_num_to) const {
215  // Get video range
216  auto video_range = _video_ID_to_frame_range[video_ID];
217 
218  auto from_it = video_range.begin();
219  auto to_it = video_range.end();
220 
221  --to_it;
222 
223  // Rewind to the first wanted frame
224  while (from_it->frame_number < frame_num_from) {
225  ++from_it;
226  }
227 
228  // Rewind to the last wanted frame
229  while (to_it->frame_number > frame_num_to) {
230  --to_it;
231  }
232 
233  // Return one frame behind to behave begin/end-like
234  return FrameRange(from_it, to_it + 1);
235  }
236 
238  std::vector<VideoFramePointer> ids_to_video_frame(const std::vector<FrameId>& ids) const;
239  static std::vector<VideoFramePointer> range_to_video_frame(const FrameRange& ids);
240 
241 private:
248  static std::vector<std::vector<KeywordId>> parse_top_kws_for_imgs_text_file(const std::string& filepath);
249 
254  VideoFrame parse_video_filename(std::string&& filename);
255 
259  FiltersData parse_metadata_line(const std::string& line);
260 };
261 
262 }; // namespace sh
263 
264 #endif // DATASET_FRAMES_H_
Definition: dataset-frames.h:162
std::vector< VideoFrame >::const_iterator get_frame_it(FrameId i) const
Definition: dataset-frames.h:192
FrameRange get_all_video_frames(VideoId video_ID) const
Return copy of FrameRange representing all selected frames from the given video.
Definition: dataset-frames.h:208
static std::vector< VideoFramePointer > range_to_video_frame(const FrameRange &ids)
Definition: dataset-frames.cpp:233
std::vector< VideoFrame >::iterator end()
Definition: dataset-frames.h:176
DatasetFrames(const Settings &config)
Definition: dataset-frames.cpp:76
std::string operator[](FrameId i) const
Definition: dataset-frames.h:174
VideoId get_video_id(FrameId img_ID) const
Definition: dataset-frames.h:196
std::string thumbs_dir
Definition: dataset-frames.h:168
const VideoFrame & get_frame(FrameId i) const
Definition: dataset-frames.h:186
size_t get_num_videos() const
Definition: dataset-frames.h:182
std::vector< VideoFrame >::iterator begin()
Definition: dataset-frames.h:177
VideoFrame parse_video_filename(std::string &&filename)
From filename string it parses useful info as video/shot/frame ID etc.
Definition: dataset-frames.cpp:185
std::string frames_dir
Definition: dataset-frames.h:167
size_t size() const
Definition: dataset-frames.h:194
std::vector< VideoFrame >::const_iterator end() const
Definition: dataset-frames.h:179
std::vector< VideoFramePointer > ids_to_video_frame(const std::vector< FrameId > &ids) const
Translation to VideoFrameRefs from vector ids or FrameRange.
Definition: dataset-frames.cpp:218
VideoFrame * get_frame_ptr(FrameId i)
Definition: dataset-frames.h:188
DatasetsSettings::VideoFilenameOffsets offs
Definition: dataset-frames.h:169
std::vector< VideoFrame >::const_iterator begin() const
Definition: dataset-frames.h:180
static std::vector< std::vector< KeywordId > > parse_top_kws_for_imgs_text_file(const std::string &filepath)
Parses a text file with lists of top keywords for given image ID.
Definition: dataset-frames.cpp:31
std::vector< VideoFrame > _frames
Definition: dataset-frames.h:165
std::vector< FrameRange > _video_ID_to_frame_range
Map from video ID to range of image IDs.
Definition: dataset-frames.h:164
const VideoFrame * get_frame_ptr(FrameId i) const
Definition: dataset-frames.h:190
FrameRange get_shot_frames(VideoId video_ID, size_t frame_num_from, size_t frame_num_to) const
Returns new instance of FrameRange representing all frames from prvided video ID in interval [frame_n...
Definition: dataset-frames.h:214
FiltersData parse_metadata_line(const std::string &line)
Parses the desired metadata from the metadata line.
Definition: dataset-frames.cpp:199
VideoFrame & get_frame(FrameId i)
Definition: dataset-frames.h:184
Represents CONTINOUS range of const frame pointers.
Definition: dataset-frames.h:121
std::vector< VideoFramePointer >::const_iterator end() const
Definition: dataset-frames.h:159
const VideoFramePointer & operator[](size_t idx)
Definition: dataset-frames.h:152
std::vector< VideoFramePointer >::const_iterator _begin
Definition: dataset-frames.h:122
std::vector< VideoFramePointer >::const_iterator _end
Definition: dataset-frames.h:123
FramePointerRange(std::vector< VideoFramePointer >::const_iterator b, std::vector< VideoFramePointer >::const_iterator e)
Definition: dataset-frames.h:128
size_t size() const
Definition: dataset-frames.h:143
const VideoFramePointer & operator[](size_t idx) const
Returns VideoFramePointer reference to the frame with given index in this frame range.
Definition: dataset-frames.h:148
std::vector< VideoFramePointer >::const_iterator begin() const
Definition: dataset-frames.h:157
bool _valid
Definition: dataset-frames.h:124
FramePointerRange(const std::vector< VideoFramePointer > &v)
Definition: dataset-frames.h:131
bool valid() const
Returns true iff FramePointerRange contains valid range at the time.
Definition: dataset-frames.h:141
FramePointerRange()=default
void print_display()
Definition: dataset-frames.h:133
#define VIDEO_ID_ERR_VAL
Definition: common-types.h:94
Definition: common-types.h:33
unsigned VideoId
Definition: common-types.h:71
uint8_t Hour
Definition: common-types.h:243
uint16_t Year
Definition: common-types.h:244
uint8_t Weekday
Definition: common-types.h:242
unsigned ShotId
Definition: common-types.h:73
unsigned long FrameId
Definition: common-types.h:75
Definition: common-types.h:247
Represents CONTINOUS range of frames.
Definition: dataset-frames.h:90
std::vector< VideoFrame >::const_iterator begin() const
Definition: dataset-frames.h:111
std::vector< VideoFrame >::iterator _begin
Definition: dataset-frames.h:91
std::vector< VideoFrame >::iterator begin()
Definition: dataset-frames.h:112
FrameRange()=default
std::vector< VideoFrame >::const_iterator end() const
Definition: dataset-frames.h:114
std::vector< VideoFrame >::iterator end()
Definition: dataset-frames.h:115
FrameRange(std::vector< VideoFrame >::iterator b, std::vector< VideoFrame >::iterator e)
Definition: dataset-frames.h:95
std::vector< VideoFrame >::iterator _end
Definition: dataset-frames.h:92
size_t size() const
Definition: dataset-frames.h:97
const VideoFrame & operator[](size_t idx) const
Returns VideoFrame reference to the frame with given index in this frame range.
Definition: dataset-frames.h:102
VideoFrame & operator[](size_t idx)
Definition: dataset-frames.h:106
Parsed current config of the core.
Definition: settings.h:190
Definition: dataset-frames.h:40
FrameId frame_ID
Definition: dataset-frames.h:71
Weekday weekday
Day in a week (mon -> 0, ..., sun -> 7).
Definition: dataset-frames.h:78
ShotId shot_ID
Definition: dataset-frames.h:69
std::string filename
Definition: dataset-frames.h:67
FrameId frame_number
Definition: dataset-frames.h:70
Hour hour
In interval [0, 23].
Definition: dataset-frames.h:81
nlohmann::json to_JSON() const
Definition: dataset-frames.h:55
VideoFrame()=default
VideoFrame(std::string &&filename, VideoId video_ID, ShotId shot_ID, FrameId frame_number, FrameId image_ID, uint8_t weekday=0, uint8_t hour=0, uint16_t year=2021)
Definition: dataset-frames.h:42
VideoId video_ID
Definition: dataset-frames.h:68
std::string LSC_id
Original filename without the suffix.
Definition: dataset-frames.h:75
Year year
Year interval.
Definition: dataset-frames.h:84