Download source_classifier.py from Dirner/VIN_OCR: direct link, hf CLI and curl.
- Browser
- Download file 2.43 kB
-
https://huggingface.co/spaces/Dirner/VIN_OCR/resolve/main/source_classifier.py
- Command line
-
hf download hf://spaces/Dirner/VIN_OCR/source_classifier.py
-
curl -L -o source_classifier.py https://huggingface.co/spaces/Dirner/VIN_OCR/resolve/main/source_classifier.py
2.43 kB
| 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 | |