@@ -57,7 +57,8 @@ TensorPtr BaseInstanceSegmentation::buildInputTensor(const cv::Mat &image) {
5757std::vector<types::Instance> BaseInstanceSegmentation::runInference (
5858 const cv::Mat &image, double confidenceThreshold, double iouThreshold,
5959 int32_t maxInstances, const std::vector<int32_t > &classIndices,
60- bool returnMaskAtOriginalResolution, const std::string &methodName) {
60+ bool returnMaskAtOriginalResolution, const std::string &methodName,
61+ bool useWeightedNms) {
6162
6263 std::scoped_lock lock (inference_mutex_);
6364
@@ -86,34 +87,37 @@ std::vector<types::Instance> BaseInstanceSegmentation::runInference(
8687 auto instances = collectInstances (
8788 forwardResult.get (), originalSize, modelInputSize, confidenceThreshold,
8889 classIndices, returnMaskAtOriginalResolution);
89- return finalizeInstances (std::move (instances), iouThreshold, maxInstances);
90+ return finalizeInstances (std::move (instances), iouThreshold, maxInstances,
91+ useWeightedNms);
9092}
9193
9294std::vector<types::Instance> BaseInstanceSegmentation::generateFromString (
9395 std::string imageSource, double confidenceThreshold, double iouThreshold,
9496 int32_t maxInstances, std::vector<int32_t > classIndices,
95- bool returnMaskAtOriginalResolution, std::string methodName) {
97+ bool returnMaskAtOriginalResolution, std::string methodName,
98+ bool useWeightedNms) {
9699
97100 cv::Mat imageBGR = image_processing::readImage (imageSource);
98101 cv::Mat imageRGB;
99102 cv::cvtColor (imageBGR, imageRGB, cv::COLOR_BGR2RGB );
100103
101104 return runInference (imageRGB, confidenceThreshold, iouThreshold, maxInstances,
102- classIndices, returnMaskAtOriginalResolution, methodName);
105+ classIndices, returnMaskAtOriginalResolution, methodName,
106+ useWeightedNms);
103107}
104108
105109std::vector<types::Instance> BaseInstanceSegmentation::generateFromFrame (
106110 jsi::Runtime &runtime, const jsi::Value &frameData,
107111 double confidenceThreshold, double iouThreshold, int32_t maxInstances,
108112 std::vector<int32_t > classIndices, bool returnMaskAtOriginalResolution,
109- std::string methodName) {
113+ std::string methodName, bool useWeightedNms ) {
110114
111115 auto orient = ::rnexecutorch::utils::readFrameOrientation (runtime, frameData);
112116 cv::Mat frame = extractFromFrame (runtime, frameData);
113117 cv::Mat rotated = utils::rotateFrameForModel (frame, orient);
114- auto instances =
115- runInference ( rotated, confidenceThreshold, iouThreshold, maxInstances,
116- classIndices, returnMaskAtOriginalResolution, methodName);
118+ auto instances = runInference (
119+ rotated, confidenceThreshold, iouThreshold, maxInstances, classIndices ,
120+ returnMaskAtOriginalResolution, methodName, useWeightedNms );
117121 for (auto &inst : instances) {
118122 utils::inverseRotateBbox (inst.bbox , orient, rotated.size ());
119123 // Inverse-rotate the mask to match the screen orientation
@@ -131,11 +135,13 @@ std::vector<types::Instance> BaseInstanceSegmentation::generateFromFrame(
131135std::vector<types::Instance> BaseInstanceSegmentation::generateFromPixels (
132136 JSTensorViewIn tensorView, double confidenceThreshold, double iouThreshold,
133137 int32_t maxInstances, std::vector<int32_t > classIndices,
134- bool returnMaskAtOriginalResolution, std::string methodName) {
138+ bool returnMaskAtOriginalResolution, std::string methodName,
139+ bool useWeightedNms) {
135140
136141 cv::Mat image = extractFromPixels (tensorView);
137142 return runInference (image, confidenceThreshold, iouThreshold, maxInstances,
138- classIndices, returnMaskAtOriginalResolution, methodName);
143+ classIndices, returnMaskAtOriginalResolution, methodName,
144+ useWeightedNms);
139145}
140146
141147std::tuple<utils::computer_vision::BBox, float , int32_t >
@@ -296,11 +302,14 @@ void BaseInstanceSegmentation::ensureMethodLoaded(
296302
297303std::vector<types::Instance> BaseInstanceSegmentation::finalizeInstances (
298304 std::vector<types::Instance> instances, double iouThreshold,
299- int32_t maxInstances) const {
305+ int32_t maxInstances, bool useWeightedNms ) const {
300306
301307 if (applyNMS_) {
302- instances =
303- utils::computer_vision::nonMaxSuppression (instances, iouThreshold);
308+ instances = useWeightedNms
309+ ? utils::computer_vision::weightedNonMaxSuppression (
310+ instances, iouThreshold)
311+ : utils::computer_vision::nonMaxSuppression (instances,
312+ iouThreshold);
304313 }
305314
306315 if (std::cmp_greater (instances.size (), maxInstances)) {
0 commit comments