99
1010namespace rnexecutorch {
1111
12- ImageSegmentation::ImageSegmentation (const std::string &modelSource,
13- jsi::Runtime *runtime)
14- : BaseModel(modelSource, runtime) {
12+ ImageSegmentation::ImageSegmentation (
13+ const std::string &modelSource,
14+ std::shared_ptr<react::CallInvoker> callInvoker)
15+ : BaseModel(modelSource, callInvoker) {
1516
16- std::vector<int32_t > modelInputShape = getInputShape ();
17+ std::vector<int32_t > modelInputShape = getInputShape ()[ 0 ] ;
1718 modelImageSize = cv::Size (modelInputShape[modelInputShape.size () - 1 ],
1819 modelInputShape[modelInputShape.size () - 2 ]);
1920 numModelPixels = modelImageSize.area ();
2021}
2122
22- jsi::Value
23+ std::unique_ptr< jsi::Object>
2324ImageSegmentation::forward (std::string imageSource,
2425 std::set<std::string, std::less<>> classesOfInterest,
2526 bool resize) {
@@ -36,7 +37,20 @@ ImageSegmentation::forward(std::string imageSource,
3637 classesOfInterest, resize);
3738}
3839
39- jsi::Value ImageSegmentation::postprocess (
40+ std::pair<TensorPtr, cv::Size>
41+ ImageSegmentation::preprocess (const std::string &imageSource) {
42+ cv::Mat input = imageprocessing::readImage (imageSource);
43+ cv::Size inputSize = input.size ();
44+
45+ cv::resize (input, input, modelImageSize);
46+
47+ std::vector<float > inputVector = imageprocessing::colorMatToVector (input);
48+ return {
49+ executorch::extension::make_tensor_ptr (getInputShape ()[0 ], inputVector),
50+ inputSize};
51+ }
52+
53+ std::unique_ptr<jsi::Object> ImageSegmentation::postprocess (
4054 const Tensor &tensor, cv::Size originalSize,
4155 std::set<std::string, std::less<>> classesOfInterest, bool resize) {
4256
@@ -84,11 +98,11 @@ jsi::Value ImageSegmentation::postprocess(
8498 reinterpret_cast <int32_t *>(argmax->data ())[pixel] = maxInd;
8599 }
86100
87- std::unordered_map <std::string_view, std::shared_ptr<OwningArrayBuffer>>
88- buffersToReturn ;
101+ auto buffersToReturn = std::make_shared <std::unordered_map<
102+ std::string_view, std::shared_ptr<OwningArrayBuffer>>>() ;
89103 for (std::size_t cl = 0 ; cl < numClasses; ++cl) {
90104 if (classesOfInterest.contains (deeplabv3_resnet50_labels[cl])) {
91- buffersToReturn[deeplabv3_resnet50_labels[cl]] = resultClasses[cl];
105+ (* buffersToReturn) [deeplabv3_resnet50_labels[cl]] = resultClasses[cl];
92106 }
93107 }
94108
@@ -102,7 +116,7 @@ jsi::Value ImageSegmentation::postprocess(
102116 std::memcpy (argmax->data (), argmaxMat.data ,
103117 originalSize.area () * sizeof (int32_t ));
104118
105- for (auto &[label, arrayBuffer] : buffersToReturn) {
119+ for (auto &[label, arrayBuffer] : * buffersToReturn) {
106120 cv::Mat classMat (modelImageSize, CV_32FC1, arrayBuffer->data ());
107121 cv::resize (classMat, classMat, originalSize);
108122 arrayBuffer = std::make_shared<OwningArrayBuffer>(originalSize.area () *
@@ -114,53 +128,41 @@ jsi::Value ImageSegmentation::postprocess(
114128 return populateDictionary (argmax, buffersToReturn);
115129}
116130
117- jsi::Value ImageSegmentation::populateDictionary (
131+ std::unique_ptr< jsi::Object> ImageSegmentation::populateDictionary (
118132 std::shared_ptr<OwningArrayBuffer> argmax,
119- std::unordered_map<std::string_view, std::shared_ptr<OwningArrayBuffer>>
133+ std::shared_ptr<std::unordered_map<std::string_view,
134+ std::shared_ptr<OwningArrayBuffer>>>
120135 classesToOutput) {
121- jsi::Object dict (*runtime);
122-
123- auto argmaxArrayBuffer = jsi::ArrayBuffer (*runtime, argmax);
124-
125- auto int32ArrayCtor =
126- runtime->global ().getPropertyAsFunction (*runtime, " Int32Array" );
127- auto int32Array =
128- int32ArrayCtor.callAsConstructor (*runtime, argmaxArrayBuffer)
129- .getObject (*runtime);
130- dict.setProperty (*runtime, " ARGMAX" , int32Array);
131-
132- std::size_t dictIndex = 1 ;
133- for (auto &[classLabel, owningBuffer] : classesToOutput) {
134- auto classArrayBuffer = jsi::ArrayBuffer (*runtime, owningBuffer);
135-
136- auto float32ArrayCtor =
137- runtime->global ().getPropertyAsFunction (*runtime, " Float32Array" );
138- auto float32Array =
139- float32ArrayCtor.callAsConstructor (*runtime, classArrayBuffer)
140- .getObject (*runtime);
141-
142- dict.setProperty (*runtime,
143- jsi::String::createFromAscii (*runtime, classLabel.data ()),
144- float32Array);
145- }
146- return dict;
147- }
148-
149- std::pair<TensorPtr, cv::Size>
150- ImageSegmentation::preprocess (const std::string &imageSource) {
151- cv::Mat input = imageprocessing::readImage (imageSource);
152- cv::Size inputSize = input.size ();
153-
154- std::vector<int32_t > modelInputShape = getInputShape ();
155- cv::Size modelImageSize =
156- cv::Size (modelInputShape[modelInputShape.size () - 1 ],
157- modelInputShape[modelInputShape.size () - 2 ]);
158-
159- cv::resize (input, input, modelImageSize);
160-
161- std::vector<float > inputVector = imageprocessing::colorMatToVector (input);
162- return {executorch::extension::make_tensor_ptr (modelInputShape, inputVector),
163- inputSize};
136+ std::unique_ptr<jsi::Object> dictPtr;
137+
138+ callInvoker->invokeSync (
139+ [argmax, classesToOutput, &dictPtr](jsi::Runtime &runtime) {
140+ dictPtr = std::make_unique<jsi::Object>(runtime);
141+ auto argmaxArrayBuffer = jsi::ArrayBuffer (runtime, argmax);
142+
143+ auto int32ArrayCtor =
144+ runtime.global ().getPropertyAsFunction (runtime, " Int32Array" );
145+ auto int32Array =
146+ int32ArrayCtor.callAsConstructor (runtime, argmaxArrayBuffer)
147+ .getObject (runtime);
148+ dictPtr->setProperty (runtime, " ARGMAX" , int32Array);
149+
150+ for (auto &[classLabel, owningBuffer] : *classesToOutput) {
151+ auto classArrayBuffer = jsi::ArrayBuffer (runtime, owningBuffer);
152+
153+ auto float32ArrayCtor =
154+ runtime.global ().getPropertyAsFunction (runtime, " Float32Array" );
155+ auto float32Array =
156+ float32ArrayCtor.callAsConstructor (runtime, classArrayBuffer)
157+ .getObject (runtime);
158+
159+ dictPtr->setProperty (
160+ runtime, jsi::String::createFromAscii (runtime, classLabel.data ()),
161+ float32Array);
162+ }
163+ });
164+
165+ return dictPtr;
164166}
165167
166168} // namespace rnexecutorch
0 commit comments