Upload 4 files
Browse files- app.py +118 -12
- gadalka.py +4 -1
- wmi.py +274 -28
app.py
CHANGED
|
@@ -39,16 +39,12 @@ DEEP_PASSES = [
|
|
| 39 |
]
|
| 40 |
|
| 41 |
FULL_FRAME_SOURCES = {"full_frame", "image", "document"}
|
| 42 |
-
FULL_FRAME_FAST_SCAN_PASSES = [
|
| 43 |
-
{"stage": "fast_scan", "rotation": 0, "preprocess": "original"},
|
| 44 |
-
{"stage": "fast_scan", "rotation": 90, "preprocess": "original"},
|
| 45 |
-
{"stage": "fast_scan", "rotation": 270, "preprocess": "original"},
|
| 46 |
-
]
|
| 47 |
FULL_FRAME_FOLLOWUP_PASSES = [
|
| 48 |
{"stage": "rotation_followup", "rotation": 180, "preprocess": "original"},
|
| 49 |
]
|
| 50 |
|
| 51 |
MEDIUM_SCORE = 38
|
|
|
|
| 52 |
MAX_UPLOAD_BYTES = 12 * 1024 * 1024
|
| 53 |
MIN_FIRST_PASS_TEXT_LENGTH = 12
|
| 54 |
MIN_CROP_OCR_WIDTH = 1100
|
|
@@ -64,8 +60,11 @@ MAX_IMAGE_PIXELS = 2_000_000
|
|
| 64 |
MIN_FULL_FRAME_OCR_WIDTH = 900
|
| 65 |
MIN_FULL_FRAME_OCR_HEIGHT = 150
|
| 66 |
MAX_FULL_FRAME_UPSCALE = 1.0
|
| 67 |
-
MAX_FULL_FRAME_LONG_SIDE =
|
| 68 |
-
MAX_FULL_FRAME_PIXELS =
|
|
|
|
|
|
|
|
|
|
| 69 |
ANGLE_PROBE_MIN_ABS = 12
|
| 70 |
ANGLE_PROBE_MAX_ABS = 60
|
| 71 |
ANGLE_PROBE_BUCKET_DEGREES = 5
|
|
@@ -165,10 +164,10 @@ def line_angle_degrees(x1, y1, x2, y2):
|
|
| 165 |
return angle
|
| 166 |
|
| 167 |
|
| 168 |
-
def
|
| 169 |
height, width = image.shape[:2]
|
| 170 |
max_side = max(height, width)
|
| 171 |
-
scale = min(1,
|
| 172 |
sample = image
|
| 173 |
|
| 174 |
if scale < 1:
|
|
@@ -195,7 +194,7 @@ def detect_skew_probe_rotations(image):
|
|
| 195 |
if lines is None:
|
| 196 |
return []
|
| 197 |
|
| 198 |
-
|
| 199 |
for line in lines:
|
| 200 |
x1, y1, x2, y2 = line[0]
|
| 201 |
length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
|
|
@@ -203,6 +202,15 @@ def detect_skew_probe_rotations(image):
|
|
| 203 |
continue
|
| 204 |
|
| 205 |
angle = line_angle_degrees(x1, y1, x2, y2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
if not ANGLE_PROBE_MIN_ABS <= abs(angle) <= ANGLE_PROBE_MAX_ABS:
|
| 207 |
continue
|
| 208 |
|
|
@@ -220,6 +228,59 @@ def detect_skew_probe_rotations(image):
|
|
| 220 |
return rotations
|
| 221 |
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
def add_unique_rotation(rotations, rotation):
|
| 224 |
rotation = normalized_small_angle(rotation)
|
| 225 |
if abs(rotation) < ANGLE_PROBE_MIN_ABS:
|
|
@@ -355,11 +416,31 @@ def get_warning(confidence):
|
|
| 355 |
return "Предварительно распознано. Проверьте номер перед отправкой."
|
| 356 |
|
| 357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
def public_candidate(candidate):
|
| 359 |
return {
|
| 360 |
"vin": candidate.get("vin", ""),
|
| 361 |
"score": candidate.get("score", -999),
|
| 362 |
"confidence": candidate.get("confidence", "none"),
|
|
|
|
| 363 |
"rotation": candidate.get("rotation"),
|
| 364 |
"preprocess": candidate.get("preprocess", ""),
|
| 365 |
"source_chunk": candidate.get("source_chunk", ""),
|
|
@@ -536,6 +617,19 @@ def ranked_promising_rotations(passes_used, limit=MAX_DEEP_PROMISING_ROTATIONS):
|
|
| 536 |
return [item["rotation"] for item in ranked[:limit]]
|
| 537 |
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
def crop_first_pass_is_empty(pass_result):
|
| 540 |
return (
|
| 541 |
pass_result["cleaned_text_length"] < MIN_FIRST_PASS_TEXT_LENGTH
|
|
@@ -623,6 +717,8 @@ def build_response(best, candidates, passes_used, started_at, image_debug):
|
|
| 623 |
if image_debug.get("source") in {"full_frame", "image", "document"} and confidence != "high":
|
| 624 |
ok = False
|
| 625 |
elapsed_ms = round((time.perf_counter() - started_at) * 1000)
|
|
|
|
|
|
|
| 626 |
|
| 627 |
return JSONResponse(
|
| 628 |
{
|
|
@@ -630,6 +726,8 @@ def build_response(best, candidates, passes_used, started_at, image_debug):
|
|
| 630 |
"vin": best["vin"] if ok else "",
|
| 631 |
"confidence": confidence,
|
| 632 |
"score": best["score"],
|
|
|
|
|
|
|
| 633 |
"warning": get_warning(confidence if ok else "none"),
|
| 634 |
"debug": {
|
| 635 |
"elapsed_ms": elapsed_ms,
|
|
@@ -733,8 +831,12 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
|
|
| 733 |
all_candidates = []
|
| 734 |
passes_used = []
|
| 735 |
pass_index = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
|
| 737 |
-
for pass_config in
|
| 738 |
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 739 |
pass_index += 1
|
| 740 |
passes_used.append(pass_result)
|
|
@@ -745,9 +847,10 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
|
|
| 745 |
|
| 746 |
promising_rotations = unique_promising_rotations(passes_used)
|
| 747 |
if not promising_rotations:
|
|
|
|
| 748 |
return build_scan_exit_response(
|
| 749 |
passes_used,
|
| 750 |
-
["
|
| 751 |
started_at,
|
| 752 |
image_debug,
|
| 753 |
)
|
|
@@ -765,6 +868,9 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
|
|
| 765 |
if confidence_from_score(best["score"]) == "high":
|
| 766 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 767 |
|
|
|
|
|
|
|
|
|
|
| 768 |
for pass_config in FULL_FRAME_FOLLOWUP_PASSES:
|
| 769 |
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 770 |
pass_index += 1
|
|
|
|
| 39 |
]
|
| 40 |
|
| 41 |
FULL_FRAME_SOURCES = {"full_frame", "image", "document"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
FULL_FRAME_FOLLOWUP_PASSES = [
|
| 43 |
{"stage": "rotation_followup", "rotation": 180, "preprocess": "original"},
|
| 44 |
]
|
| 45 |
|
| 46 |
MEDIUM_SCORE = 38
|
| 47 |
+
FULL_FRAME_STOP_LOW_SCORE = 30
|
| 48 |
MAX_UPLOAD_BYTES = 12 * 1024 * 1024
|
| 49 |
MIN_FIRST_PASS_TEXT_LENGTH = 12
|
| 50 |
MIN_CROP_OCR_WIDTH = 1100
|
|
|
|
| 60 |
MIN_FULL_FRAME_OCR_WIDTH = 900
|
| 61 |
MIN_FULL_FRAME_OCR_HEIGHT = 150
|
| 62 |
MAX_FULL_FRAME_UPSCALE = 1.0
|
| 63 |
+
MAX_FULL_FRAME_LONG_SIDE = 1100
|
| 64 |
+
MAX_FULL_FRAME_PIXELS = 1_050_000
|
| 65 |
+
MAX_FULL_FRAME_FAST_PASSES = 2
|
| 66 |
+
CARDINAL_AXIS_RATIO = 1.18
|
| 67 |
+
PORTRAIT_SIDEWAYS_RATIO = 1.35
|
| 68 |
ANGLE_PROBE_MIN_ABS = 12
|
| 69 |
ANGLE_PROBE_MAX_ABS = 60
|
| 70 |
ANGLE_PROBE_BUCKET_DEGREES = 5
|
|
|
|
| 164 |
return angle
|
| 165 |
|
| 166 |
|
| 167 |
+
def collect_line_angles(image, max_sample_side=900):
|
| 168 |
height, width = image.shape[:2]
|
| 169 |
max_side = max(height, width)
|
| 170 |
+
scale = min(1, max_sample_side / max(max_side, 1))
|
| 171 |
sample = image
|
| 172 |
|
| 173 |
if scale < 1:
|
|
|
|
| 194 |
if lines is None:
|
| 195 |
return []
|
| 196 |
|
| 197 |
+
line_angles = []
|
| 198 |
for line in lines:
|
| 199 |
x1, y1, x2, y2 = line[0]
|
| 200 |
length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
|
|
|
|
| 202 |
continue
|
| 203 |
|
| 204 |
angle = line_angle_degrees(x1, y1, x2, y2)
|
| 205 |
+
line_angles.append((angle, length))
|
| 206 |
+
|
| 207 |
+
return line_angles
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def detect_skew_probe_rotations(image):
|
| 211 |
+
buckets = {}
|
| 212 |
+
|
| 213 |
+
for angle, length in collect_line_angles(image):
|
| 214 |
if not ANGLE_PROBE_MIN_ABS <= abs(angle) <= ANGLE_PROBE_MAX_ABS:
|
| 215 |
continue
|
| 216 |
|
|
|
|
| 228 |
return rotations
|
| 229 |
|
| 230 |
|
| 231 |
+
def add_cardinal_rotation(rotations, rotation):
|
| 232 |
+
rotation = rotation % 360
|
| 233 |
+
if rotation not in rotations:
|
| 234 |
+
rotations.append(rotation)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def detect_cardinal_rotation_order(image):
|
| 238 |
+
height, width = image.shape[:2]
|
| 239 |
+
line_angles = collect_line_angles(image)
|
| 240 |
+
horizontal_weight = 0
|
| 241 |
+
vertical_weight = 0
|
| 242 |
+
|
| 243 |
+
for angle, length in line_angles:
|
| 244 |
+
abs_angle = abs(angle)
|
| 245 |
+
if abs_angle <= 15:
|
| 246 |
+
horizontal_weight += length
|
| 247 |
+
elif abs_angle >= 75:
|
| 248 |
+
vertical_weight += length
|
| 249 |
+
|
| 250 |
+
rotations = []
|
| 251 |
+
looks_sideways = (
|
| 252 |
+
vertical_weight > 0
|
| 253 |
+
and vertical_weight >= horizontal_weight * CARDINAL_AXIS_RATIO
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
if looks_sideways:
|
| 257 |
+
add_cardinal_rotation(rotations, 90)
|
| 258 |
+
add_cardinal_rotation(rotations, 270)
|
| 259 |
+
add_cardinal_rotation(rotations, 0)
|
| 260 |
+
elif height >= width * PORTRAIT_SIDEWAYS_RATIO:
|
| 261 |
+
add_cardinal_rotation(rotations, 90)
|
| 262 |
+
add_cardinal_rotation(rotations, 0)
|
| 263 |
+
add_cardinal_rotation(rotations, 270)
|
| 264 |
+
else:
|
| 265 |
+
add_cardinal_rotation(rotations, 0)
|
| 266 |
+
add_cardinal_rotation(rotations, 90)
|
| 267 |
+
add_cardinal_rotation(rotations, 270)
|
| 268 |
+
|
| 269 |
+
return rotations[:MAX_FULL_FRAME_FAST_PASSES]
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
def build_full_frame_fast_scan_passes(image):
|
| 273 |
+
return [
|
| 274 |
+
{
|
| 275 |
+
"stage": "fast_orientation",
|
| 276 |
+
"rotation": rotation,
|
| 277 |
+
"preprocess": "original",
|
| 278 |
+
"angle_source": "cardinal_probe",
|
| 279 |
+
}
|
| 280 |
+
for rotation in detect_cardinal_rotation_order(image)
|
| 281 |
+
]
|
| 282 |
+
|
| 283 |
+
|
| 284 |
def add_unique_rotation(rotations, rotation):
|
| 285 |
rotation = normalized_small_angle(rotation)
|
| 286 |
if abs(rotation) < ANGLE_PROBE_MIN_ABS:
|
|
|
|
| 416 |
return "Предварительно распознано. Проверьте номер перед отправкой."
|
| 417 |
|
| 418 |
|
| 419 |
+
def vehicle_notice(vehicle_type):
|
| 420 |
+
if vehicle_type == "commercial":
|
| 421 |
+
return {
|
| 422 |
+
"special_order": True,
|
| 423 |
+
"sales_note": "Коммерческий или грузовой VIN распознан. Такие позиции мы делаем под заказ: оставьте почту и телефон, мы свяжемся с вами.",
|
| 424 |
+
}
|
| 425 |
+
|
| 426 |
+
if vehicle_type == "motorcycle":
|
| 427 |
+
return {
|
| 428 |
+
"special_order": True,
|
| 429 |
+
"sales_note": "VIN мотоцикла распознан. Мотоциклы мы пока не обслуживаем. Можете оставить почту и телефон: мы свяжемся с вами, когда это направление появится.",
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
return {
|
| 433 |
+
"special_order": False,
|
| 434 |
+
"sales_note": "",
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
|
| 438 |
def public_candidate(candidate):
|
| 439 |
return {
|
| 440 |
"vin": candidate.get("vin", ""),
|
| 441 |
"score": candidate.get("score", -999),
|
| 442 |
"confidence": candidate.get("confidence", "none"),
|
| 443 |
+
"vehicle_type": candidate.get("vehicle_type", "unknown"),
|
| 444 |
"rotation": candidate.get("rotation"),
|
| 445 |
"preprocess": candidate.get("preprocess", ""),
|
| 446 |
"source_chunk": candidate.get("source_chunk", ""),
|
|
|
|
| 617 |
return [item["rotation"] for item in ranked[:limit]]
|
| 618 |
|
| 619 |
|
| 620 |
+
def candidate_has_stable_vin_shape(candidate):
|
| 621 |
+
if not candidate:
|
| 622 |
+
return False
|
| 623 |
+
|
| 624 |
+
reasons = candidate.get("reasons", [])
|
| 625 |
+
return (
|
| 626 |
+
"length:17" in reasons
|
| 627 |
+
and "valid_charset" in reasons
|
| 628 |
+
and "serial_tail_digits" in reasons
|
| 629 |
+
and any(reason.startswith("checksum_match") for reason in reasons)
|
| 630 |
+
)
|
| 631 |
+
|
| 632 |
+
|
| 633 |
def crop_first_pass_is_empty(pass_result):
|
| 634 |
return (
|
| 635 |
pass_result["cleaned_text_length"] < MIN_FIRST_PASS_TEXT_LENGTH
|
|
|
|
| 717 |
if image_debug.get("source") in {"full_frame", "image", "document"} and confidence != "high":
|
| 718 |
ok = False
|
| 719 |
elapsed_ms = round((time.perf_counter() - started_at) * 1000)
|
| 720 |
+
vehicle_type = best.get("vehicle_type", "unknown")
|
| 721 |
+
notice = vehicle_notice(vehicle_type)
|
| 722 |
|
| 723 |
return JSONResponse(
|
| 724 |
{
|
|
|
|
| 726 |
"vin": best["vin"] if ok else "",
|
| 727 |
"confidence": confidence,
|
| 728 |
"score": best["score"],
|
| 729 |
+
"vehicle_type": vehicle_type,
|
| 730 |
+
**notice,
|
| 731 |
"warning": get_warning(confidence if ok else "none"),
|
| 732 |
"debug": {
|
| 733 |
"elapsed_ms": elapsed_ms,
|
|
|
|
| 831 |
all_candidates = []
|
| 832 |
passes_used = []
|
| 833 |
pass_index = 1
|
| 834 |
+
fast_scan_passes = build_full_frame_fast_scan_passes(image)
|
| 835 |
+
image_debug["fast_scan_rotations"] = [
|
| 836 |
+
pass_config["rotation"] for pass_config in fast_scan_passes
|
| 837 |
+
]
|
| 838 |
|
| 839 |
+
for pass_config in fast_scan_passes:
|
| 840 |
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 841 |
pass_index += 1
|
| 842 |
passes_used.append(pass_result)
|
|
|
|
| 847 |
|
| 848 |
promising_rotations = unique_promising_rotations(passes_used)
|
| 849 |
if not promising_rotations:
|
| 850 |
+
fast_scan_label = "_".join(str(item["rotation"]) for item in fast_scan_passes)
|
| 851 |
return build_scan_exit_response(
|
| 852 |
passes_used,
|
| 853 |
+
[f"fast_scan_no_vin_signal_{fast_scan_label}"],
|
| 854 |
started_at,
|
| 855 |
image_debug,
|
| 856 |
)
|
|
|
|
| 868 |
if confidence_from_score(best["score"]) == "high":
|
| 869 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 870 |
|
| 871 |
+
if best["score"] >= FULL_FRAME_STOP_LOW_SCORE and candidate_has_stable_vin_shape(best):
|
| 872 |
+
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 873 |
+
|
| 874 |
for pass_config in FULL_FRAME_FOLLOWUP_PASSES:
|
| 875 |
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 876 |
pass_index += 1
|
gadalka.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from wmi import KNOWN_WMI
|
| 2 |
from vidalka import VIN_LENGTH, final_vin_is_valid, has_bad_pattern
|
| 3 |
|
| 4 |
|
|
@@ -232,10 +232,12 @@ def score_vin(vin, seed_reasons=None):
|
|
| 232 |
|
| 233 |
wmi = vin[:3]
|
| 234 |
wmi_score = KNOWN_WMI.get(wmi)
|
|
|
|
| 235 |
|
| 236 |
if wmi_score is not None:
|
| 237 |
score += wmi_score
|
| 238 |
reasons.append(f"known_wmi:{wmi}")
|
|
|
|
| 239 |
elif vin[:1] == "0":
|
| 240 |
score -= 35
|
| 241 |
reasons.append("impossible_wmi_first:0")
|
|
@@ -311,6 +313,7 @@ def score_vin(vin, seed_reasons=None):
|
|
| 311 |
"vin": vin,
|
| 312 |
"score": score,
|
| 313 |
"confidence": confidence_from_score(score),
|
|
|
|
| 314 |
"reasons": dedupe_reasons(reasons),
|
| 315 |
}
|
| 316 |
|
|
|
|
| 1 |
+
from wmi import KNOWN_WMI, WMI_CATEGORY
|
| 2 |
from vidalka import VIN_LENGTH, final_vin_is_valid, has_bad_pattern
|
| 3 |
|
| 4 |
|
|
|
|
| 232 |
|
| 233 |
wmi = vin[:3]
|
| 234 |
wmi_score = KNOWN_WMI.get(wmi)
|
| 235 |
+
vehicle_type = WMI_CATEGORY.get(wmi, "unknown")
|
| 236 |
|
| 237 |
if wmi_score is not None:
|
| 238 |
score += wmi_score
|
| 239 |
reasons.append(f"known_wmi:{wmi}")
|
| 240 |
+
reasons.append(f"vehicle_type:{vehicle_type}")
|
| 241 |
elif vin[:1] == "0":
|
| 242 |
score -= 35
|
| 243 |
reasons.append("impossible_wmi_first:0")
|
|
|
|
| 313 |
"vin": vin,
|
| 314 |
"score": score,
|
| 315 |
"confidence": confidence_from_score(score),
|
| 316 |
+
"vehicle_type": vehicle_type,
|
| 317 |
"reasons": dedupe_reasons(reasons),
|
| 318 |
}
|
| 319 |
|
wmi.py
CHANGED
|
@@ -1,47 +1,293 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"XWF": 45,
|
| 7 |
"XWW": 40,
|
| 8 |
"XWZ": 25,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"MX1": 40,
|
| 10 |
|
| 11 |
-
|
| 12 |
-
"WAU": 45,
|
| 13 |
-
"WA1": 45,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"WBA": 45,
|
| 15 |
"WBS": 45,
|
|
|
|
|
|
|
| 16 |
"WDD": 45,
|
| 17 |
"WDB": 45,
|
| 18 |
"WDC": 45,
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
"JTM": 45,
|
| 29 |
"JTN": 45,
|
| 30 |
-
"
|
| 31 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"JMZ": 40,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
"
|
| 42 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"1G6": 35,
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Practical WMI knowledge layer.
|
| 2 |
+
#
|
| 3 |
+
# This is intentionally not a full world VIN database. WMI is a scoring signal:
|
| 4 |
+
# common/strong codes get a larger boost, rare or adjacent vehicle types get a
|
| 5 |
+
# smaller boost, and unknown WMI remains possible.
|
| 6 |
+
|
| 7 |
+
COMMON_PASSENGER_WMI = {
|
| 8 |
+
# Russia / Eastern Europe / local assembly
|
| 9 |
+
"XTA": 40, # Lada
|
| 10 |
+
"XTT": 35, # UAZ
|
| 11 |
+
"X96": 30, # GAZ / local commercial-family VINs
|
| 12 |
+
"XW7": 55, # Toyota Russia
|
| 13 |
+
"XW8": 45, # Volkswagen Russia
|
| 14 |
+
"XWE": 45, # Hyundai/Kia local
|
| 15 |
"XWF": 45,
|
| 16 |
"XWW": 40,
|
| 17 |
"XWZ": 25,
|
| 18 |
+
"X7L": 35, # Renault Russia / local assembly patterns
|
| 19 |
+
"X7M": 30,
|
| 20 |
+
"Z8N": 35, # Nissan Russia
|
| 21 |
+
"Z94": 35, # Hyundai Russia
|
| 22 |
"MX1": 40,
|
| 23 |
|
| 24 |
+
# VAG / Europe
|
| 25 |
+
"WAU": 45, # Audi
|
| 26 |
+
"WA1": 45, # Audi SUV
|
| 27 |
+
"WAC": 35,
|
| 28 |
+
"TRU": 35, # Audi TT / Hungary
|
| 29 |
+
"WVW": 45, # Volkswagen
|
| 30 |
+
"WVG": 45, # Volkswagen SUV
|
| 31 |
+
"VWV": 30, # Volkswagen Spain
|
| 32 |
+
"TMB": 45, # Skoda
|
| 33 |
+
"VSS": 45, # Seat
|
| 34 |
+
"WP0": 45, # Porsche
|
| 35 |
+
"WP1": 45, # Porsche SUV
|
| 36 |
+
|
| 37 |
+
# BMW / Mercedes / European premium
|
| 38 |
"WBA": 45,
|
| 39 |
"WBS": 45,
|
| 40 |
+
"WBX": 40,
|
| 41 |
+
"WBY": 35,
|
| 42 |
"WDD": 45,
|
| 43 |
"WDB": 45,
|
| 44 |
"WDC": 45,
|
| 45 |
+
"WDF": 35,
|
| 46 |
+
"WME": 35, # Smart
|
| 47 |
+
"WMW": 40, # Mini
|
| 48 |
+
"SAL": 40, # Land Rover
|
| 49 |
+
"SAJ": 40, # Jaguar
|
| 50 |
+
"SCB": 35, # Bentley
|
| 51 |
+
"SCC": 35, # Lotus
|
| 52 |
+
"YV1": 40, # Volvo car
|
| 53 |
+
"YV4": 40, # Volvo SUV
|
| 54 |
|
| 55 |
+
# France / Italy / Europe mass market
|
| 56 |
+
"VF1": 40, # Renault
|
| 57 |
+
"VF3": 40, # Peugeot
|
| 58 |
+
"VF7": 40, # Citroen
|
| 59 |
+
"UU1": 35, # Dacia
|
| 60 |
+
"ZFA": 40, # Fiat
|
| 61 |
+
"ZAR": 35, # Alfa Romeo
|
| 62 |
+
"ZFF": 30, # Ferrari
|
| 63 |
+
"ZHW": 30, # Lamborghini
|
| 64 |
+
"W0L": 40, # Opel
|
| 65 |
+
"XUF": 30, # Opel / local assembly patterns
|
| 66 |
|
| 67 |
+
# Japan / Asia passenger
|
| 68 |
+
"JTD": 45, # Toyota
|
| 69 |
+
"JTE": 45,
|
| 70 |
+
"JTF": 40,
|
| 71 |
+
"JTG": 40,
|
| 72 |
+
"JTJ": 45,
|
| 73 |
"JTM": 45,
|
| 74 |
"JTN": 45,
|
| 75 |
+
"NMT": 35, # Toyota Turkey
|
| 76 |
+
"MR0": 35, # Toyota Thailand
|
| 77 |
+
"VNK": 35, # Toyota France
|
| 78 |
+
"JHM": 40, # Honda
|
| 79 |
+
"JHL": 40,
|
| 80 |
+
"JH4": 35, # Acura / Honda family
|
| 81 |
+
"1HG": 35,
|
| 82 |
+
"2HG": 35,
|
| 83 |
+
"5FN": 35,
|
| 84 |
+
"JN1": 40, # Nissan
|
| 85 |
+
"JN6": 35,
|
| 86 |
+
"JN8": 35,
|
| 87 |
+
"SJN": 35,
|
| 88 |
+
"3N1": 35,
|
| 89 |
+
"JM1": 40, # Mazda
|
| 90 |
+
"JM3": 40,
|
| 91 |
"JMZ": 40,
|
| 92 |
+
"JF1": 35, # Subaru
|
| 93 |
+
"JF2": 35,
|
| 94 |
+
"JA3": 35, # Mitsubishi
|
| 95 |
+
"JA4": 35,
|
| 96 |
+
"JMB": 35,
|
| 97 |
+
"MMB": 30,
|
| 98 |
|
| 99 |
+
# Korea
|
| 100 |
+
"KMH": 45, # Hyundai
|
| 101 |
+
"KM8": 45, # Hyundai SUV
|
| 102 |
+
"KNA": 45, # Kia
|
| 103 |
+
"KND": 45,
|
| 104 |
+
"KNE": 40,
|
| 105 |
+
"KNC": 35,
|
| 106 |
+
"KMF": 30,
|
| 107 |
+
"KL1": 35, # GM Korea / Chevrolet
|
| 108 |
|
| 109 |
+
# China / local Asian assembly
|
| 110 |
+
"LFP": 35, # FAW
|
| 111 |
+
"LFV": 35, # FAW-VW
|
| 112 |
+
"LFM": 35, # Toyota China
|
| 113 |
+
"LHG": 35, # Honda China
|
| 114 |
+
"LGB": 35, # Dongfeng Nissan / China
|
| 115 |
+
"LDC": 40, # Dongfeng / PSA China
|
| 116 |
+
"LSG": 40, # SAIC / GM China
|
| 117 |
+
"LSV": 40, # SAIC Volkswagen
|
| 118 |
+
"LZW": 40, # SGMW
|
| 119 |
+
"LVS": 40, # Ford China
|
| 120 |
+
"LVV": 35, # Chery
|
| 121 |
+
"L6T": 35, # Geely common OCR-practical layer
|
| 122 |
+
"LBV": 35, # BMW Brilliance
|
| 123 |
+
"LGW": 40, # Great Wall
|
| 124 |
+
"LRW": 35, # Tesla China
|
| 125 |
+
|
| 126 |
+
# North America passenger / pickup / SUV
|
| 127 |
+
"1FA": 35, # Ford
|
| 128 |
+
"1FM": 35,
|
| 129 |
+
"1FT": 35,
|
| 130 |
+
"1F1": 25,
|
| 131 |
+
"1G1": 35, # GM / Chevrolet
|
| 132 |
+
"1G4": 30,
|
| 133 |
"1G6": 35,
|
| 134 |
+
"1GC": 35,
|
| 135 |
+
"1GT": 35,
|
| 136 |
+
"1GY": 35,
|
| 137 |
+
"2G1": 35,
|
| 138 |
+
"2G4": 30,
|
| 139 |
+
"3G1": 35,
|
| 140 |
+
"3GC": 35,
|
| 141 |
+
"1B3": 30, # Chrysler / Dodge
|
| 142 |
+
"1C3": 30,
|
| 143 |
+
"1C4": 35,
|
| 144 |
+
"1C6": 35,
|
| 145 |
+
"2C3": 30,
|
| 146 |
+
"2C4": 30,
|
| 147 |
+
"3C4": 35,
|
| 148 |
+
"3C6": 35,
|
| 149 |
+
"1J4": 30, # Jeep older patterns
|
| 150 |
+
"4T1": 35, # Toyota USA
|
| 151 |
+
"5TD": 35,
|
| 152 |
+
"5TF": 35,
|
| 153 |
+
"4S3": 30, # Subaru USA
|
| 154 |
+
"3VW": 35, # Volkswagen Mexico
|
| 155 |
+
"5YJ": 35, # Tesla
|
| 156 |
+
"7SA": 35,
|
| 157 |
}
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
COMMERCIAL_TRUCK_WMI = {
|
| 161 |
+
# Russia / CIS commercial
|
| 162 |
+
"XTC": 35, # KamAZ
|
| 163 |
+
"XTH": 25,
|
| 164 |
+
"X89": 25,
|
| 165 |
+
|
| 166 |
+
# European vans / trucks / buses / trailers
|
| 167 |
+
"WV1": 35, # VW commercial
|
| 168 |
+
"WV2": 35, # VW bus/MPV
|
| 169 |
+
"WMA": 35, # MAN
|
| 170 |
+
"YV2": 35, # Volvo Trucks
|
| 171 |
+
"XLR": 35, # DAF
|
| 172 |
+
"ZCF": 35, # Iveco
|
| 173 |
+
"VF6": 30, # Renault Trucks
|
| 174 |
+
"WDP": 25, # Freightliner Sprinter incomplete
|
| 175 |
+
"WDR": 25,
|
| 176 |
+
"WDY": 25,
|
| 177 |
+
"WDZ": 25,
|
| 178 |
+
|
| 179 |
+
# North America heavy/commercial
|
| 180 |
+
"1FD": 30, # Ford incomplete/commercial
|
| 181 |
+
"1FU": 35, # Freightliner
|
| 182 |
+
"1FV": 35,
|
| 183 |
+
"1HT": 35, # International / Navistar
|
| 184 |
+
"2HS": 35,
|
| 185 |
+
"3HS": 35,
|
| 186 |
+
"1M1": 35, # Mack
|
| 187 |
+
"1XK": 35, # Kenworth
|
| 188 |
+
"1XP": 35, # Peterbilt
|
| 189 |
+
"4V4": 35, # Volvo Trucks NA
|
| 190 |
+
"4V5": 35,
|
| 191 |
+
"5PV": 30, # Hino USA
|
| 192 |
+
"JAL": 30, # Isuzu commercial
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
MOTORCYCLE_WMI = {
|
| 197 |
+
# Honda / Acura motorcycle and powersports family
|
| 198 |
+
"JH1": 35,
|
| 199 |
+
"JH2": 40,
|
| 200 |
+
"JH3": 35,
|
| 201 |
+
"MLH": 35, # Thai Honda
|
| 202 |
+
"VTD": 30, # Montesa Honda
|
| 203 |
+
|
| 204 |
+
# Yamaha / MBK
|
| 205 |
+
"JYA": 40,
|
| 206 |
+
"VTL": 30, # Yamaha Spain
|
| 207 |
+
"VG5": 25, # MBK / Yamaha family
|
| 208 |
+
|
| 209 |
+
# Suzuki
|
| 210 |
+
"JS1": 40,
|
| 211 |
+
"JSA": 30,
|
| 212 |
+
"MH8": 30, # Suzuki Indonesia
|
| 213 |
+
"MLC": 30, # Thai Suzuki
|
| 214 |
+
"VTT": 30, # Suzuki Spain
|
| 215 |
+
"LC6": 25, # Haojue Suzuki / China
|
| 216 |
+
"5SA": 25, # Suzuki Manufacturing of America
|
| 217 |
+
|
| 218 |
+
# Kawasaki
|
| 219 |
+
"JKA": 40,
|
| 220 |
+
"JKB": 35,
|
| 221 |
+
"JK1": 30,
|
| 222 |
+
"ML5": 30, # Kawasaki Thailand
|
| 223 |
+
|
| 224 |
+
# BMW Motorrad
|
| 225 |
+
"WB1": 35,
|
| 226 |
+
"WB3": 30,
|
| 227 |
+
"WB4": 30,
|
| 228 |
+
"95V": 25, # BMW Motorrad Brazil practical layer
|
| 229 |
+
|
| 230 |
+
# Ducati / Italian sport and scooter groups
|
| 231 |
+
"ZDM": 35,
|
| 232 |
+
"ML0": 30, # Ducati Thailand
|
| 233 |
+
"ZCG": 25, # Cagiva / MV Agusta family
|
| 234 |
+
"ZAP": 30, # Piaggio / Vespa
|
| 235 |
+
"ZD4": 30, # Aprilia / Piaggio family
|
| 236 |
+
"ZD3": 25,
|
| 237 |
+
"ZGU": 25, # Moto Guzzi
|
| 238 |
+
|
| 239 |
+
# KTM / Husqvarna / Bajaj-adjacent
|
| 240 |
+
"VBK": 35,
|
| 241 |
+
"MD2": 30, # KTM/Bajaj family
|
| 242 |
+
"ME3": 30, # Royal Enfield
|
| 243 |
+
|
| 244 |
+
# Triumph / Harley-Davidson / Indian / Polaris
|
| 245 |
+
"SMT": 35,
|
| 246 |
+
"1HD": 35,
|
| 247 |
+
"5HD": 35,
|
| 248 |
+
"MLY": 30, # Harley-Davidson Thailand
|
| 249 |
+
"56K": 30, # Indian Motorcycle
|
| 250 |
+
"5Y4": 25, # Yamaha/Polaris powersports practical layer
|
| 251 |
+
|
| 252 |
+
# Korean / Chinese / Taiwan / India practical layer
|
| 253 |
+
"KM4": 25, # Hyosung / KR motors family
|
| 254 |
+
"LBB": 25,
|
| 255 |
+
"LCE": 25,
|
| 256 |
+
"L5Y": 25,
|
| 257 |
+
"LM1": 25,
|
| 258 |
+
"LM4": 25,
|
| 259 |
+
"RFD": 25,
|
| 260 |
+
"MD6": 25, # TVS
|
| 261 |
+
"MBL": 25, # Hero
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def merge_wmi_groups(*groups):
|
| 266 |
+
merged = {}
|
| 267 |
+
for group in groups:
|
| 268 |
+
for wmi, score in group.items():
|
| 269 |
+
merged[wmi] = max(score, merged.get(wmi, -999))
|
| 270 |
+
return merged
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def build_wmi_category():
|
| 274 |
+
categories = {}
|
| 275 |
+
|
| 276 |
+
for wmi in COMMON_PASSENGER_WMI:
|
| 277 |
+
categories[wmi] = "passenger"
|
| 278 |
+
|
| 279 |
+
for wmi in COMMERCIAL_TRUCK_WMI:
|
| 280 |
+
categories[wmi] = "commercial"
|
| 281 |
+
|
| 282 |
+
for wmi in MOTORCYCLE_WMI:
|
| 283 |
+
categories[wmi] = "motorcycle"
|
| 284 |
+
|
| 285 |
+
return categories
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
KNOWN_WMI = merge_wmi_groups(
|
| 289 |
+
COMMON_PASSENGER_WMI,
|
| 290 |
+
COMMERCIAL_TRUCK_WMI,
|
| 291 |
+
MOTORCYCLE_WMI,
|
| 292 |
+
)
|
| 293 |
+
WMI_CATEGORY = build_wmi_category()
|