OpenShot Library | libopenshot  0.2.7
ObjectDetection.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for Object Detection effect class
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_OBJECT_DETECTION_EFFECT_H
33 #define OPENSHOT_OBJECT_DETECTION_EFFECT_H
34 
35 #include "../EffectBase.h"
36 
37 #include <cmath>
38 #include <stdio.h>
39 #include <memory>
40 #include <opencv2/opencv.hpp>
41 #include "../Color.h"
42 #include "../Json.h"
43 #include "../KeyFrame.h"
44 #include "protobuf_messages/objdetectdata.pb.h"
45 
46 // Struct that stores the detected bounding boxes for all the clip frames
50  std::vector<int> _classIds,
51  std::vector<float> _confidences,
52  std::vector<cv::Rect_<float>> _boxes,
53  size_t _frameId,
54  std::vector<int> _objectIds)
55  {
56  classIds = _classIds;
57  confidences = _confidences;
58  boxes = _boxes;
59  frameId = _frameId;
60  objectIds = _objectIds;
61  }
62  size_t frameId;
63  std::vector<int> classIds;
64  std::vector<float> confidences;
65  std::vector<cv::Rect_<float>> boxes;
66  std::vector<int> objectIds;
67 };
68 
69 namespace openshot
70 {
71  /**
72  * @brief This effect displays all the detected objects on a clip.
73  */
74  class ObjectDetection : public EffectBase
75  {
76  private:
77  std::string protobuf_data_path;
78  std::map<size_t, DetectionData> detectionsData;
79  std::vector<std::string> classNames;
80 
81  std::vector<cv::Scalar> classesColor;
82 
83  /// Draw class name and confidence score on top of the bounding box
84  Keyframe display_box_text;
85  /// Minimum confidence value to display the detected objects
86  float confidence_threshold = 0.5;
87  /// Contain the user selected classes for visualization
88  std::vector<std::string> display_classes;
89  std::string class_filter;
90 
91  /// Init effect settings
92  void init_effect_details();
93  /// Draw bounding box with class and score text
94  void drawPred(int classId, float conf, cv::Rect2d box, cv::Mat& frame, int objectNumber, std::vector<int> color, float alpha,
95  int thickness, bool is_background, bool draw_text);
96  /// Draw rotated rectangle with alpha channel
97  void DrawRectangleRGBA(cv::Mat &frame_image, cv::RotatedRect box, std::vector<int> color, float alpha, int thickness, bool is_background);
98 
99 
100  public:
101  /// Index of the Tracked Object that was selected to modify it's properties
103 
104  ObjectDetection(std::string clipTrackerDataPath);
105 
106  /// Default constructor
107  ObjectDetection();
108 
109  /// @brief This method is required for all derived classes of EffectBase, and returns a
110  /// modified openshot::Frame object
111  ///
112  /// The frame object is passed into this method, and a frame_number is passed in which
113  /// tells the effect which settings to use from its keyframes (starting at 1).
114  ///
115  /// @returns The modified openshot::Frame object
116  /// @param frame The frame object that needs the effect applied to it
117  /// @param frame_number The frame number (starting at 1) of the effect on the timeline.
118  std::shared_ptr<Frame> GetFrame(std::shared_ptr<Frame> frame, int64_t frame_number) override;
119 
120  std::shared_ptr<openshot::Frame> GetFrame(int64_t frame_number) override { return GetFrame(std::make_shared<Frame>(), frame_number); }
121 
122  /// Load protobuf data file
123  bool LoadObjDetectdData(std::string inputFilePath);
124 
125  /// Get the indexes and IDs of all visible objects in the given frame
126  std::string GetVisibleObjects(int64_t frame_number) const override;
127 
128  // Get and Set JSON methods
129  std::string Json() const override; ///< Generate JSON string of this object
130  void SetJson(const std::string value) override; ///< Load JSON string into this object
131  Json::Value JsonValue() const override; ///< Generate Json::Value for this object
132  void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
133 
134  /// Get all properties for a specific frame (perfect for a UI to display the current state
135  /// of all properties at any time)
136  std::string PropertiesJSON(int64_t requested_frame) const override;
137  };
138 
139 }
140 
141 #endif
This abstract class is the base class, used by all effects in libopenshot.
Definition: EffectBase.h:71
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:72
This effect displays all the detected objects on a clip.
std::shared_ptr< openshot::Frame > GetFrame(int64_t frame_number) override
This method is required for all derived classes of ClipBase, and returns a new openshot::Frame object...
Json::Value JsonValue() const override
Generate Json::Value for this object.
int selectedObjectIndex
Index of the Tracked Object that was selected to modify it's properties.
std::shared_ptr< Frame > GetFrame(std::shared_ptr< Frame > frame, int64_t frame_number) override
This method is required for all derived classes of EffectBase, and returns a modified openshot::Frame...
ObjectDetection()
Default constructor.
bool LoadObjDetectdData(std::string inputFilePath)
Load protobuf data file.
std::string GetVisibleObjects(int64_t frame_number) const override
Get the indexes and IDs of all visible objects in the given frame.
std::string Json() const override
Generate JSON string of this object.
std::string PropertiesJSON(int64_t requested_frame) const override
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
void SetJson(const std::string value) override
Load JSON string into this object.
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:47
std::vector< cv::Rect_< float > > boxes
DetectionData(std::vector< int > _classIds, std::vector< float > _confidences, std::vector< cv::Rect_< float >> _boxes, size_t _frameId, std::vector< int > _objectIds)
std::vector< float > confidences
std::vector< int > classIds
std::vector< int > objectIds