SOMHunter Core
somhunter.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  *
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 #ifndef SOMHUNTER_H_
22 #define SOMHUNTER_H_
23 
24 #include <cstddef>
25 #include <string>
26 #include <vector>
27 // ---
28 #include "async-som.h"
29 #include "canvas-query-ranker.h"
30 #include "common.h"
31 #include "dataset-features.h"
32 #include "dataset-frames.h"
33 #include "image-processor.h"
34 #include "keyword-clip-ranker.h"
35 #include "keyword-ranker.h"
36 #include "logger.h"
37 #include "query-types.h"
38 #include "relocation-ranker.h"
39 #include "scores.h"
40 #include "search-context.h"
41 #include "task-target-helper.h"
42 #include "user-context.h"
43 #include "utils.hpp"
44 
45 namespace sh {
46 
54 class Somhunter {
55 public:
56  Somhunter() = delete;
58  Somhunter(const std::string& config_filepath);
59 
60  // ---
61 
67  GetDisplayResult get_display(DisplayType d_type, FrameId selected_image = 0, PageId page = 0, bool log_it = true);
68 
70  std::vector<bool> like_frames(const std::vector<FrameId>& new_likes);
71 
73  std::vector<bool> bookmark_frames(const std::vector<FrameId>& new_bookmarks);
74 
76  std::vector<const Keyword*> autocomplete_keywords(const std::string& prefix, size_t count = 5) const;
77 
83  RescoreResult rescore(Query& query, bool benchmark_run = false);
84 
88  const UserContext& switch_search_context(size_t index, size_t src_search_ctx_ID = SIZE_T_ERR_VAL,
89  const std::string& screenshot_fpth = "", const std::string& label = "");
90 
94  const SearchContext& get_search_context() const;
95 
99  const UserContext& get_user_context() const;
100 
101  const VideoFrame& get_frame(FrameId ID) const;
102 
103  FrameRange get_frames(VideoId video_ID, FrameNum fr, FrameNum to) const;
104 
106 
108  bool som_ready() const;
109 
110  bool som_ready(size_t temp_id) const;
111 
113  void reset_search_session();
114 
115  // ********************************
116  // Remote server related calls
117  // ********************************
118 
122  bool login_to_eval_server();
123 
128 
131 
132  // ********************************
133  // Logging calls
134  // ********************************
135 
136  /*
137  * Log events that need to be triggered from the outside (e.g. the UI).
138  */
139  void log_video_replay(FrameId frame_ID, float delta_X);
140  void log_scroll(DisplayType t, float delta_Y);
141  void log_text_query_change(const std::string& text_query);
143 
144  // ********************************
145  // Image manipulation utilites
146  // ********************************
147 
148  std::string store_rescore_screenshot(const std::string& filepath);
149 
150  size_t get_num_frames() const { return _dataset_frames.size(); }
151  const std::vector<FrameId>& get_top_scored(size_t max_count = 0, size_t from_video = 0, size_t from_shot = 0) const;
152  std::vector<VideoFramePointer> get_top_scored_frames(size_t max_count = 0, size_t from_video = 0,
153  size_t from_shot = 0) const;
154  std::vector<float> get_top_scored_scores(std::vector<FrameId>& top_scored_frames) const;
155  size_t find_targets(const std::vector<FrameId>& top_scored, const std::vector<FrameId>& targets) const;
156 
157  // ********************************
158  // Other
159  // ********************************
160  const Settings& settings() const { return _settings; }
161  const std::string& get_config_filepath() { return _core_settings_filepath; }
162  const std::string& get_API_config_filepath() { return _API_settings_filepath; }
164 
165  void benchmark_native_text_queries(const std::string& queries_filepath, const std::string& out_dir);
166  void benchmark_canvas_queries(const std::string& queries_dir, const std::string& out_dir);
167  void benchmark_real_queries(const std::string& queries_dir, const std::string& targets_fpth,
168  const std::string& out_dir);
169 
176 
177  static void write_resultset(const std::string& file, const std::vector<VideoFramePointer>& results);
178  static void write_query(const std::string& file, const Query& q);
179  static void write_query_info(const std::string& file, const std::string& ID, const std::string& user,
180  const std::tuple<VideoId, FrameId, FrameId>& target, std::size_t pos_vid,
181  std::size_t pos_fr, std::size_t unpos_vid, std::size_t unpos_fr);
182 
183  void run_basic_test();
184  void run_generators();
185 
186 private:
187  void apply_filters();
188 
192  void generate_new_targets();
193 
197  template <typename SpecificKWRanker, typename SpecificFrameFeatures>
198  void rescore_keywords(SpecificKWRanker& kw_ranker, const TextualQuery& query, size_t temporal,
199  const SpecificFrameFeatures& features);
200 
205  void rescore_feedback();
206 
210  void som_start(size_t temporal);
211 
213 
215 
217 
219 
221 
222  FramePointerRange get_video_detail_display(FrameId selected_image, bool log_it = true);
223 
224  FramePointerRange get_topKNN_display(FrameId selected_image, PageId page);
225 
226  // Gets only part of last display
228 
229  void reset_scores(float val = 1.0F);
230 
235  void push_search_ctx();
236 
240  bool has_metadata() const;
241 
243  friend sh::tests::TESTER_Somhunter;
244 
245  // ---
248 
249  // ********************************
250  // Loaded dataset
251  // (shared for all the users)
252  // ********************************
255 
256  // ********************************
257  // User contexts
258  // (private for each unique user session)
259  // ********************************
261 
262  // ********************************
263  // Services
264  // (shared for all the users)
265  // ********************************
266  const std::string _core_settings_filepath;
267  const std::string _API_settings_filepath;
268 
273 };
274 
275 // ---
276 
277 template <typename SpecificKWRanker, typename SpecificFrameFeatures>
278 void Somhunter::rescore_keywords(SpecificKWRanker& kw_ranker, const TextualQuery& query, size_t temporal,
279  const SpecificFrameFeatures& features) {
280  kw_ranker.rank_sentence_query(query, _user_context.ctx.scores, features, temporal);
281 
283 }
284 
285 }; // namespace sh
286 #endif // SOMHUNTER_H_
Definition: canvas-query-ranker.h:95
Represents all available feature sets.
Definition: dataset-features.h:81
Definition: dataset-frames.h:162
size_t size() const
Definition: dataset-frames.h:194
Represents CONTINOUS range of const frame pointers.
Definition: dataset-frames.h:121
Definition: keyword-clip-ranker.h:38
Definition: keyword-ranker.h:52
Definition: relocation-ranker.h:42
The main C++ API of the SOMHunter Core.
Definition: somhunter.h:54
size_t get_num_frames() const
Definition: somhunter.h:150
void benchmark_canvas_queries(const std::string &queries_dir, const std::string &out_dir)
Definition: somhunter.cpp:813
void run_basic_test()
Definition: somhunter.cpp:272
void benchmark_real_queries(const std::string &queries_dir, const std::string &targets_fpth, const std::string &out_dir)
Definition: somhunter.cpp:998
std::vector< bool > like_frames(const std::vector< FrameId > &new_likes)
Inverts the like states of the provided frames and returns the new states.
Definition: somhunter.cpp:130
void rescore_feedback()
Applies the relevance feedback from the user based on images the user already saw (as implicit negati...
Definition: somhunter.cpp:1354
std::vector< const Keyword * > autocomplete_keywords(const std::string &prefix, size_t count=5) const
Returns the nearest supported keyword matches to the provided prefix.
Definition: somhunter.cpp:193
FramePointerRange get_video_detail_display(FrameId selected_image, bool log_it=true)
Definition: somhunter.cpp:1481
void generate_example_images_for_keywords()
Generates the top example images from the database for all supported W2VV keywords.
Definition: somhunter.cpp:1217
FramePointerRange get_som_relocation_display(size_t temp_id)
Definition: somhunter.cpp:1457
static void write_query(const std::string &file, const Query &q)
Definition: somhunter.cpp:1292
void som_start(size_t temporal)
Gives the SOM worker the new work.
Definition: somhunter.cpp:1368
void log_canvas_query_change()
Definition: somhunter.cpp:659
void log_scroll(DisplayType t, float delta_Y)
Definition: somhunter.cpp:653
Somhunter()=delete
SubmitResult submit_to_eval_server(FrameId frame_id)
Sumbits frame with given id to VBS server.
Definition: somhunter.cpp:612
bool logout_from_eval_server()
Tries to logout from the remote evaluation server (competition one).
Definition: somhunter.cpp:610
FramePointerRange get_page_from_last(PageId page)
Definition: somhunter.cpp:1536
const DatasetFrames _dataset_frames
Definition: somhunter.h:253
FramePointerRange get_som_display()
Definition: somhunter.cpp:1435
void generate_new_targets()
Generates the new debug targets.
Definition: somhunter.cpp:1322
FrameRange get_frames(VideoId video_ID, FrameNum fr, FrameNum to) const
Definition: somhunter.cpp:1608
void reset_search_session()
Resets current search context and starts new search.
Definition: somhunter.cpp:627
void reset_scores(float val=1.0F)
Definition: somhunter.cpp:1556
void log_text_query_change(const std::string &text_query)
Definition: somhunter.cpp:655
const VideoFrame & get_frame(FrameId ID) const
Definition: somhunter.cpp:1606
bool has_metadata() const
Returns true if LSC metadata file provided inside the config.
Definition: somhunter.cpp:212
VideoFramePointer get_frame_ptr(FrameId img) const
Definition: somhunter.cpp:1612
FramePointerRange get_topKNN_display(FrameId selected_image, PageId page)
Definition: somhunter.cpp:1507
static void write_query_info(const std::string &file, const std::string &ID, const std::string &user, const std::tuple< VideoId, FrameId, FrameId > &target, std::size_t pos_vid, std::size_t pos_fr, std::size_t unpos_vid, std::size_t unpos_fr)
Definition: somhunter.cpp:1301
RescoreResult rescore(Query &query, bool benchmark_run=false)
Applies all algorithms for score computation and updates context.
Definition: somhunter.cpp:399
bool som_ready() const
Returns true if the user's SOM is ready.
Definition: somhunter.cpp:605
const Settings _settings
Current application settings.
Definition: somhunter.h:247
const UserContext & switch_search_context(size_t index, size_t src_search_ctx_ID=SIZE_T_ERR_VAL, const std::string &screenshot_fpth="", const std::string &label="")
Switches the search context for the user to the provided index in the history and returns reference t...
Definition: somhunter.cpp:1561
void log_video_replay(FrameId frame_ID, float delta_X)
Definition: somhunter.cpp:649
bool login_to_eval_server()
Tries to login into the remote evaluation server (competition one).
Definition: somhunter.cpp:609
std::vector< float > get_top_scored_scores(std::vector< FrameId > &top_scored_frames) const
Definition: somhunter.cpp:680
std::vector< VideoFramePointer > get_top_scored_frames(size_t max_count=0, size_t from_video=0, size_t from_shot=0) const
Definition: somhunter.cpp:674
FramePointerRange get_topn_context_display(PageId page)
Definition: somhunter.cpp:1414
size_t find_targets(const std::vector< FrameId > &top_scored, const std::vector< FrameId > &targets) const
Definition: somhunter.cpp:689
void apply_filters()
Definition: somhunter.cpp:223
const UserContext & get_user_context() const
Returns a reference to the current user's context.
Definition: somhunter.cpp:1604
void rescore_keywords(SpecificKWRanker &kw_ranker, const TextualQuery &query, size_t temporal, const SpecificFrameFeatures &features)
Applies text query from the user.
Definition: somhunter.h:278
void push_search_ctx()
Adds the currently active search context to the history and starts a new context (with next contiguou...
Definition: somhunter.cpp:214
void benchmark_native_text_queries(const std::string &queries_filepath, const std::string &out_dir)
Definition: somhunter.cpp:706
const RelocationRanker _relocation_ranker
Definition: somhunter.h:272
KeywordRanker _keyword_ranker
Definition: somhunter.h:269
const std::string _API_settings_filepath
Definition: somhunter.h:267
const SearchContext & get_search_context() const
Returns a reference to the current user's search context.
Definition: somhunter.cpp:1602
static void write_resultset(const std::string &file, const std::vector< VideoFramePointer > &results)
Definition: somhunter.cpp:1268
const std::string & get_config_filepath()
Definition: somhunter.h:161
std::string store_rescore_screenshot(const std::string &filepath)
Definition: somhunter.cpp:661
const std::string & get_API_config_filepath()
Definition: somhunter.h:162
UserContext _user_context
Definition: somhunter.h:260
void run_generators()
Definition: somhunter.cpp:384
const std::string _core_settings_filepath
Definition: somhunter.h:266
FramePointerRange get_random_display()
Definition: somhunter.cpp:1377
const DatasetFeatures _dataset_features
Definition: somhunter.h:254
const Settings & settings() const
Definition: somhunter.h:160
const std::vector< FrameId > & get_top_scored(size_t max_count=0, size_t from_video=0, size_t from_shot=0) const
Definition: somhunter.cpp:670
CanvasQueryRanker _collage_ranker
Definition: somhunter.h:271
const KeywordRanker * textual_model()
Definition: somhunter.h:163
FramePointerRange get_topn_display(PageId page)
Definition: somhunter.cpp:1392
GetDisplayResult get_display(DisplayType d_type, FrameId selected_image=0, PageId page=0, bool log_it=true)
Returns display of the desired type.
Definition: somhunter.cpp:54
std::vector< bool > bookmark_frames(const std::vector< FrameId > &new_bookmarks)
(De)selects the provided frames from the bookmark list.
Definition: somhunter.cpp:162
KeywordClipRanker _secondary_keyword_ranker
Definition: somhunter.h:270
Represents exactly one state of ONE user that uses this core.
Definition: user-context.h:38
SearchContext ctx
Definition: user-context.h:72
#define SIZE_T_ERR_VAL
Definition: common-types.h:92
Definition: common-types.h:33
unsigned VideoId
Definition: common-types.h:71
unsigned PageId
Definition: common-types.h:240
unsigned FrameNum
Definition: common-types.h:72
std::string TextualQuery
Definition: query-types.h:155
DisplayType
Definition: common-types.h:102
SubmitResult
Definition: common-types.h:56
unsigned long FrameId
Definition: common-types.h:75
Represents CONTINOUS range of frames.
Definition: dataset-frames.h:90
Result type get_display returns.
Definition: user-context.h:96
The type representing the whole query.
Definition: query-types.h:470
Result type rescore returns.
Definition: user-context.h:104
Represents exactly one momentary state of a search session.
Definition: search-context.h:44
ScoreModel scores
Definition: search-context.h:70
UsedTools used_tools
Definition: search-context.h:63
Parsed current config of the core.
Definition: settings.h:190
bool text_search_used
Definition: common-types.h:206
Definition: dataset-frames.h:40