OpenShot Library | libopenshot
1.1.1
Toggle main menu visibility
Loading...
Searching...
No Matches
CVObjectDetection.h
Go to the documentation of this file.
1
9
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
struct
CVObjectMaskData
{
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
44
struct
CVTrackedMaskData
{
45
size_t
frameId
= 0;
46
CVObjectMaskData
mask
;
47
cv::Rect_<float>
box
;
48
};
49
50
// Stores the detected object bounding boxes and its properties.
51
struct
CVDetectionData
{
52
CVDetectionData
(){}
53
CVDetectionData
(
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
81
class
CVObjectDetection
{
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
}
Clip.h
Header file for Clip class.
Json.h
Header file for JSON class.
ProcessingController.h
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
ProcessingController
Definition
ProcessingController.h:20
SortTracker
Definition
sort.hpp:34
openshot::CVObjectDetection::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition
CVObjectDetection.cpp:885
openshot::CVObjectDetection::CVObjectDetection
CVObjectDetection(std::string processInfoJson, ProcessingController &processingController)
Definition
CVObjectDetection.cpp:252
openshot::CVObjectDetection::_LoadObjDetectdData
bool _LoadObjDetectdData()
Definition
CVObjectDetection.cpp:949
openshot::CVObjectDetection::detectObjectsClip
void detectObjectsClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
Definition
CVObjectDetection.cpp:306
openshot::CVObjectDetection::AddFrameDataToProto
void AddFrameDataToProto(pb_objdetect::Frame *pbFrameData, CVDetectionData &dData)
Definition
CVObjectDetection.cpp:838
openshot::CVObjectDetection::GetDetectionData
CVDetectionData GetDetectionData(size_t frameId)
Definition
CVObjectDetection.cpp:733
openshot::CVObjectDetection::ValidateONNXModel
static std::string ValidateONNXModel(std::string modelPath)
Definition
CVObjectDetection.cpp:259
openshot::CVObjectDetection::detectionsData
std::map< size_t, CVDetectionData > detectionsData
Definition
CVObjectDetection.h:127
openshot::CVObjectDetection::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition
CVObjectDetection.cpp:867
openshot::CVObjectDetection::SaveObjDetectedData
bool SaveObjDetectedData()
Protobuf Save and Load methods.
Definition
CVObjectDetection.cpp:795
openshot::Clip
This class represents a clip (used to arrange readers on the timeline).
Definition
Clip.h:89
openshot
This namespace is the default namespace for all code in the openshot library.
Definition
AnimatedCurve.h:24
pb_objdetect
Definition
CVObjectDetection.h:30
sort.hpp
openshot::CVDetectionData
Definition
CVObjectDetection.h:51
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
openshot::CVDetectionData::frameId
size_t frameId
Definition
CVObjectDetection.h:68
openshot::CVDetectionData::CVDetectionData
CVDetectionData()
Definition
CVObjectDetection.h:52
openshot::CVDetectionData::objectIds
std::vector< int > objectIds
Definition
CVObjectDetection.h:72
openshot::CVDetectionData::boxes
std::vector< cv::Rect_< float > > boxes
Definition
CVObjectDetection.h:71
openshot::CVDetectionData::masks
std::vector< CVObjectMaskData > masks
Definition
CVObjectDetection.h:73
openshot::CVDetectionData::classIds
std::vector< int > classIds
Definition
CVObjectDetection.h:69
openshot::CVDetectionData::confidences
std::vector< float > confidences
Definition
CVObjectDetection.h:70
openshot::CVObjectMaskData
Definition
CVObjectDetection.h:36
openshot::CVObjectMaskData::width
int width
Definition
CVObjectDetection.h:37
openshot::CVObjectMaskData::rle
std::vector< uint32_t > rle
Definition
CVObjectDetection.h:39
openshot::CVObjectMaskData::height
int height
Definition
CVObjectDetection.h:38
openshot::CVObjectMaskData::HasData
bool HasData() const
Definition
CVObjectDetection.h:41
openshot::CVTrackedMaskData
Definition
CVObjectDetection.h:44
openshot::CVTrackedMaskData::box
cv::Rect_< float > box
Definition
CVObjectDetection.h:47
openshot::CVTrackedMaskData::frameId
size_t frameId
Definition
CVObjectDetection.h:45
openshot::CVTrackedMaskData::mask
CVObjectMaskData mask
Definition
CVObjectDetection.h:46
CVObjectDetection.h
Generated by
1.17.0