How To
Use Golang
Using RustO! pure-Rust OCR engine in Go applications via Cgo.
The github.com/byrizki/rusto-rs/packages/go module provides native Go bindings via Cgo. The module embeds dynamic linker runtime search paths (-Wl,-rpath), allowing Go binaries and unit tests to locate librusto without manual LD_LIBRARY_PATH configuration.
Installation
go get github.com/byrizki/rusto-rs/packages/go
Ensure librusto.so (Linux), librusto.dylib (macOS), or rusto.dll (Windows) is built and located in the library search path.
1. Engine Initialization
Initialize the engine by passing a JSON configuration string:
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/byrizki/rusto-rs/packages/go"
)
func main() {
config := map[string]interface{}{
"detection": map[string]string{
"model_path": "models/PPOCR_v6/det.rten",
},
"recognition": map[string]string{
"model_path": "models/PPOCR_v6/rec.rten",
"dict_path": "models/PPOCR_v6/dict.txt",
},
}
configBytes, _ := json.Marshal(config)
engine, err := rusto.New(string(configBytes))
if err != nil {
log.Fatalf("Failed to initialize RustO: %v", err)
}
defer engine.Close()
}
2. Text Recognition
From Filesystem Path
results, err := engine.DetectTextFile("receipt.jpg", "{}")
if err != nil {
log.Fatalf("Detection failed: %v", err)
}
for i, line := range results {
fmt.Printf("[%d] (Score: %0.2f) %s\n", i+1, line.Score, line.Text)
fmt.Printf(" Frame: left=%0.1f, top=%0.1f, w=%0.1f, h=%0.1f\n",
line.Frame.Left, line.Frame.Top, line.Frame.Width, line.Frame.Height)
}
From In-Memory Bytes Buffer
import "os"
imageBytes, err := os.ReadFile("document.png")
if err != nil {
log.Fatal(err)
}
results, err := engine.DetectTextData(imageBytes, "{}")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Detected %d text segments\n", len(results))
3. Spatial Layout Reconstruction
To reconstruct formatted 2D text layouts:
spatialText, err := engine.DetectTextFileSpatial("invoice.png", `{"line_y_threshold": 0.5}`)
if err != nil {
log.Fatal(err)
}
fmt.Println("--- Spatial Output ---")
fmt.Println(spatialText)