Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ragaeeb/kokokor/llms.txt

Use this file to discover all available pages before exploring further.

Overview

Converts bounding box coordinates from array format to object format. Transforms [x1, y1, x2, y2] coordinates to {x, y, width, height} format, commonly needed when integrating with OCR libraries like Surya that use matrix-style bounding boxes.

Signature

mapMatrixToBoundingBox(
    box: [number, number, number, number]
): BoundingBox

Parameters

box
[number, number, number, number]
required
Array containing [x1, y1, x2, y2] coordinates where:
  • x1, y1 is the top-left corner
  • x2, y2 is the bottom-right corner

Returns

BoundingBox - Bounding box object with x, y, width, and height properties
{
  x: number;      // x1 (left edge)
  y: number;      // y1 (top edge)
  width: number;  // x2 - x1
  height: number; // y2 - y1
}

What It Does

Converts from coordinate-pair format (used by many OCR engines) to the width/height format used throughout Kokokor’s API.

Usage Example

Surya OCR Integration

import { mapMatrixToBoundingBox } from 'kokokor';

// Surya OCR output format
const suryaResult = {
  text_lines: [
    {
      bbox: [100, 100, 400, 120], // [x1, y1, x2, y2] format
      text: 'Text from Surya OCR',
    },
    {
      bbox: [100, 130, 450, 150],
      text: 'Another line of text',
    },
  ],
};

// Convert to Kokokor observations
const observations = suryaResult.text_lines.map((line) => ({
  text: line.text,
  bbox: mapMatrixToBoundingBox(line.bbox as [number, number, number, number]),
}));

// Result:
// [
//   {
//     text: 'Text from Surya OCR',
//     bbox: { x: 100, y: 100, width: 300, height: 20 }
//   },
//   {
//     text: 'Another line of text',
//     bbox: { x: 100, y: 130, width: 350, height: 20 }
//   }
// ]

Basic Conversion

import { mapMatrixToBoundingBox } from 'kokokor';

const matrixBox = [100, 200, 500, 250]; // [x1, y1, x2, y2]
const bbox = mapMatrixToBoundingBox(matrixBox);

// Result: { x: 100, y: 200, width: 400, height: 50 }

Build docs developers (and LLMs) love