OpenShot Library | libopenshot  0.3.3
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 {
36  // Stores the detected object bounding boxes and its properties.
40  std::vector<int> _classIds,
41  std::vector<float> _confidences,
42  std::vector<cv::Rect_<float>> _boxes,
43  size_t _frameId,
44  std::vector<int> _objectIds)
45  {
46  classIds = _classIds;
47  confidences = _confidences;
48  boxes = _boxes;
49  frameId = _frameId;
50  objectIds = _objectIds;
51  }
52  size_t frameId;
53  std::vector<int> classIds;
54  std::vector<float> confidences;
55  std::vector<cv::Rect_<float>> boxes;
56  std::vector<int> objectIds;
57  };
58 
65 
66  private:
67 
68  cv::dnn::Net net;
69  std::vector<std::string> classNames;
70  float confThreshold, nmsThreshold;
71 
72  std::string classesFile;
73  std::string modelConfiguration;
74  std::string modelWeights;
75  std::string processingDevice;
76  std::string protobuf_data_path;
77 
78  SortTracker sort;
79 
80  uint progress;
81 
82  size_t start;
83  size_t end;
84 
85  bool error = false;
86 
88  ProcessingController *processingController;
89 
90  void setProcessingDevice();
91 
92  // Detect onbects on a single frame
93  void DetectObjects(const cv::Mat &frame, size_t frame_number);
94 
95  bool iou(cv::Rect pred_box, cv::Rect sort_box);
96 
97  // Remove the bounding boxes with low confidence using non-maxima suppression
98  void postprocess(const cv::Size &frameDims, const std::vector<cv::Mat>& out, size_t frame_number);
99 
100  // Get the names of the output layers
101  std::vector<cv::String> getOutputsNames(const cv::dnn::Net& net);
102 
103  public:
104 
105  std::map<size_t, CVDetectionData> detectionsData;
106 
107  CVObjectDetection(std::string processInfoJson, ProcessingController &processingController);
108 
109  // Iterate over a clip object and run inference for each video frame
110  void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false);
111 
112  CVDetectionData GetDetectionData(size_t frameId);
113 
115  // Save protobuf file
116  bool SaveObjDetectedData();
117  // Add frame object detection data into protobuf message.
118  void AddFrameDataToProto(pb_objdetect::Frame* pbFrameData, CVDetectionData& dData);
119 
120  // Get and Set JSON methods
121  void SetJson(const std::string value);
122  void SetJsonValue(const Json::Value root);
123 
124  // Load protobuf file (ONLY FOR MAKE TEST)
125  bool _LoadObjDetectdData();
126  };
127 
128 }
openshot::CVObjectDetection::CVObjectDetection
CVObjectDetection(std::string processInfoJson, ProcessingController &processingController)
Definition: CVObjectDetection.cpp:28
Clip.h
Header file for Clip class.
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::CVDetectionData::classIds
std::vector< int > classIds
Definition: CVObjectDetection.h:53
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::CVObjectDetection::detectionsData
std::map< size_t, CVDetectionData > detectionsData
Definition: CVObjectDetection.h:105
openshot::CVDetectionData::confidences
std::vector< float > confidences
Definition: CVObjectDetection.h:54
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)
Definition: CVObjectDetection.h:39
openshot::CVObjectDetection
This class runs trought a clip to detect objects and returns the bounding boxes and its properties.
Definition: CVObjectDetection.h:64
openshot::CVObjectDetection::GetDetectionData
CVDetectionData GetDetectionData(size_t frameId)
Definition: CVObjectDetection.cpp:290
pb_objdetect
Definition: CVObjectDetection.h:30
openshot::CVObjectDetection::AddFrameDataToProto
void AddFrameDataToProto(pb_objdetect::Frame *pbFrameData, CVDetectionData &dData)
Definition: CVObjectDetection.cpp:338
openshot::CVDetectionData::objectIds
std::vector< int > objectIds
Definition: CVObjectDetection.h:56
SortTracker
Definition: sort.hpp:32
openshot::CVObjectDetection::_LoadObjDetectdData
bool _LoadObjDetectdData()
Definition: CVObjectDetection.cpp:423
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:52
openshot::CVDetectionData
Definition: CVObjectDetection.h:37
openshot::CVObjectDetection::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: CVObjectDetection.cpp:377
openshot::CVDetectionData::boxes
std::vector< cv::Rect_< float > > boxes
Definition: CVObjectDetection.h:55
openshot::CVObjectDetection::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: CVObjectDetection.cpp:359
ProcessingController
Definition: ProcessingController.h:20
openshot::CVObjectDetection::SaveObjDetectedData
bool SaveObjDetectedData()
Protobuf Save and Load methods.
Definition: CVObjectDetection.cpp:301
sort.hpp
Json.h
Header file for JSON class.
openshot::CVObjectDetection::detectObjectsClip
void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
Definition: CVObjectDetection.cpp:46
openshot::CVDetectionData::CVDetectionData
CVDetectionData()
Definition: CVObjectDetection.h:38