OpenShot Library | libopenshot 1.1.1
Loading...
Searching...
No Matches
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"
25#include "Clip.h"
26
27#include "sort_filter/sort.hpp"
28
29// Forward decl
30namespace pb_objdetect {
31 class Frame;
32}
33
34namespace 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}
Header file for Clip class.
Header file for JSON class.
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
This class runs trought a clip to detect objects and returns the bounding boxes and its properties.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
void AddFrameDataToProto(pb_objdetect::Frame *pbFrameData, CVDetectionData &dData)
CVDetectionData GetDetectionData(size_t frameId)
static std::string ValidateONNXModel(std::string modelPath)
std::map< size_t, CVDetectionData > detectionsData
void SetJson(const std::string value)
Load JSON string into this object.
bool SaveObjDetectedData()
Protobuf Save and Load methods.
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.
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={})
std::vector< int > objectIds
std::vector< cv::Rect_< float > > boxes
std::vector< CVObjectMaskData > masks
std::vector< int > classIds
std::vector< float > confidences
std::vector< uint32_t > rle