Skip to content

Commit 456cd83

Browse files
feat: apply orientation handling to instance segmentation
1 parent a172209 commit 456cd83

4 files changed

Lines changed: 33 additions & 4 deletions

File tree

apps/computer-vision/components/vision_camera/tasks/InstanceSegmentationTask.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export default function InstanceSegmentationTask({
9696
const frameOutput = useFrameOutput({
9797
pixelFormat: 'rgb',
9898
dropFramesWhileBusy: true,
99+
enablePreviewSizedOutputBuffers: true,
99100
onFrame: useCallback(
100101
(frame: Frame) => {
101102
'worklet';
@@ -105,9 +106,10 @@ export default function InstanceSegmentationTask({
105106
}
106107
try {
107108
if (!instSegRof) return;
109+
const isFrontCamera = cameraPositionSync.getDirty() === 'front';
108110
const iw = frame.width > frame.height ? frame.height : frame.width;
109111
const ih = frame.width > frame.height ? frame.width : frame.height;
110-
const result = instSegRof(frame, {
112+
const result = instSegRof(frame, isFrontCamera, {
111113
confidenceThreshold: 0.5,
112114
iouThreshold: 0.5,
113115
maxInstances: 5,
@@ -129,7 +131,13 @@ export default function InstanceSegmentationTask({
129131
frame.dispose();
130132
}
131133
},
132-
[instSegRof, frameKillSwitch, updateInstances, activeModel]
134+
[
135+
instSegRof,
136+
frameKillSwitch,
137+
updateInstances,
138+
activeModel,
139+
cameraPositionSync,
140+
]
133141
),
134142
});
135143

packages/react-native-executorch/common/rnexecutorch/models/instance_segmentation/BaseInstanceSegmentation.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <rnexecutorch/ErrorCodes.h>
77
#include <rnexecutorch/Log.h>
88
#include <rnexecutorch/data_processing/ImageProcessing.h>
9+
#include <rnexecutorch/utils/FrameProcessor.h>
10+
#include <rnexecutorch/utils/FrameTransform.h>
911
#include <rnexecutorch/utils/computer_vision/Processing.h>
1012

1113
namespace rnexecutorch::models::instance_segmentation {
@@ -112,9 +114,23 @@ std::vector<types::Instance> BaseInstanceSegmentation::generateFromFrame(
112114
std::vector<int32_t> classIndices, bool returnMaskAtOriginalResolution,
113115
std::string methodName) {
114116

117+
auto orient = ::rnexecutorch::utils::readFrameOrientation(runtime, frameData);
115118
cv::Mat frame = extractFromFrame(runtime, frameData);
116-
return runInference(frame, confidenceThreshold, iouThreshold, maxInstances,
117-
classIndices, returnMaskAtOriginalResolution, methodName);
119+
cv::Mat rotated = utils::rotateFrameForModel(frame, orient);
120+
auto instances =
121+
runInference(rotated, confidenceThreshold, iouThreshold, maxInstances,
122+
classIndices, returnMaskAtOriginalResolution, methodName);
123+
for (auto &inst : instances) {
124+
utils::inverseRotateBbox(inst.bbox, orient, rotated.size());
125+
// Inverse-rotate the mask to match the screen orientation
126+
cv::Mat maskMat(inst.maskHeight, inst.maskWidth, CV_8UC1, inst.mask->data());
127+
cv::Mat invMask = utils::inverseRotateMat(maskMat, orient);
128+
inst.mask = std::make_shared<OwningArrayBuffer>(
129+
invMask.data, static_cast<size_t>(invMask.total()));
130+
inst.maskWidth = invMask.cols;
131+
inst.maskHeight = invMask.rows;
132+
}
133+
return instances;
118134
}
119135

120136
std::vector<types::Instance> BaseInstanceSegmentation::generateFromPixels(

packages/react-native-executorch/src/modules/computer_vision/InstanceSegmentationModule.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export class InstanceSegmentationModule<
289289
override get runOnFrame():
290290
| ((
291291
frame: Frame,
292+
isFrontCamera: boolean,
292293
options?: InstanceSegmentationOptions<ResolveLabels<T>>
293294
) => SegmentedInstance<ResolveLabels<T>>[])
294295
| null {
@@ -315,6 +316,7 @@ export class InstanceSegmentationModule<
315316

316317
return (
317318
frame: Frame,
319+
isFrontCamera: boolean,
318320
options?: InstanceSegmentationOptions<ResolveLabels<T>>
319321
): SegmentedInstance<ResolveLabels<T>>[] => {
320322
'worklet';
@@ -340,6 +342,7 @@ export class InstanceSegmentationModule<
340342

341343
const nativeResults = baseRunOnFrame(
342344
frame,
345+
isFrontCamera,
343346
confidenceThreshold,
344347
iouThreshold,
345348
maxInstances,

packages/react-native-executorch/src/types/instanceSegmentation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,14 @@ export interface InstanceSegmentationType<L extends LabelEnum> {
211211
* Available after model is loaded (`isReady: true`).
212212
*
213213
* @param frame - VisionCamera Frame object
214+
* @param isFrontCamera - Whether the front camera is active (for mirroring correction).
214215
* @param options - Optional configuration for the segmentation process.
215216
* @returns Array of SegmentedInstance objects representing detected items in the frame.
216217
*/
217218
runOnFrame:
218219
| ((
219220
frame: Frame,
221+
isFrontCamera: boolean,
220222
options?: InstanceSegmentationOptions<L>
221223
) => SegmentedInstance<L>[])
222224
| null;

0 commit comments

Comments
 (0)