Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

OCR With Tesseract

guides/examples/tesseract_ocr.livemd

OCR With Tesseract

Mix.install([
  {:kino, "~> 0.9"},
  {:deno_ex, "~> 0.2"}
])

Introduction

This Livebook shows you how you can use the Deno Terreract wrapper in order to perform optical character recognition (OCR for short) on images. Use the Kino input form to process your images and take it for a test drive!

Code

form = Kino.Control.form([file: Kino.Input.file("File")], submit: "Process image")
typescript_code = fn path ->
  {
    :stdin,
    """
    import { recognize } from "https://deno.land/x/tesseract/mod.ts";

    const output = await recognize("#{path}");
    console.log(output);
    """
  }
end

frame = Kino.Frame.new()

form
|> Kino.Control.stream()
|> Kino.listen(fn event ->
  path = Kino.Input.file_path(event.data.file.file_ref)

  image = path |> File.read!() |> Kino.Image.new(:png)

  {:ok, content} =
    path
    |> typescript_code.()
    |> DenoEx.run([], allow_run: true)

  ocr_result = String.trim(content) |> Kino.Markdown.new()

  render_result = Kino.Layout.grid([image, ocr_result], columns: 2)
  Kino.Frame.render(frame, render_result)
end)

frame