File size: 2,430 Bytes
8a761b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c024057
 
 
8a761b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import re

from gadalka import confidence_from_score


def clean_alnum(text):
    return re.sub(r"[^A-Z0-9]", "", str(text).upper())


DOCUMENT_CLASS_MARKERS = [
    "CERTIFICAT",
    "CERTIFICATE",
    "IMMATRICULATION",
    "DIMMATRICULATION",
    "REGISTRATION",
    "REGISTR",
    "SVIDET",
    "FEDER",
    "GOSUDAR",
    "IDENTIF",
]

PLATE_CLASS_MARKERS = [
    "MOTOR",
    "MODEL",
    "MANUFACT",
    "MFG",
    "FRAME",
    "VEHICLE",
    "HONDA",
    "YAMAHA",
    "KAWASAKI",
    "SUZUKI",
]

HEAVY_REPAIR_PREFIXES = (
    "wmi_repair:",
    "pattern_repair:",
)


def count_marker_hits(text, markers):
    upper = str(text).upper()
    cleaned = clean_alnum(upper)

    return sum(1 for marker in markers if marker in upper or marker in cleaned)


def classify_full_frame_source(pass_result, image_debug):
    raw_text = pass_result.get("raw_text", "")
    document_hits = count_marker_hits(raw_text, DOCUMENT_CLASS_MARKERS)
    plate_hits = count_marker_hits(raw_text, PLATE_CLASS_MARKERS)
    chunks_count = pass_result.get("chunks_count", 0)
    context_count = pass_result.get("context_chunks_count", 0)
    width = image_debug.get("ocr_width") or image_debug.get("original_width") or 0
    height = image_debug.get("ocr_height") or image_debug.get("original_height") or 0
    aspect = width / max(height, 1)

    if plate_hits >= 1 and aspect >= 1.25:
        source_class = "vin_plate"
    elif document_hits >= 2 or context_count >= 3 or chunks_count >= 12:
        source_class = "document"
    elif plate_hits >= 1 or aspect >= 1.35:
        source_class = "vin_plate"
    elif chunks_count >= 6:
        source_class = "text_frame"
    else:
        source_class = "unknown"

    return {
        "class": source_class,
        "document_hits": document_hits,
        "plate_hits": plate_hits,
        "chunks_count": chunks_count,
        "context_chunks_count": context_count,
        "aspect": round(aspect, 2),
    }


def candidate_has_heavy_repairs(candidate):
    reasons = candidate.get("reasons", []) if candidate else []
    return any(
        any(str(reason).startswith(prefix) for prefix in HEAVY_REPAIR_PREFIXES)
        for reason in reasons
    )


def full_frame_can_early_return(best, source_class):
    if confidence_from_score(best["score"]) != "high":
        return False

    if source_class == "document" and candidate_has_heavy_repairs(best):
        return False

    return True