OpenShot Library | libopenshot  0.5.0
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  // count of consecutive “missed” frames
98  int lostCount{0};
99 
100  // KLT parameters and state
101  cv::Mat prevGray; // last frame in gray
102  std::vector<cv::Point2f> prevPts; // tracked feature points
103  const int kltMaxCorners = 100; // max features to keep
104  const double kltQualityLevel = 0.01; // goodFeatures threshold
105  const double kltMinDist = 5.0; // min separation
106  const int kltBlockSize = 3; // window for feature detect
107  const int minKltPts = 10; // below this, we assume occluded
108  double smoothC_x = 0, smoothC_y = 0;
109  const double smoothAlpha = 0.8;
110 
111  // full-frame fall-back
112  cv::Mat fullPrevGray;
113 
114  // last known good box size
115  double origWidth{0}, origHeight{0};
116 
117  public:
118 
119  // Constructor
120  CVTracker(std::string processInfoJson, ProcessingController &processingController);
121 
122  // Set desirable tracker method
123  cv::Ptr<OPENCV_TRACKER_TYPE> selectTracker(std::string trackerType);
124 
128  void trackClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);
129 
130  // Update the object tracker according to frame
131  bool trackFrame(cv::Mat &frame, size_t frameId);
132 
133  // Initialize the tracker
134  bool initTracker(cv::Mat &frame, size_t frameId);
135 
137  FrameData GetTrackedData(size_t frameId);
138 
139  // Protobuf Save and Load methods
141  bool SaveTrackedData();
143  void AddFrameDataToProto(pb_tracker::Frame* pbFrameData, FrameData& fData);
144 
145  // Get and Set JSON methods
146  void SetJson(const std::string value);
147  void SetJsonValue(const Json::Value root);
148 
149  // Load protobuf file (ONLY FOR MAKE TEST)
150  bool _LoadTrackedData();
151  };
152 }
153 
154 #endif
Header file for Clip class.
Header file for Frame class.
Header file for JSON class.
Header file for the Keyframe class.
Header file for OpenCVUtilities (set some common macros)
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
The tracker class will receive one bounding box provided by the user and then iterate over the clip f...
Definition: CVTracker.h:75
void trackClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Definition: CVTracker.cpp:70
CVTracker(std::string processInfoJson, ProcessingController &processingController)
Definition: CVTracker.cpp:40
bool SaveTrackedData()
Save protobuf file.
Definition: CVTracker.cpp:315
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVTracker.cpp:394
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVTracker.cpp:377
bool trackFrame(cv::Mat &frame, size_t frameId)
Definition: CVTracker.cpp:172
void AddFrameDataToProto(pb_tracker::Frame *pbFrameData, FrameData &fData)
Add frame tracked data into protobuf message.
Definition: CVTracker.cpp:348
FrameData GetTrackedData(size_t frameId)
Get tracked data for a given frame.
Definition: CVTracker.cpp:363
cv::Ptr< OPENCV_TRACKER_TYPE > selectTracker(std::string trackerType)
Definition: CVTracker.cpp:49
bool initTracker(cv::Mat &frame, size_t frameId)
Definition: CVTracker.cpp:127
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:29
FrameData(size_t _frame_id)
Definition: CVTracker.h:57
FrameData(size_t _frame_id, float _rotation, float _x1, float _y1, float _x2, float _y2)
Definition: CVTracker.h:60