OpenShot Library | libopenshot  0.2.7
CVTracker.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Track an object selected by the user
4  * @author Jonathan Thomas <jonathan@openshot.org>
5  * @author Brenno Caldato <brenno.caldato@outlook.com>
6  *
7  * @ref License
8  */
9 
10 /* LICENSE
11  *
12  * Copyright (c) 2008-2019 OpenShot Studios, LLC
13  * <http://www.openshotstudios.com/>. This file is part of
14  * OpenShot Library (libopenshot), an open-source project dedicated to
15  * delivering high quality video editing and animation solutions to the
16  * world. For more information visit <http://www.openshot.org/>.
17  *
18  * OpenShot Library (libopenshot) is free software: you can redistribute it
19  * and/or modify it under the terms of the GNU Lesser General Public License
20  * as published by the Free Software Foundation, either version 3 of the
21  * License, or (at your option) any later version.
22  *
23  * OpenShot Library (libopenshot) is distributed in the hope that it will be
24  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
30  */
31 
32 #ifndef OPENSHOT_CVTRACKER_H
33 #define OPENSHOT_CVTRACKER_H
34 
35 #include "OpenCVUtilities.h"
36 
37 #define int64 int64_t
38 #define uint64 uint64_t
39 #include <opencv2/opencv.hpp>
40 #include <opencv2/tracking.hpp>
41 #include <opencv2/core.hpp>
42 #undef uint64
43 #undef int64
44 
45 #include "Clip.h"
46 #include "KeyFrame.h"
47 #include "Frame.h"
48 #include "Json.h"
49 
50 #include "ProcessingController.h"
51 #include "protobuf_messages/trackerdata.pb.h"
52 
53 #include "sort_filter/sort.hpp"
54 
55 namespace openshot
56 {
57 
58  // Store the tracked object information for one frame
59  struct FrameData{
60  size_t frame_id = -1;
61  float rotation = 0;
62  float x1 = -1;
63  float y1 = -1;
64  float x2 = -1;
65  float y2 = -1;
66 
67  // Constructors
69  {}
70 
71  FrameData( size_t _frame_id)
72  {frame_id = _frame_id;}
73 
74  FrameData( size_t _frame_id , float _rotation, float _x1, float _y1, float _x2, float _y2)
75  {
76  frame_id = _frame_id;
77  rotation = _rotation;
78  x1 = _x1;
79  y1 = _y1;
80  x2 = _x2;
81  y2 = _y2;
82  }
83  };
84 
85  /**
86  * @brief The tracker class will receive one bounding box provided by the user and then iterate over the clip frames
87  * to return the object position in all the frames.
88  */
89  class CVTracker {
90  private:
91  std::map<size_t, FrameData> trackedDataById; // Save tracked data
92  std::string trackerType; // Name of the chosen tracker
93  cv::Ptr<OPENCV_TRACKER_TYPE> tracker; // Pointer of the selected tracker
94 
95  cv::Rect2d bbox; // Bounding box coords
96  SortTracker sort;
97 
98  std::string protobuf_data_path; // Path to protobuf data file
99 
100  uint progress; // Pre-processing effect progress
101 
102  /// Will handle a Thread safely comutication between ClipProcessingJobs and the processing effect classes
103  ProcessingController *processingController;
104 
105  bool json_interval;
106  size_t start;
107  size_t end;
108 
109  bool error = false;
110 
111  // Initialize the tracker
112  bool initTracker(cv::Mat &frame, size_t frameId);
113 
114  // Update the object tracker according to frame
115  bool trackFrame(cv::Mat &frame, size_t frameId);
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 
125  /// Track object in the hole clip or in a given interval
126  ///
127  /// If start, end and process_interval are passed as argument, clip will be processed in [start,end)
128  void trackClip(openshot::Clip& video, size_t _start=0, size_t _end=0, bool process_interval=false);
129 
130  /// Filter current bounding box jitter
131  cv::Rect2d filter_box_jitter(size_t frameId);
132 
133  /// Get tracked data for a given frame
134  FrameData GetTrackedData(size_t frameId);
135 
136  // Protobuf Save and Load methods
137  /// Save protobuf file
138  bool SaveTrackedData();
139  /// Add frame tracked data into protobuf message.
140  void AddFrameDataToProto(pb_tracker::Frame* pbFrameData, FrameData& fData);
141 
142  // Get and Set JSON methods
143  void SetJson(const std::string value); ///< Load JSON string into this object
144  void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object
145 
146  // Load protobuf file (ONLY FOR MAKE TEST)
147  bool _LoadTrackedData();
148  };
149 }
150 
151 #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:89
void trackClip(openshot::Clip &video, size_t _start=0, size_t _end=0, bool process_interval=false)
Definition: CVTracker.cpp:74
CVTracker(std::string processInfoJson, ProcessingController &processingController)
Definition: CVTracker.cpp:45
bool SaveTrackedData()
Save protobuf file.
Definition: CVTracker.cpp:221
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVTracker.cpp:300
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVTracker.cpp:283
cv::Rect2d filter_box_jitter(size_t frameId)
Filter current bounding box jitter.
Definition: CVTracker.cpp:201
void AddFrameDataToProto(pb_tracker::Frame *pbFrameData, FrameData &fData)
Add frame tracked data into protobuf message.
Definition: CVTracker.cpp:254
FrameData GetTrackedData(size_t frameId)
Get tracked data for a given frame.
Definition: CVTracker.cpp:269
cv::Ptr< OPENCV_TRACKER_TYPE > selectTracker(std::string trackerType)
Definition: CVTracker.cpp:53
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:109
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
FrameData(size_t _frame_id)
Definition: CVTracker.h:71
FrameData(size_t _frame_id, float _rotation, float _x1, float _y1, float _x2, float _y2)
Definition: CVTracker.h:74