Vertext Edge TFLite Object Detection Bounding box is off the screen with React Native TFLite/Skia

I am using VertexAI to train for Object Detection. I have created by data set as per documentation. I am using 1000x1000px images for training data. Then I export the Edge ML model as a .tflite file and import to my project.

My React Native app uses a camera from RNVision Camera. I also installed the TF Lite package. The app is functional so it seems like it’s setup right. However, my bounding box on the screen is not what I expect, as the Skia bounding box is out of screen view as you can see:

Here is what I am doing with the frameProcessor:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const frameProcessor = useSkiaFrameProcessor(
frame => {
'worklet';
if (!modelFile?.runSync) {
return frame;
}
frame.render();
runAtTargetFps(30, () => {
// 1. Resize 4k Frame to match model reqs vision-camera-resize-plugin
const resized = resize(frame, {
scale: {
height: 512,
width: 512,
},
pixelFormat: 'rgb',
dataType: 'uint8',
});
const done = modelFile.runSync([resized]);
const [boundingBoxes, labels, scores] = done;
const top = boundingBoxes[0] as number;
const left = boundingBoxes[0 + 1] as number;
const bottom = boundingBoxes[0 + 2] as number;
const right = boundingBoxes[0 + 3] as number;
// Function to add a new position
function addPosition() {
const newPosition = { top, left, bottom, right };
// If the array is already at max length, remove the oldest item
if ((positions.value?.length || 0) >= maxLength) {
const copy = [...(positions.value || [])];
copy.shift();
positions.value = copy;
}
// Add the new position to the end of the array
positions.value = [...(positions.value || []), newPosition];
}
if (scores[0] >= 0.7) {
addPosition();
const position = calculateAveragePosition();
box.value = position;
labelText.value =
objectDetectMap[labels[0] as keyof typeof objectDetectMap];
} else {
box.value = null;
}
});
if (box.value) {
const rect = Skia.XYWHRect(
// without multiplier, box is tiny & off screen
box.value.left * frame.width,
box.value.top * frame.height,
(box.value.right - box.value.left) * frame.width,
(box.value.bottom - box.value.top) * frame.height
);
const rectPaint = Skia.Paint();
rectPaint.setStyle(PaintStyle.Stroke);
rectPaint.setStrokeWidth(20);
rectPaint.setColor(Skia.Color('red'));
frame.drawRect(rect, rectPaint);
if (font) {
const fontPaint = Skia.Paint();
fontPaint.setColor(Skia.Color('red'));
const text = Skia.TextBlob.MakeFromText(
labelText.value || 'Move Closer',
font
);
frame.drawTextBlob(
text,
box.value.left * frame.width,
box.value.top * frame.height,
fontPaint
);
}
}
},
[modelFile, box, calculateAveragePosition, sharedLoadingModel]
);
</code>
<code>const frameProcessor = useSkiaFrameProcessor( frame => { 'worklet'; if (!modelFile?.runSync) { return frame; } frame.render(); runAtTargetFps(30, () => { // 1. Resize 4k Frame to match model reqs vision-camera-resize-plugin const resized = resize(frame, { scale: { height: 512, width: 512, }, pixelFormat: 'rgb', dataType: 'uint8', }); const done = modelFile.runSync([resized]); const [boundingBoxes, labels, scores] = done; const top = boundingBoxes[0] as number; const left = boundingBoxes[0 + 1] as number; const bottom = boundingBoxes[0 + 2] as number; const right = boundingBoxes[0 + 3] as number; // Function to add a new position function addPosition() { const newPosition = { top, left, bottom, right }; // If the array is already at max length, remove the oldest item if ((positions.value?.length || 0) >= maxLength) { const copy = [...(positions.value || [])]; copy.shift(); positions.value = copy; } // Add the new position to the end of the array positions.value = [...(positions.value || []), newPosition]; } if (scores[0] >= 0.7) { addPosition(); const position = calculateAveragePosition(); box.value = position; labelText.value = objectDetectMap[labels[0] as keyof typeof objectDetectMap]; } else { box.value = null; } }); if (box.value) { const rect = Skia.XYWHRect( // without multiplier, box is tiny & off screen box.value.left * frame.width, box.value.top * frame.height, (box.value.right - box.value.left) * frame.width, (box.value.bottom - box.value.top) * frame.height ); const rectPaint = Skia.Paint(); rectPaint.setStyle(PaintStyle.Stroke); rectPaint.setStrokeWidth(20); rectPaint.setColor(Skia.Color('red')); frame.drawRect(rect, rectPaint); if (font) { const fontPaint = Skia.Paint(); fontPaint.setColor(Skia.Color('red')); const text = Skia.TextBlob.MakeFromText( labelText.value || 'Move Closer', font ); frame.drawTextBlob( text, box.value.left * frame.width, box.value.top * frame.height, fontPaint ); } } }, [modelFile, box, calculateAveragePosition, sharedLoadingModel] ); </code>
const frameProcessor = useSkiaFrameProcessor(
    frame => {
      'worklet';
      if (!modelFile?.runSync) {
        return frame;
      }

      frame.render();
      runAtTargetFps(30, () => {
        // 1. Resize 4k Frame to match model reqs vision-camera-resize-plugin
        const resized = resize(frame, {
          scale: {
            height: 512,
            width: 512,
          },
          pixelFormat: 'rgb',
          dataType: 'uint8',
        });

        const done = modelFile.runSync([resized]);
        const [boundingBoxes, labels, scores] = done;
        const top = boundingBoxes[0] as number;
        const left = boundingBoxes[0 + 1] as number;
        const bottom = boundingBoxes[0 + 2] as number;
        const right = boundingBoxes[0 + 3] as number;

        // Function to add a new position
        function addPosition() {
          const newPosition = { top, left, bottom, right };
          // If the array is already at max length, remove the oldest item
          if ((positions.value?.length || 0) >= maxLength) {
            const copy = [...(positions.value || [])];
            copy.shift();
            positions.value = copy;
          }

          // Add the new position to the end of the array
          positions.value = [...(positions.value || []), newPosition];
        }
        if (scores[0] >= 0.7) {
          addPosition();
          const position = calculateAveragePosition();
          box.value = position;
          labelText.value =
            objectDetectMap[labels[0] as keyof typeof objectDetectMap];
        } else {
          box.value = null;
        }
      });
      if (box.value) {
        const rect = Skia.XYWHRect(
 // without multiplier, box is tiny & off screen
          box.value.left * frame.width,
          box.value.top * frame.height,
          (box.value.right - box.value.left) * frame.width,
          (box.value.bottom - box.value.top) * frame.height
        );

        const rectPaint = Skia.Paint();
        rectPaint.setStyle(PaintStyle.Stroke);
        rectPaint.setStrokeWidth(20);
        rectPaint.setColor(Skia.Color('red'));
        frame.drawRect(rect, rectPaint);
        if (font) {
          const fontPaint = Skia.Paint();
          fontPaint.setColor(Skia.Color('red'));
          const text = Skia.TextBlob.MakeFromText(
            labelText.value || 'Move Closer',
            font
          );
          frame.drawTextBlob(
            text,
            box.value.left * frame.width,
            box.value.top * frame.height,
            fontPaint
          );
        }
      }
    },
    [modelFile, box, calculateAveragePosition, sharedLoadingModel]
  );

My frame is 1920×1080. I also tried a smaller resolution below the 1024 image max that is mentioned in the Vertex documentation, however with the resize plugin to a 512x512 RGB I didn’t think the frame itself matters?

What is wrong with my logic?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật