OpenShot Library | libopenshot  0.3.3
CVTracker.h
Go to the documentation of this file.
1 
10 // Copyright (c) 2008-2019 OpenShot Studios, LLC
11 //
12 // SPDX-License-Identifier: LGPL-3.0-or-later
13 
14 #ifndef OPENSHOT_CVTRACKER_H
15 #define OPENSHOT_CVTRACKER_H
16 
17 #include "OpenCVUtilities.h"
18 
19 #define int64 int64_t
20 #define uint64 uint64_t
21 #include <opencv2/opencv.hpp>
22 #include <opencv2/tracking.hpp>
23 #include <opencv2/core.hpp>
24 #undef uint64
25 #undef int64
26 
27 #include "Clip.h"
28 #include "KeyFrame.h"
29 #include "Frame.h"
30 #include "Json.h"
31 
32 #include "ProcessingController.h"
33 
34 #include "sort_filter/sort.hpp"
35 
36 // Forward decl
37 namespace pb_tracker {
38  class Frame;
39 }
40 
41 namespace openshot
42 {
43 
44  // Store the tracked object information for one frame
45  struct FrameData{
46  size_t frame_id = -1;
47  float rotation = 0;
48  float x1 = -1;
49  float y1 = -1;
50  float x2 = -1;
51  float y2 = -1;
52 
53  // Constructors
55  {}
56 
57  FrameData( size_t _frame_id)
58  {frame_id = _frame_id;}
59 
60  FrameData( size_t _frame_id , float _rotation, float _x1, float _y1, float _x2, float _y2)
61  {
62  frame_id = _frame_id;
63  rotation = _rotation;
64  x1 = _x1;
65  y1 = _y1;
66  x2 = _x2;
67  y2 = _y2;
68  }
69  };
70 
75  class CVTracker {
76  private:
77  std::map<size_t, FrameData> trackedDataById; // Save tracked data
78  std::string trackerType; // Name of the chosen tracker
79  cv::Ptr<OPENCV_TRACKER_TYPE> tracker; // Pointer of the selected tracker
80 
81  cv::Rect2d bbox; // Bounding box coords
82  SortTracker sort;
83 
84  std::string protobuf_data_path; // Path to protobuf data file
85 
86  uint progress; // Pre-processing effect progress
87 
89  ProcessingController *processingController;
90 
91  bool json_interval;
92  size_t start;
93  size_t end;
94 
95  bool error = false;
96 
97  // Initialize the tracker
98  bool initTracker(cv::Mat &frame, size_t frameId);
99 
100  // Update the object tracker according to frame
101  bool trackFrame(cv::Mat &frame, size_t frameId);
102 
103  public:
104 
105  // Constructor
106  CVTracker(std::string processInfoJson, ProcessingController &processingController);
107 
108  // Set desirable tracker method
109  cv::Ptr<OPENCV_TRACKER_TYPE> selectTracker(std::string trackerType);
110 
114  void trackClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);
115 
117  cv::Rect2d filter_box_jitter(size_t frameId);
118 
120  FrameData GetTrackedData(size_t frameId);
121 
122  // Protobuf Save and Load methods
124  bool SaveTrackedData();
126  void AddFrameDataToProto(pb_tracker::Frame* pbFrameData, FrameData& fData);
127 
128  // Get and Set JSON methods
129  void SetJson(const std::string value);
130  void SetJsonValue(const Json::Value root);
131 
132  // Load protobuf file (ONLY FOR MAKE TEST)
133  bool _LoadTrackedData();
134  };
135 }
136 
137 #endif
openshot::CVTracker::GetTrackedData
FrameData GetTrackedData(size_t frameId)
Get tracked data for a given frame.
Definition: CVTracker.cpp:253
Clip.h
Header file for Clip class.
openshot::FrameData::x2
float x2
Definition: CVTracker.h:50
openshot::FrameData::FrameData
FrameData(size_t _frame_id, float _rotation, float _x1, float _y1, float _x2, float _y2)
Definition: CVTracker.h:60
openshot::FrameData::frame_id
size_t frame_id
Definition: CVTracker.h:46
openshot::CVTracker::AddFrameDataToProto
void AddFrameDataToProto(pb_tracker::Frame *pbFrameData, FrameData &fData)
Add frame tracked data into protobuf message.
Definition: CVTracker.cpp:238
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::FrameData::rotation
float rotation
Definition: CVTracker.h:47
pb_tracker
Definition: CVTracker.h:37
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::CVTracker::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVTracker.cpp:267
openshot::CVTracker::SaveTrackedData
bool SaveTrackedData()
Save protobuf file.
Definition: CVTracker.cpp:205
KeyFrame.h
Header file for the Keyframe class.
openshot::FrameData
Definition: CVTracker.h:45
openshot::FrameData::FrameData
FrameData()
Definition: CVTracker.h:54
openshot::CVTracker::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVTracker.cpp:284
openshot::FrameData::y2
float y2
Definition: CVTracker.h:51
openshot::CVTracker::CVTracker
CVTracker(std::string processInfoJson, ProcessingController &processingController)
Definition: CVTracker.cpp:29
SortTracker
Definition: sort.hpp:32
openshot::FrameData::x1
float x1
Definition: CVTracker.h:48
ProcessingController.h
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
Frame.h
Header file for Frame class.
openshot::FrameData::y1
float y1
Definition: CVTracker.h:49
openshot::CVTracker::_LoadTrackedData
bool _LoadTrackedData()
Definition: CVTracker.cpp:326
openshot::CVTracker::filter_box_jitter
cv::Rect2d filter_box_jitter(size_t frameId)
Filter current bounding box jitter.
Definition: CVTracker.cpp:185
openshot::CVTracker
The tracker class will receive one bounding box provided by the user and then iterate over the clip f...
Definition: CVTracker.h:75
openshot::CVTracker::selectTracker
cv::Ptr< OPENCV_TRACKER_TYPE > selectTracker(std::string trackerType)
Definition: CVTracker.cpp:37
ProcessingController
Definition: ProcessingController.h:20
openshot::CVTracker::trackClip
void trackClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Definition: CVTracker.cpp:58
sort.hpp
Json.h
Header file for JSON class.
openshot::FrameData::FrameData
FrameData(size_t _frame_id)
Definition: CVTracker.h:57
OpenCVUtilities.h
Header file for OpenCVUtilities (set some common macros)