OcrLine.kt 34 lignes · 1424 octets
package fr.ebii.card2vcf.ocr

/**
 * Boîte englobante en pixels de l'image OCRisée (repère y vers le bas).
 * Data class maison (pas android.graphics.Rect) : le modèle et le parseur
 * restent testables en JVM pure. Seules les positions RELATIVES comptent
 * (hauteurs, écarts, alignements) — le repère absolu dépend du candidat
 * (prétraitement, orientation) mais est cohérent au sein d'un même résultat.
 */
data class OcrBox(val left: Int, val top: Int, val right: Int, val bottom: Int) {
    val height: Int get() = bottom - top
    val width: Int get() = right - left

    fun overlapsHorizontally(other: OcrBox): Boolean =
        left < other.right && other.left < right

    fun union(other: OcrBox): OcrBox = OcrBox(
        minOf(left, other.left),
        minOf(top, other.top),
        maxOf(right, other.right),
        maxOf(bottom, other.bottom),
    )
}

/** Ligne de texte reconnue avec sa boîte et la confiance Tesseract (0-100). */
data class OcrLine(val text: String, val box: OcrBox, val confidence: Float)

/** Groupe de lignes spatialement cohérent (pavé d'adresse, bloc contact…). */
data class OcrBlock(val lines: List<OcrLine>) {
    fun text(): String = lines.joinToString("\n") { it.text }
    fun box(): OcrBox = lines.map { it.box }.reduce(OcrBox::union)
    fun avgConfidence(): Float =
        if (lines.isEmpty()) 0f else lines.map { it.confidence }.average().toFloat()
}