SOMHunter Core
task-target-helper.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlek <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <string>
22 // ---
23 #include "common-types.h"
24 #include "common.h"
25 #include "utils.hpp"
26 
27 namespace sh {
28 
33 public:
34  class Task {
35  public:
36  enum class Type { TEXT, VISUAL, AVS };
37 
38  Type type_to_enum(const std::string& s) {
39  if (s == "t" || s == "T")
40  return Type::TEXT;
41  else if (s == "v" || s == "V")
42  return Type::VISUAL;
43  else
44  return Type::AVS;
45  }
46 
47  public:
48  Task() = delete;
49  Task(const std::string& name, const std::string& type, std::size_t ts_from, std::size_t ts_to, VideoId video_ID,
50  FrameNum fr_from, FrameNum fr_to)
51  : _name(name),
52  _type(type_to_enum(type)),
53  _timestamps(ts_from, ts_to),
54  _video_ID(video_ID - 1), // To 0-based
55  _frames_interval(fr_from, fr_to) {}
56 
57  std::pair<std::size_t, std::size_t> timestamps() { return _timestamps; }
58 
59  VideoId video_ID() { return _video_ID; }
60 
61  FrameNum frame_from() { return _frames_interval.first; }
62 
63  FrameNum frame_to() { return _frames_interval.second; }
64 
65  private:
66  std::string _name;
68  std::pair<std::size_t, std::size_t> _timestamps;
69  VideoId _video_ID; // 0-based
70  std::pair<FrameNum, FrameNum> _frames_interval;
71  };
72 
73 public:
74  TaskTargetHelper() = delete;
75  TaskTargetHelper(const std::string& filepath) {
76  SHLOG_I("Parsing tasks from '" << filepath << "'...");
77 
78  auto ifs = std::ifstream(filepath, std::ios::in);
79  if (!ifs) {
80  std::string msg{ "Error opening file: " + filepath };
81  SHLOG_E(msg);
82  throw std::runtime_error(msg);
83  }
84 
85  // task_name,task_type,timestamp_from,timestamp_to,video_ID_starts_from_1,frame_from,frame_to
86  // 01_v21-1,V,1624277458765,1624277758765,4178,1875,2300
87 
88  // Read the file line by line until EOF
89  std::size_t i_line = 0;
90  for (std::string line_text_buffer; std::getline(ifs, line_text_buffer); ++i_line) {
91  // Skip the first line
92  if (i_line == 0) continue;
93 
94  std::stringstream line_buffer_ss(line_text_buffer);
95 
96  std::vector<std::string> tokens;
97 
98  // Tokenize this line with "," separator
99  for (std::string token; std::getline(line_buffer_ss, token, ',');) {
100  tokens.push_back(token);
101  }
102 
103  do_assert(tokens.size() == 7, "Exactly 7 cols in the CSV.");
104 
105  std::string& name = tokens[0];
106  std::string& type = tokens[1];
107  std::size_t ts_from = utils::str2<std::size_t>(tokens[2]);
108  std::size_t ts_to = utils::str2<std::size_t>(tokens[3]);
109  std::size_t video_ID = utils::str2<std::size_t>(tokens[4]);
110  std::size_t fr_from = utils::str2<std::size_t>(tokens[5]);
111  std::size_t fr_to = utils::str2<std::size_t>(tokens[6]);
112 
113  _tasks.emplace_back(name, type, ts_from, ts_to, video_ID, fr_from, fr_to);
114  }
115 
116  SHLOG_I("Tasks parsed.");
117  }
118 
119  // ---
120  std::tuple<VideoId, FrameNum, FrameNum> target(std::size_t ts) {
121  for (auto&& t : _tasks) {
122  auto [ts_fr, ts_to] = t.timestamps();
123  if (ts_fr <= ts && ts <= ts_to) {
124  return std::tuple(t.video_ID(), t.frame_from(), t.frame_to());
125  }
126  }
127 
128  std::string msg("No task found for timestamp '" + std::to_string(ts) + "'!");
129  SHLOG_E(msg);
130  throw std::runtime_error(msg);
131  }
132 
133 private:
134  std::vector<Task> _tasks;
135 };
136 
137 }; // namespace sh
Definition: task-target-helper.h:34
Type type_to_enum(const std::string &s)
Definition: task-target-helper.h:38
FrameNum frame_from()
Definition: task-target-helper.h:61
FrameNum frame_to()
Definition: task-target-helper.h:63
std::pair< FrameNum, FrameNum > _frames_interval
Definition: task-target-helper.h:70
Type _type
Definition: task-target-helper.h:67
std::string _name
Definition: task-target-helper.h:66
VideoId video_ID()
Definition: task-target-helper.h:59
std::pair< std::size_t, std::size_t > timestamps()
Definition: task-target-helper.h:57
Task(const std::string &name, const std::string &type, std::size_t ts_from, std::size_t ts_to, VideoId video_ID, FrameNum fr_from, FrameNum fr_to)
Definition: task-target-helper.h:49
Type
Definition: task-target-helper.h:36
std::pair< std::size_t, std::size_t > _timestamps
Definition: task-target-helper.h:68
VideoId _video_ID
Definition: task-target-helper.h:69
Represents a real competition task and for the given timestamp responds with the target.
Definition: task-target-helper.h:32
std::vector< Task > _tasks
Definition: task-target-helper.h:134
TaskTargetHelper(const std::string &filepath)
Definition: task-target-helper.h:75
std::tuple< VideoId, FrameNum, FrameNum > target(std::size_t ts)
Definition: task-target-helper.h:120
Definition: common-types.h:33
unsigned VideoId
Definition: common-types.h:71
unsigned FrameNum
Definition: common-types.h:72
#define SHLOG_E(x)
Definition: static-logger.hpp:157
#define do_assert(assertion, msg)
Assert execuded at all times.
Definition: static-logger.hpp:210
#define SHLOG_I(x)
Definition: static-logger.hpp:172