OpenShot Library | libopenshot  1.0.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 }
Header file for Clip class.
Header file for JSON class.
This is a message class for thread safe comunication between ClipProcessingJobs and OpenCV classes.
Preprocess a clip into EfficientSAM/Cutie object masks stored in the object-detection protobuf format...
Definition: CVObjectMask.h:58
std::map< size_t, CVObjectMaskFrameData > masksData
Definition: CVObjectMask.h:87
void maskClip(openshot::Clip &video, size_t start=0, size_t end=0, bool process_interval=false)
std::shared_ptr< Frame > PreviewSeedMask(std::shared_ptr< Frame > frame)
void SetJson(const std::string value)
static std::string ValidateONNXModel(std::string modelPath)
CVObjectMask(std::string processInfoJson, ProcessingController &processingController)
void SetJsonValue(const Json::Value root)
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.
Definition: AnimatedCurve.h:24
std::vector< uint32_t > rle
Definition: CVObjectMask.h:40
std::vector< cv::Rect_< float > > positiveRects
Definition: CVObjectMask.h:48
std::vector< cv::Point2f > positivePoints
Definition: CVObjectMask.h:46
std::vector< cv::Rect_< float > > negativeRects
Definition: CVObjectMask.h:49
std::vector< cv::Point2f > negativePoints
Definition: CVObjectMask.h:47