OpenShot Library | libopenshot  0.7.0
CVObjectMask.h
Go to the documentation of this file.
1 
9 // Copyright (c) 2026 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #pragma once
14 
15 #define int64 opencv_broken_int
16 #define uint64 opencv_broken_uint
17 #include <opencv2/core.hpp>
18 #include <opencv2/dnn.hpp>
19 #include <opencv2/opencv.hpp>
20 #undef uint64
21 #undef int64
22 
23 #include "Clip.h"
24 #include "Json.h"
25 #include "ProcessingController.h"
26 
27 namespace pb_objdetect {
28  class Frame;
29 }
30 
31 namespace openshot
32 {
34  size_t frameId = 0;
35  cv::Rect_<float> box;
36  float score = 0.0f;
37  int objectId = 1;
38  int width = 0;
39  int height = 0;
40  std::vector<uint32_t> rle;
41 
42  bool HasMask() const { return width > 0 && height > 0 && !rle.empty(); }
43  };
44 
46  std::vector<cv::Point2f> positivePoints;
47  std::vector<cv::Point2f> negativePoints;
48  std::vector<cv::Rect_<float>> positiveRects;
49  std::vector<cv::Rect_<float>> negativeRects;
50 
51  bool HasPositivePrompt() const { return !positiveRects.empty() || !positivePoints.empty(); }
52  };
53 
58  {
59  private:
60  cv::dnn::Net efficientSam;
61 
62  std::string efficientSamModelPath;
63  std::string cutieModelDir;
64  std::string cutieEncodeKeyModelPath;
65  std::string cutieEncodeValueModelPath;
66  std::string cutieMemoryReadoutModelPath;
67  std::string cutieDecodeModelPath;
68  std::string protobufDataPath;
69  std::string processingDevice = "CPU";
70 
71  std::map<size_t, CVObjectMaskPromptSet> promptKeyframes;
72  int promptSlots = 6;
73  float maskThreshold = 0.0f;
74  int modelSize = 1024;
75 
76  size_t start = 0;
77  size_t end = 0;
78  bool error = false;
79 
80  ProcessingController* processingController;
81 
82  void SetProcessingDevice();
83  cv::Mat CreateEfficientSAMSeedMask(const cv::Mat& frame, const CVObjectMaskPromptSet& prompts);
84  void AddFrameDataToProto(pb_objdetect::Frame* pbFrameData, const CVObjectMaskFrameData& frameData);
85 
86  public:
87  std::map<size_t, CVObjectMaskFrameData> masksData;
88 
89  CVObjectMask(std::string processInfoJson, ProcessingController& processingController);
90 
91  static std::string ValidateONNXModel(std::string modelPath);
92  std::shared_ptr<Frame> PreviewSeedMask(std::shared_ptr<Frame> frame);
93 
94  void maskClip(openshot::Clip& video, size_t start = 0, size_t end = 0, bool process_interval = false);
95  bool SaveObjMaskData();
96 
97  void SetJson(const std::string value);
98  void SetJsonValue(const Json::Value root);
99  };
100 }
openshot::CVObjectMaskFrameData::score
float score
Definition: CVObjectMask.h:36
openshot::CVObjectMaskFrameData::box
cv::Rect_< float > box
Definition: CVObjectMask.h:35
openshot::CVObjectMask::maskClip
void maskClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
Definition: CVObjectMask.cpp:901
Clip.h
Header file for Clip class.
openshot::CVObjectMaskFrameData::rle
std::vector< uint32_t > rle
Definition: CVObjectMask.h:40
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::CVObjectMaskPromptSet::negativePoints
std::vector< cv::Point2f > negativePoints
Definition: CVObjectMask.h:47
openshot::Clip
This class represents a clip (used to arrange readers on the timeline)
Definition: Clip.h:89
openshot::CVObjectMaskPromptSet
Definition: CVObjectMask.h:45
openshot::CVObjectMaskFrameData::HasMask
bool HasMask() const
Definition: CVObjectMask.h:42
openshot::CVObjectMask::SaveObjMaskData
bool SaveObjMaskData()
Definition: CVObjectMask.cpp:1096
openshot::CVObjectMaskFrameData::width
int width
Definition: CVObjectMask.h:38
pb_objdetect
Definition: CVObjectDetection.h:30
openshot::CVObjectMaskPromptSet::positivePoints
std::vector< cv::Point2f > positivePoints
Definition: CVObjectMask.h:46
openshot::CVObjectMask::PreviewSeedMask
std::shared_ptr< Frame > PreviewSeedMask(std::shared_ptr< Frame > frame)
Definition: CVObjectMask.cpp:827
openshot::CVObjectMaskFrameData::frameId
size_t frameId
Definition: CVObjectMask.h:34
ProcessingController.h
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
openshot::CVObjectMaskFrameData
Definition: CVObjectMask.h:33
openshot::CVObjectMaskFrameData::objectId
int objectId
Definition: CVObjectMask.h:37
openshot::CVObjectMask::SetJsonValue
void SetJsonValue(const Json::Value root)
Definition: CVObjectMask.cpp:1153
openshot::CVObjectMask::ValidateONNXModel
static std::string ValidateONNXModel(std::string modelPath)
Definition: CVObjectMask.cpp:822
openshot::CVObjectMask::CVObjectMask
CVObjectMask(std::string processInfoJson, ProcessingController &processingController)
Definition: CVObjectMask.cpp:816
openshot::CVObjectMaskPromptSet::negativeRects
std::vector< cv::Rect_< float > > negativeRects
Definition: CVObjectMask.h:49
openshot::CVObjectMaskFrameData::height
int height
Definition: CVObjectMask.h:39
ProcessingController
Definition: ProcessingController.h:20
openshot::CVObjectMaskPromptSet::HasPositivePrompt
bool HasPositivePrompt() const
Definition: CVObjectMask.h:51
openshot::CVObjectMask
Preprocess a clip into EfficientSAM/Cutie object masks stored in the object-detection protobuf format...
Definition: CVObjectMask.h:57
Json.h
Header file for JSON class.
openshot::CVObjectMask::masksData
std::map< size_t, CVObjectMaskFrameData > masksData
Definition: CVObjectMask.h:87
openshot::CVObjectMaskPromptSet::positiveRects
std::vector< cv::Rect_< float > > positiveRects
Definition: CVObjectMask.h:48
openshot::CVObjectMask::SetJson
void SetJson(const std::string value)
Definition: CVObjectMask.cpp:1144