- 源码 316 文件收编为 rusto-rs/ 文件夹,随本仓库管理 - 移除内部 .git(补丁已存档 docs/rusto-rs-metal-patch-20260912.patch,上游可随时 re-clone) - target/ 与 docs/.yarn 缓存不入库;本地改动:ROCR_* 环境变量调优 + Metal 链接修复 + 预处理测试脚本
269 lines
8.4 KiB
Markdown
269 lines
8.4 KiB
Markdown
---
|
|
title: 'Panduan Cepat'
|
|
description: 'Jalankan deteksi OCR pertama Anda dengan RustO! di semua platform dalam waktu kurang dari 5 menit.'
|
|
navigation:
|
|
icon: 'i-ph-play'
|
|
seo:
|
|
title: 'Panduan Cepat - RustO!'
|
|
description: 'Jalankan deteksi OCR pertama Anda dengan RustO! di semua platform dalam waktu kurang dari 5 menit.'
|
|
---
|
|
|
|
Mulai gunakan RustO! di berbagai bahasa pemrograman dengan contoh ringkas yang siap disalin dan dijalankan di bawah ini.
|
|
|
|
## Contoh Gambar Dokumen
|
|
|
|
Sebagai gambaran masukan, kita menggunakan gambar struk pembayaran `receipt.jpg`:
|
|
|
|
```text
|
|
┌────────────────────────────────────────┐
|
|
│ KEDAI KOPI │
|
|
│ 2026-08-20 12:30 │
|
|
│ │
|
|
│ Menu Jumlah Harga │
|
|
│ ────────────────────────────────────── │
|
|
│ Espresso Double 1 Rp25.000 │
|
|
│ Croissant 2 Rp40.000 │
|
|
│ Flat White 1 Rp30.000 │
|
|
│ ────────────────────────────────────── │
|
|
│ Total Rp95.000 │
|
|
└────────────────────────────────────────┘
|
|
```
|
|
|
|
---
|
|
|
|
## 1. Rust
|
|
|
|
```rust
|
|
use rusto::{
|
|
DetectTextResult, ImageSource, InitializeConfig, OcrRunOptions,
|
|
OutputGranularity, RustO,
|
|
};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// 1. Inisialisasi mesin dengan preset bawaan PP-OCRv6
|
|
let config = InitializeConfig::ppv6("models/det.mnn", "models/rec.mnn", "models/dict.txt");
|
|
let mut ocr = RustO::initialize(config)?;
|
|
|
|
// 2. Jalankan deteksi teks terstruktur per baris
|
|
let image = ImageSource::Path("receipt.jpg".into());
|
|
let mut options = OcrRunOptions::default();
|
|
options.output = OutputGranularity::Lines;
|
|
|
|
let result = ocr.detect_text(&image, &options)?;
|
|
|
|
match result {
|
|
DetectTextResult::Structured(lines) => {
|
|
println!("--- Hasil Teks Terstruktur ({} baris) ---", lines.len());
|
|
for line in lines {
|
|
println!(
|
|
"[{:5.1}, {:5.1}, {:5.1}, {:5.1}] (Skor: {:.2}) {}",
|
|
line.frame.left, line.frame.top, line.frame.width, line.frame.height,
|
|
line.score, line.text
|
|
);
|
|
}
|
|
}
|
|
DetectTextResult::Spatial(text) => println!("{text}"),
|
|
}
|
|
|
|
// 3. Jalankan rekonstruksi tata letak spasial
|
|
options.output = OutputGranularity::Spatial;
|
|
options.line_y_threshold = Some(0.5);
|
|
options.word_x_threshold = Some(0.4);
|
|
|
|
if let DetectTextResult::Spatial(spatial_text) = ocr.detect_text(&image, &options)? {
|
|
println!("\n--- Tata Letak Spasial Hasil Rekonstruksi ---\n{spatial_text}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
### Contoh Keluaran (Output)
|
|
|
|
```text
|
|
--- Hasil Teks Terstruktur (7 baris) ---
|
|
[ 95.0, 30.0, 180.0, 25.0] (Skor: 0.99) KEDAI KOPI
|
|
[ 40.0, 70.0, 100.0, 20.0] (Skor: 0.98) 2026-08-20
|
|
[260.0, 70.0, 60.0, 20.0] (Skor: 0.97) 12:30
|
|
[ 40.0, 110.0, 280.0, 20.0] (Skor: 0.96) Menu Jumlah Harga
|
|
[ 40.0, 145.0, 280.0, 20.0] (Skor: 0.98) Espresso Double 1 Rp25.000
|
|
[ 40.0, 175.0, 280.0, 20.0] (Skor: 0.97) Croissant 2 Rp40.000
|
|
[ 40.0, 205.0, 280.0, 20.0] (Skor: 0.98) Flat White 1 Rp30.000
|
|
[ 40.0, 245.0, 280.0, 20.0] (Skor: 0.99) Total Rp95.000
|
|
|
|
--- Tata Letak Spasial Hasil Rekonstruksi ---
|
|
KEDAI KOPI
|
|
2026-08-20 12:30
|
|
Menu Jumlah Harga
|
|
Espresso Double 1 Rp25.000
|
|
Croissant 2 Rp40.000
|
|
Flat White 1 Rp30.000
|
|
Total Rp95.000
|
|
```
|
|
|
|
---
|
|
|
|
## 2. .NET / C#
|
|
|
|
```csharp
|
|
using System;
|
|
using RustODotnet;
|
|
|
|
// 1. Inisialisasi mesin (memuat model bawaan PP-OCRv6 secara otomatis)
|
|
using var ocr = RustO.Initialize();
|
|
|
|
// 2. Deteksi teks terstruktur per baris
|
|
var imageSource = new UriImageSource("receipt.jpg");
|
|
var options = new OcrRunOptions { Output = OutputGranularity.Lines };
|
|
|
|
var result = ocr.DetectText(imageSource, options);
|
|
if (result is StructuredDetectTextResult structured)
|
|
{
|
|
foreach (var item in structured.Items)
|
|
{
|
|
Console.WriteLine($"[{item.Frame.Left:F0}, {item.Frame.Top:F0}] ({item.Score:P0}) {item.Text}");
|
|
}
|
|
}
|
|
|
|
// 3. Rekonstruksi tata letak spasial
|
|
var spatialOptions = new OcrRunOptions { Output = OutputGranularity.Spatial };
|
|
var spatialResult = ocr.DetectText(imageSource, spatialOptions);
|
|
if (spatialResult is SpatialDetectTextResult spatial)
|
|
{
|
|
Console.WriteLine("\n--- Tata Letak Spasial ---");
|
|
Console.WriteLine(spatial.Text);
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 3. React Native
|
|
|
|
```typescript
|
|
import React, { useEffect, useState } from 'react';
|
|
import { Button, SafeAreaView, ScrollView, Text, View } from 'react-native';
|
|
import { detectText, initialize, TextResult } from 'react-native-rusto';
|
|
|
|
export default function QuickScanApp() {
|
|
const [lines, setLines] = useState<TextResult[]>([]);
|
|
const [spatialText, setSpatialText] = useState<string>('');
|
|
|
|
useEffect(() => {
|
|
// Inisialisasi model bawaan satu kali saat startup
|
|
initialize();
|
|
}, []);
|
|
|
|
const handleScanStructured = async () => {
|
|
const results = await detectText(
|
|
{ uri: 'file:///path/to/receipt.jpg' },
|
|
{ output: 'lines' }
|
|
);
|
|
setLines(results as TextResult[]);
|
|
};
|
|
|
|
const handleScanSpatial = async () => {
|
|
const text = await detectText(
|
|
{ uri: 'file:///path/to/receipt.jpg' },
|
|
{ output: 'spatial', lineYThreshold: 0.5, wordXThreshold: 0.4 }
|
|
);
|
|
setSpatialText(text as string);
|
|
};
|
|
|
|
return (
|
|
<SafeAreaView style={{ flex: 1, padding: 16 }}>
|
|
<Button title="Pindai Baris Teks" onPress={handleScanStructured} />
|
|
<Button title="Pindai Tata Letak Spasial" onPress={handleScanSpatial} />
|
|
|
|
<ScrollView style={{ marginTop: 16 }}>
|
|
{spatialText ? (
|
|
<Text style={{ fontFamily: 'monospace' }}>{spatialText}</Text>
|
|
) : (
|
|
lines.map((line, idx) => (
|
|
<Text key={idx}>{line.text} ({Math.round(line.score * 100)}%)</Text>
|
|
))
|
|
)}
|
|
</ScrollView>
|
|
</SafeAreaView>
|
|
);
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 4. iOS (Swift)
|
|
|
|
```swift
|
|
import Foundation
|
|
import RustO
|
|
|
|
func runQuickOcr() {
|
|
do {
|
|
// Inisialisasi model bawaan
|
|
let ocr = try RustO.initialize()
|
|
|
|
// Deteksi baris terstruktur
|
|
let structuredResult = try ocr.detectText(.uri("receipt.jpg"), options: OcrRunOptions(output: .lines))
|
|
if case .structured(let items) = structuredResult {
|
|
for item in items {
|
|
print("[\(item.frame.left), \(item.frame.top)] (\(item.score)) \(item.text)")
|
|
}
|
|
}
|
|
|
|
// Rekonstruksi spasial
|
|
let spatialResult = try ocr.detectText(.uri("receipt.jpg"), options: OcrRunOptions(output: .spatial))
|
|
if case .spatial(let text) = spatialResult {
|
|
print("\nDokumen Hasil Rekonstruksi:\n\(text)")
|
|
}
|
|
} catch {
|
|
print("Kesalahan OCR: \(error)")
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 5. Android (Kotlin)
|
|
|
|
```kotlin
|
|
package com.example.ocr
|
|
|
|
import android.content.Context
|
|
import com.byrizki.rusto.*
|
|
|
|
fun processReceipt(context: Context, imageUri: String) {
|
|
// Mengekstrak model bawaan secara otomatis dari aset AAR
|
|
RustO.initialize(context).use { ocr ->
|
|
val source = ImageSource.Uri(imageUri)
|
|
|
|
// 1. Deteksi Baris Terstruktur
|
|
val structured = ocr.detectText(source, OcrRunOptions(output = OutputGranularity.LINES))
|
|
if (structured is DetectTextResult.Structured) {
|
|
structured.items.forEach { line ->
|
|
println("${line.text} (Skor: ${line.score})")
|
|
}
|
|
}
|
|
|
|
// 2. Tata Letak Spasial
|
|
val spatial = ocr.detectText(source, OcrRunOptions(output = OutputGranularity.SPATIAL))
|
|
if (spatial is DetectTextResult.Spatial) {
|
|
println("Tata Letak Spasial:\n${spatial.text}")
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 6. Antarmuka Baris Perintah (CLI)
|
|
|
|
```bash
|
|
# 1. Keluaran JSON terstruktur dengan koordinat poligon
|
|
rusto --det-model det.mnn --rec-model rec.mnn --dict dict.txt --format json receipt.jpg
|
|
|
|
# 2. Keluaran tata letak teks spasial terformat
|
|
rusto --det-model det.mnn --rec-model rec.mnn --dict dict.txt --format text-ordered receipt.jpg
|
|
|
|
# 3. Keluaran nilai dipisahkan tab (TSV)
|
|
rusto --det-model det.mnn --rec-model rec.mnn --dict dict.txt --format tsv receipt.jpg
|
|
```
|