YotsukaD API Guide
This page explains how to use the YotsukaD API from EdgeOCRExtensions to detect labels and optionally obtain perspective-corrected images.
See the Swift API Reference / Kotlin API Reference for detailed API specifications.
YotsukaD API Overview
- Create a
YotsukaDinstance. - Call
load()once. - Pass a
UIImagefor still images, or aCMSampleBuffer/ImageProxyfor camera frames. - Retrieve the score, corner coordinates, and corrected image (
UIImageorBitmap) fromresult.documents.
1. Initialize and load the model
- iOS (Swift)
- Android (Kotlin)
import AVFoundation
import EVKit
import UIKit
func loadYotsukaD() -> YotsukaD {
// Create the detector once and reuse it for subsequent frames.
let detector = YotsukaD(
modelPath: "",
inputSize: (3, 640, 640),
scoreThreshold: 0.25,
numClasses: 3,
useGPU: true
)
// Detect load failures before running inference.
guard detector.load() else {
fatalError("Failed to load YotsukaD")
}
// Verify the state after loading.
guard detector.isLoaded() else {
fatalError("YotsukaD is not ready")
}
return detector
}
import android.content.Context
import com.nefrock.edgevision.yotsukad.YotsukaD
fun loadYotsukaD(context: Context): YotsukaD {
// Create the detector once and reuse it for subsequent frames.
val detector = YotsukaD(
modelPath = "",
inputSize = Triple(3, 640, 640),
scoreThreshold = 0.25f,
numClasses = 3,
useGpu = true,
context = context,
)
// Detect load failures before running inference.
check(detector.load()) { "Failed to load YotsukaD" }
// Verify the state after loading.
check(detector.isLoaded()) { "YotsukaD is not ready" }
return detector
}
modelPath— Pass an empty string""to use the bundled model. Specify a file path to use a custom model.inputSize— The model input size in(channels, height, width)order.scoreThreshold— The detection score threshold. Increase this value if there are too many false positives.numClasses— The number of classes the model can distinguish.useGPU/useGpu— Set totrueto use the GPU.
2. Run inference on camera input
- iOS (Swift)
- Android (Kotlin)
import AVFoundation
import CoreGraphics
import EVKit
// Configure scan options.
let options = DocScanOptions(
transformMode: .auto,
centerOnly: false
)
// Run real-time inference on the camera frame.
let result = try detector.scan(
sampleBuffer,
viewBounds: viewBounds,
cropRect: cropRect,
rotation: .rotate0,
options: options
)
import android.graphics.RectF
import androidx.camera.core.ImageProxy
import com.nefrock.edgevision.yotsukad.DocScanOptions
import com.nefrock.edgevision.yotsukad.DocScanResult
import com.nefrock.edgevision.yotsukad.YotsukaD
// Configure scan options.
val options = DocScanOptions(
transformMode = DocScanOptions.TransformMode.Auto,
centerOnly = false,
)
// Run real-time inference on the camera frame.
val result = try {
detector.scan(
image = imageProxy,
cropRect = cropRect,
options = options,
)
} finally {
// Always close so the next frame can flow.
imageProxy.close()
}
For camera input, specify the target area within the preview using cropRect and pass it to scan(...). On both platforms, retrieve the results from result.documents.
cropRectuses normalized coordinates from 0.0 to 1.0.- Swift requires
viewBounds(the preview display area) androtation(the frame orientation) as additional parameters. Kotlin reads rotation automatically fromImageProxy, so these are not needed.
Use transformMode: .auto or TransformMode.Auto to automatically estimate the document size.
Use .fixedSize(width:height:) or FixedSize(width, height) to save the corrected image at a fixed size.
Use centerOnly: true or centerOnly = true to detect only the document closest to the center.
3. Read the result
- iOS (Swift)
- Android (Kotlin)
for document in result.documents {
print("score: \(document.score)")
print("keypoints: \(document.keypoints)")
// Use the corrected image for display or saving.
if let cropped = document.cropped {
imageView.image = cropped
}
}
for (document in result.documents) {
println("score: ${document.score}")
println("keypoints: ${document.keypoints}")
// Use the corrected image for display or saving.
document.cropped?.let { cropped ->
imageView.setImageBitmap(cropped)
}
}
result.documents contains the list of detected documents. Each Document has the following properties:
score— Detection confidence (0.0–1.0)keypoints— Four corner coordinates (normalized)cropped— Perspective-corrected image (Swift:PlatformImage?/ Kotlin:Bitmap?)
If documents is empty, no targets were detected. Review the capture angle or cropRect range.
4. Run inference on a still image (Swift only)
To process a still image or a pre-processed image instead of a camera frame, pass a PlatformImage to scan(...).
import EVKit
// Configure scan options.
let options = DocScanOptions(
transformMode: .auto,
centerOnly: false
)
// Run inference on the full image.
let result = try detector.scan(
image,
cropRect: CGRect(x: 0, y: 0, width: 1, height: 1),
rotation: .rotate0,
options: options
)
for document in result.documents {
print("score: \(document.score)")
print("keypoints: \(document.keypoints)")
if let cropped = document.cropped {
imageView.image = cropped
}
}
- Passing
(0, 0, 1, 1)tocropRecttargets the entire image. - To restrict inference to a specific region, specify the area in normalized coordinates.