OCR

OCR extracts text from an image in the agent’s data directory:

def runTask(): Unit receives stdout, ocr =
  match ocr.text("scan.png")
  case Ok(text)   => println: text
  case Err(error) => println: error

Ok("") means the image contained no recognized text. Err means the image could not be read or the OCR engine was unavailable.

OCR is a separate capability from Image. An application can allow pixel transformations without allowing content extraction, and it can replace the OCR engine independently.

Scanned PDFs

Render the page first, then recognize it:

def runTask(): Unit receives stdout, fs, pdfReader, ocr =
  match fs.openPDF("scan.pdf")
  case Err(error) => println: error
  case Ok(pdf) =>
    match pdf.pageImage(1, "page-1.png")
    case Err(error) => println: error
    case Ok(_) =>
      match ocr.text("page-1.png")
      case Ok(text)   => println: text
      case Err(error) => println: error

    pdf.close()

Harpe provides two OCR engines: TesseractOcr and RapidOcr. Change the binding in SandboxRuntime.jo to select another implementation.

Interface reference

interface OCR
  def text(src: String): Result[String, String]
end

param ocr: OCR