← All procedures

ocr.image

Read the text of an image with a local OCR engine (tesseract), with the confidence and the box of each line and word

CLI
Package
@procedures/ocr · README
Path
["ocr","image"]
CLI
mark ocr image --file <string>
As data
{"$proc":["ocr","image"],"input":{}}

Use it

In TypeScript

import { Client, LocalTransport, PROCEDURE_REGISTRY } from "@procedures/client";
import "@procedures/ocr/register"; // registers its procedures

const client = new Client(new LocalTransport({ registry: PROCEDURE_REGISTRY }));
const result = await client.exec(["ocr","image"], { file: "<file>" });
console.log(JSON.stringify(result));

From the command line

$ mark ocr image --file <string>

As data, in a program

{ "$proc": ["ocr", "image"], "input": { "file": "<file>" } }

Input

FieldTypeRequiredDefaultDescription
filestringyesThe path of the image file, for example a PNG, JPEG or TIFF file
dpiintegernoThe resolution of the image, when the file does not tell it
languagestringnoThe language of the text (default: eng)
psmintegernoThe page segmentation mode of tesseract, from 0 to 13
timeoutintegernoThe time limit of each program, in milliseconds (default: 120000)
uprightbooleannoWhen true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page (default: false)

Output

FieldTypeRequiredDefaultDescription
enginestringyesThe OCR engine
orientationobjectnoThe orientation that OCR found, when the input has upright
unit"px" | "pt"yesThe unit of the boxes: px is a pixel of the image, pt is a point of the PDF page
widthnumberyesThe width of the page or of the image
heightnumberyesThe height of the page or of the image
textstringyesThe text of the lines, with one line end between each two lines
confidencenumberyesThe mean confidence of all words, from 0 to 1
linesobject[]yes
JSON Schemas
{
  "input": {
    "type": "object",
    "properties": {
      "file": {
        "type": "string",
        "minLength": 1,
        "description": "The path of the image file, for example a PNG, JPEG or TIFF file"
      },
      "dpi": {
        "description": "The resolution of the image, when the file does not tell it",
        "type": "integer",
        "minimum": 1,
        "maximum": 4800
      },
      "language": {
        "description": "The language of the text (default: eng)",
        "type": "string",
        "pattern": "^[A-Za-z0-9_][A-Za-z0-9_/]*(\\+[A-Za-z0-9_][A-Za-z0-9_/]*)*$"
      },
      "psm": {
        "description": "The page segmentation mode of tesseract, from 0 to 13",
        "type": "integer",
        "minimum": 0,
        "maximum": 13
      },
      "timeout": {
        "description": "The time limit of each program, in milliseconds (default: 120000)",
        "type": "integer",
        "exclusiveMinimum": 0,
        "maximum": 9007199254740991
      },
      "upright": {
        "description": "When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page (default: false)",
        "type": "boolean"
      }
    },
    "required": [
      "file"
    ]
  },
  "output": {
    "type": "object",
    "properties": {
      "engine": {
        "type": "string",
        "description": "The OCR engine"
      },
      "orientation": {
        "description": "The orientation that OCR found, when the input has upright",
        "type": "object",
        "properties": {
          "turn": {
            "anyOf": [
              {
                "type": "number",
                "const": 0
              },
              {
                "type": "number",
                "const": 90
              },
              {
                "type": "number",
                "const": 180
              },
              {
                "type": "number",
                "const": 270
              }
            ],
            "description": "The turn in the clock direction that makes the page upright, in degrees"
          },
          "confidence": {
            "type": "number",
            "minimum": 0,
            "description": "The confidence of the engine in the turn. For tesseract, a value below 2 is weak"
          }
        },
        "required": [
          "turn",
          "confidence"
        ],
        "additionalProperties": false
      },
      "unit": {
        "type": "string",
        "enum": [
          "px",
          "pt"
        ],
        "description": "The unit of the boxes: px is a pixel of the image, pt is a point of the PDF page"
      },
      "width": {
        "type": "number",
        "description": "The width of the page or of the image"
      },
      "height": {
        "type": "number",
        "description": "The height of the page or of the image"
      },
      "text": {
        "type": "string",
        "description": "The text of the lines, with one line end between each two lines"
      },
      "confidence": {
        "type": "number",
        "minimum": 0,
        "maximum": 1,
        "description": "The mean confidence of all words, from 0 to 1"
      },
      "lines": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "text": {
              "type": "string",
              "description": "The words of the line, with one space between each two words"
            },
            "confidence": {
              "type": "number",
              "minimum": 0,
              "maximum": 1,
              "description": "The mean confidence of the words, from 0 to 1"
            },
            "box": {
              "type": "object",
              "properties": {
                "x": {
                  "type": "number",
                  "description": "The left side of the box"
                },
                "y": {
                  "type": "number",
                  "description": "The top side of the box (the y axis points down)"
                },
                "w": {
                  "type": "number",
                  "description": "The width of the box"
                },
                "h": {
                  "type": "number",
                  "description": "The height of the box"
                }
              },
              "required": [
                "x",
                "y",
                "w",
                "h"
              ],
              "additionalProperties": false
            },
            "words": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "text": {
                    "type": "string",
                    "description": "The text of the word"
                  },
                  "confidence": {
                    "type": "number",
                    "minimum": 0,
                    "maximum": 1,
                    "description": "The confidence, from 0 to 1"
                  },
                  "box": {
                    "type": "object",
                    "properties": {
                      "x": {
                        "type": "number",
                        "description": "The left side of the box"
                      },
                      "y": {
                        "type": "number",
                        "description": "The top side of the box (the y axis points down)"
                      },
                      "w": {
                        "type": "number",
                        "description": "The width of the box"
                      },
                      "h": {
                        "type": "number",
                        "description": "The height of the box"
                      }
                    },
                    "required": [
                      "x",
                      "y",
                      "w",
                      "h"
                    ],
                    "additionalProperties": false
                  }
                },
                "required": [
                  "text",
                  "confidence",
                  "box"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "text",
            "confidence",
            "box",
            "words"
          ],
          "additionalProperties": false
        }
      }
    },
    "required": [
      "engine",
      "unit",
      "width",
      "height",
      "text",
      "confidence",
      "lines"
    ],
    "additionalProperties": false
  }
}