Skip to content

Add Custom OpenCV and ONNX Runtime Builds and OCR performance optimization - #57

Open
283375 wants to merge 26 commits into
masterfrom
refactor/custom-libs
Open

Add Custom OpenCV and ONNX Runtime Builds and OCR performance optimization#57
283375 wants to merge 26 commits into
masterfrom
refactor/custom-libs

Conversation

@283375

@283375 283375 commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator
  • Migrate to OpenCV 5, remove KNearest digit OCR logics
  • Use custom OpenCV and ONNX Runtime builds from https://github.com/ArcaeaOffline/custom-lib-builds
  • Add "OCR Performance" quick evaluation page
  • Fix OCR performance: hammingDistance is now allocation-free in image hash lookups, previous zip-based version caused severe GC pressure. Now queue throughput is roughly doubled

283375 added 26 commits August 2, 2026 22:05
- delete OcrDigits.kt (HOG feature extraction + findNearest helpers)
- remove kNearestModel parameter from DeviceOcr constructor
- remove pfl/pure/far/lost/score/maxRecall methods (dead code since ONNX migration)
- remove FixRects (only used by removed KNN digit pipeline)
- remove kNearestModel parameter from DeviceOcrHelper.ocrImage
- stop loading KNN model in OcrQueueOcrImageTaskExecutor and OcrFromShareViewModel.startOcr
- drop kNearestModel() loaders, KNearestModelStatusDetail and status builder
- remove knnModelFile path constant and its deletion in emergency mode
- add LegacyKnnModelCleanUpTask to clean up leftover digits.knn.dat
  on devices that installed before the removal
- delete OcrDependencyKNearestModelStatusViewer (status card + ui state)
- remove KNearest status cards from OcrNavEntry, OcrFromShare card and OcrDependenciesScreen
- remove importKNearestModel and related state from OcrDependenciesScreenViewModel
- delete ocr_dependency_knn_model strings (app + shared composeResources, zh-rCN)
- delete ic_knearest_model drawable
- OpenCV 5.0 removes ml (KNearest) and HOG from the main repo;
  both were already removed from this codebase, so no migration needed
- usage limited to stable core/imgproc/imgcodecs/android APIs,
  verified by Android Studio build (no problems)
Database version jumped from 2 to 4 in 80139db; version 3 was never
published, but the AutoMigration was generated against a fictional
3.json (enqueue_buffer with uri_type column) which crashes on real
v2 databases (no such column: uri_type).

Remove the AutoMigration and the spec; old databases now hit the
existing fallbackToDestructiveMigration(dropAllTables = true) path
and are rebuilt cleanly as v4.
Switch to the local maven repo (maven-local/) that overrides official
onnxruntime 1.26.0 and opencv 5.0.0 with custom-built AARs.
Parse dual-block model_info.json for the dependency status card instead of
OnnxModelMetadata, disable graph optimization (NO_OPT) for the reduced-op
runtime, and read INT32 model output directly.
Add setup-custom-maven action that downloads maven_repo.zip from the
custom-lib-builds stable release tags (onnxruntime-1.26.0, opencv-5.0.0)
into maven-local/, overriding official artifacts with identical GAV.
Wire it into build_unstable, check, and connected-android-test workflows.

No cache: stable tags are overwritten on publish (URL stable, content
changes), so version-based cache keys would serve stale artifacts.
Compare .so sha256 between maven-local/ AARs and built APKs, guarding
against silent fallback to official onnxruntime/opencv artifacts.
Warnings go to annotations and the job summary; continue-on-error
keeps artifact upload and release drafting unblocked.
Move the download/extract logic out of the composite action and the
apksigner checks out of the workflow, so both are runnable and testable
locally. setup-custom-maven and verify-apk-signing keep hard-fail
semantics; verify-custom-libs remains soft-fail. Cleaned up shellcheck
findings along the way.
- Previous commit was mislead, this commit actually just fixes it.
@283375 283375 changed the title Add Custom OpenCV and ONNX Runtime Builds Add Custom OpenCV and ONNX Runtime Builds and OCR performance optimization Aug 18, 2026
Comment on lines +24 to +31
val parallelCount: Int = defaultParallelCount(),
) {
companion object {
fun defaultParallelCount(): Int = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)

fun parallelCountRange(): IntRange = 1..(Runtime.getRuntime().availableProcessors() * 2).coerceAtLeast(2)
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it appropriate to put this "single source" in a datastore-related object?

Comment on lines +91 to +100
/**
* Lightweight sanity check for the bundled model asset. Does not load or
* validate the model itself (a truncated file still passes); use
* [createOrtSession] for a full check.
*/
fun checkModelAsset(context: Context) {
context.assets.open(MODEL_ASSET_PATH).use { stream ->
if (stream.read(ByteArray(1)) == -1) throw IOException("OCR model asset is empty: $MODEL_ASSET_PATH")
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about comparing the version info in model metadata (and probably other lightweight fields) to improve accuracy?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the file location. Now the import name for Mat.use is import xyz.sevive.arcaeaoffline.core.ocr.use, which might not clear or readable.

@283375 283375 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs further investigations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant