OpenShot Library | libopenshot  0.7.0
CVObjectDetection.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 #pragma once
15 
16 #define int64 opencv_broken_int
17 #define uint64 opencv_broken_uint
18 #include <opencv2/dnn.hpp>
19 #include <opencv2/opencv.hpp>
20 #include <opencv2/core.hpp>
21 #undef uint64
22 #undef int64
23 #include "Json.h"
24 #include "ProcessingController.h"
25 #include "Clip.h"
26 
27 #include "sort_filter/sort.hpp"
28 
29 // Forward decl
30 namespace pb_objdetect {
31  class Frame;
32 }
33 
34 namespace openshot
35 {
37  int width = 0;
38  int height = 0;
39  std::vector<uint32_t> rle;
40 
41  bool HasData() const { return width > 0 && height > 0 && !rle.empty(); }
42  };
43 
45  size_t frameId = 0;
47  cv::Rect_<float> box;
48  };
49 
50  // Stores the detected object bounding boxes and its properties.
54  std::vector<int> _classIds,
55  std::vector<float> _confidences,
56  std::vector<cv::Rect_<float>> _boxes,
57  size_t _frameId,
58  std::vector<int> _objectIds,
59  std::vector<CVObjectMaskData> _masks = {})
60  {
61  classIds = _classIds;
62  confidences = _confidences;
63  boxes = _boxes;
64  frameId = _frameId;
65  objectIds = _objectIds;
66  masks = _masks;
67  }
68  size_t frameId;
69  std::vector<int> classIds;
70  std::vector<float> confidences;
71  std::vector<cv::Rect_<float>> boxes;
72  std::vector<int> objectIds;
73  std::vector<CVObjectMaskData> masks;
74  };
75 
82 
83  private:
84 
85  cv::dnn::Net net;
86  std::vector<std::string> classNames;
87  float confThreshold, nmsThreshold;
88 
89  std::string classesFile;
90  std::string modelPath;
91  std::string processingDevice;
92  std::string protobuf_data_path;
93  int inpWidth;
94  int inpHeight;
95  bool generateMasks;
96 
97  SortTracker sort;
98  std::map<int, CVTrackedMaskData> recentObjectMasks;
99 
100  uint progress;
101 
102  size_t start;
103  size_t end;
104 
105  bool error = false;
106 
108  ProcessingController *processingController;
109 
110  void setProcessingDevice();
111 
112  // Detect onbects on a single frame
113  void DetectObjects(const cv::Mat &frame, size_t frame_number);
114 
115  bool iou(cv::Rect pred_box, cv::Rect sort_box);
116 
117  // Remove the bounding boxes with low confidence using non-maxima suppression
118  void postprocess(const cv::Size &frameDims, const std::vector<cv::Mat>& out, size_t frame_number);
119 
120  void NormalizeTrackedClasses();
121 
122  // Get the names of the output layers
123  std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net);
124 
125  public:
126 
127  std::map<size_t, CVDetectionData> detectionsData;
128 
129  CVObjectDetection(std::string processInfoJson, ProcessingController &processingController);
130 
131  static std::string ValidateONNXModel(std::string modelPath);
132 
133  // Iterate over a clip object and run inference for each video frame
134  void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false);
135 
136  CVDetectionData GetDetectionData(size_t frameId);
137 
139  // Save protobuf file
140  bool SaveObjDetectedData();
141  // Add frame object detection data into protobuf message.
142  void AddFrameDataToProto(pb_objdetect::Frame* pbFrameData, CVDetectionData& dData);
143 
144  // Get and Set JSON methods
145  void SetJson(const std::string value);
146  void SetJsonValue(const Json::Value root);
147 
148  // Load protobuf file (ONLY FOR MAKE TEST)
149  bool _LoadObjDetectdData();
150  };
151 
152 }
openshot::CVObjectDetection::CVObjectDetection
CVObjectDetection(std::string processInfoJson, ProcessingController &processingController)
Definition: CVObjectDetection.cpp:246
openshot::CVObjectMaskData::width
int width
Definition: CVObjectDetection.h:37
Clip.h
Header file for Clip class.
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::CVDetectionData::classIds
std::vector< int > classIds
Definition: CVObjectDetection.h:69
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::CVDetectionData::masks
std::vector< CVObjectMaskData > masks
Definition: CVObjectDetection.h:73
openshot::CVObjectDetection::detectionsData
std::map< size_t, CVDetectionData > detectionsData
Definition: CVObjectDetection.h:127
openshot::CVDetectionData::confidences
std::vector< float > confidences
Definition: CVObjectDetection.h:70
openshot::CVTrackedMaskData::box
cv::Rect_< float > box
Definition: CVObjectDetection.h:47
openshot::CVObjectDetection
This class runs trought a clip to detect objects and returns the bounding boxes and its properties.
Definition: CVObjectDetection.h:81
openshot::CVObjectDetection::GetDetectionData
CVDetectionData GetDetectionData(size_t frameId)
Definition: CVObjectDetection.cpp:705
pb_objdetect
Definition: CVObjectDetection.h:30
openshot::CVObjectDetection::AddFrameDataToProto
void AddFrameDataToProto(pb_objdetect::Frame *pbFrameData, CVDetectionData &dData)
Definition: CVObjectDetection.cpp:810
openshot::CVDetectionData::objectIds
std::vector< int > objectIds
Definition: CVObjectDetection.h:72
SortTracker
Definition: sort.hpp:33
openshot::CVObjectDetection::_LoadObjDetectdData
bool _LoadObjDetectdData()
Definition: CVObjectDetection.cpp:921
openshot::CVObjectMaskData::rle
std::vector< uint32_t > rle
Definition: CVObjectDetection.h:39
ProcessingController.h
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
openshot::CVDetectionData::frameId
size_t frameId
Definition: CVObjectDetection.h:68
openshot::CVDetectionData
Definition: CVObjectDetection.h:51
openshot::CVObjectMaskData::HasData
bool HasData() const
Definition: CVObjectDetection.h:41
openshot::CVObjectMaskData
Definition: CVObjectDetection.h:36
openshot::CVObjectDetection::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVObjectDetection.cpp:857
openshot::CVDetectionData::boxes
std::vector< cv::Rect_< float > > boxes
Definition: CVObjectDetection.h:71
openshot::CVObjectDetection::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVObjectDetection.cpp:839
openshot::CVTrackedMaskData::frameId
size_t frameId
Definition: CVObjectDetection.h:45
openshot::CVDetectionData::CVDetectionData
CVDetectionData(std::vector< int > _classIds, std::vector< float > _confidences, std::vector< cv::Rect_< float >> _boxes, size_t _frameId, std::vector< int > _objectIds, std::vector< CVObjectMaskData > _masks={})
Definition: CVObjectDetection.h:53
ProcessingController
Definition: ProcessingController.h:20
openshot::CVTrackedMaskData
Definition: CVObjectDetection.h:44
openshot::CVObjectDetection::ValidateONNXModel
static std::string ValidateONNXModel(std::string modelPath)
Definition: CVObjectDetection.cpp:253
openshot::CVObjectDetection::SaveObjDetectedData
bool SaveObjDetectedData()
Protobuf Save and Load methods.
Definition: CVObjectDetection.cpp:767
sort.hpp
Json.h
Header file for JSON class.
openshot::CVTrackedMaskData::mask
CVObjectMaskData mask
Definition: CVObjectDetection.h:46
openshot::CVObjectDetection::detectObjectsClip
void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
Definition: CVObjectDetection.cpp:278
openshot::CVObjectMaskData::height
int height
Definition: CVObjectDetection.h:38
openshot::CVDetectionData::CVDetectionData
CVDetectionData()
Definition: CVObjectDetection.h:52