Dirner commited on
Commit
bfaaf1f
·
verified ·
1 Parent(s): 85e9709

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -6
app.py CHANGED
@@ -370,6 +370,92 @@ def clean_alnum(text):
370
  return re.sub(r"[^A-Z0-9]", "", str(text).upper())
371
 
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  def empty_best():
374
  return {
375
  "vin": "",
@@ -833,6 +919,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
833
  pass_index = 1
834
  fast_scan_passes = build_full_frame_fast_scan_passes(image)
835
  early_angle_done = False
 
836
  image_debug["fast_scan_rotations"] = [
837
  pass_config["rotation"] for pass_config in fast_scan_passes
838
  ]
@@ -843,7 +930,11 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
843
  passes_used.append(pass_result)
844
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
845
 
846
- if confidence_from_score(best["score"]) == "high":
 
 
 
 
847
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
848
 
849
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
@@ -872,7 +963,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
872
  passes_used.append(pass_result)
873
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
874
 
875
- if confidence_from_score(best["score"]) == "high":
876
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
877
 
878
  for pass_config in fast_scan_passes[1:]:
@@ -884,7 +975,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
884
  passes_used.append(pass_result)
885
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
886
 
887
- if confidence_from_score(best["score"]) == "high":
888
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
889
 
890
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
@@ -914,7 +1005,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
914
  passes_used.append(pass_result)
915
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
916
 
917
- if confidence_from_score(best["score"]) == "high":
918
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
919
 
920
  if best["score"] >= FULL_FRAME_STOP_LOW_SCORE and candidate_has_stable_vin_shape(best):
@@ -929,7 +1020,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
929
  passes_used.append(pass_result)
930
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
931
 
932
- if confidence_from_score(best["score"]) == "high":
933
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
934
 
935
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
@@ -951,7 +1042,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
951
  passes_used.append(pass_result)
952
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
953
 
954
- if confidence_from_score(best["score"]) == "high":
955
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
956
 
957
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
 
370
  return re.sub(r"[^A-Z0-9]", "", str(text).upper())
371
 
372
 
373
+ DOCUMENT_CLASS_MARKERS = [
374
+ "CERTIFICAT",
375
+ "CERTIFICATE",
376
+ "IMMATRICULATION",
377
+ "DIMMATRICULATION",
378
+ "REGISTRATION",
379
+ "REGISTR",
380
+ "SVIDET",
381
+ "FEDER",
382
+ "GOSUDAR",
383
+ "IDENTIF",
384
+ ]
385
+
386
+ PLATE_CLASS_MARKERS = [
387
+ "MOTOR",
388
+ "MODEL",
389
+ "MANUFACT",
390
+ "MFG",
391
+ "FRAME",
392
+ "VEHICLE",
393
+ "HONDA",
394
+ "YAMAHA",
395
+ "KAWASAKI",
396
+ "SUZUKI",
397
+ ]
398
+
399
+ HEAVY_REPAIR_PREFIXES = (
400
+ "wmi_repair:",
401
+ "pattern_repair:",
402
+ )
403
+
404
+
405
+ def count_marker_hits(text, markers):
406
+ upper = str(text).upper()
407
+ cleaned = clean_alnum(upper)
408
+
409
+ return sum(1 for marker in markers if marker in upper or marker in cleaned)
410
+
411
+
412
+ def classify_full_frame_source(pass_result, image_debug):
413
+ raw_text = pass_result.get("raw_text", "")
414
+ document_hits = count_marker_hits(raw_text, DOCUMENT_CLASS_MARKERS)
415
+ plate_hits = count_marker_hits(raw_text, PLATE_CLASS_MARKERS)
416
+ chunks_count = pass_result.get("chunks_count", 0)
417
+ context_count = pass_result.get("context_chunks_count", 0)
418
+ width = image_debug.get("ocr_width") or image_debug.get("original_width") or 0
419
+ height = image_debug.get("ocr_height") or image_debug.get("original_height") or 0
420
+ aspect = width / max(height, 1)
421
+
422
+ if document_hits >= 2 or context_count >= 3 or chunks_count >= 12:
423
+ source_class = "document"
424
+ elif plate_hits >= 1 or aspect >= 1.35:
425
+ source_class = "vin_plate"
426
+ elif chunks_count >= 6:
427
+ source_class = "text_frame"
428
+ else:
429
+ source_class = "unknown"
430
+
431
+ return {
432
+ "class": source_class,
433
+ "document_hits": document_hits,
434
+ "plate_hits": plate_hits,
435
+ "chunks_count": chunks_count,
436
+ "context_chunks_count": context_count,
437
+ "aspect": round(aspect, 2),
438
+ }
439
+
440
+
441
+ def candidate_has_heavy_repairs(candidate):
442
+ reasons = candidate.get("reasons", []) if candidate else []
443
+ return any(
444
+ any(str(reason).startswith(prefix) for prefix in HEAVY_REPAIR_PREFIXES)
445
+ for reason in reasons
446
+ )
447
+
448
+
449
+ def full_frame_can_early_return(best, source_class):
450
+ if confidence_from_score(best["score"]) != "high":
451
+ return False
452
+
453
+ if source_class == "document" and candidate_has_heavy_repairs(best):
454
+ return False
455
+
456
+ return True
457
+
458
+
459
  def empty_best():
460
  return {
461
  "vin": "",
 
919
  pass_index = 1
920
  fast_scan_passes = build_full_frame_fast_scan_passes(image)
921
  early_angle_done = False
922
+ source_class = "unknown"
923
  image_debug["fast_scan_rotations"] = [
924
  pass_config["rotation"] for pass_config in fast_scan_passes
925
  ]
 
930
  passes_used.append(pass_result)
931
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
932
 
933
+ classification = classify_full_frame_source(pass_result, image_debug)
934
+ source_class = classification["class"]
935
+ image_debug["source_class"] = classification
936
+
937
+ if full_frame_can_early_return(best, source_class):
938
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
939
 
940
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
 
963
  passes_used.append(pass_result)
964
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
965
 
966
+ if full_frame_can_early_return(best, source_class):
967
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
968
 
969
  for pass_config in fast_scan_passes[1:]:
 
975
  passes_used.append(pass_result)
976
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
977
 
978
+ if full_frame_can_early_return(best, source_class):
979
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
980
 
981
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
 
1005
  passes_used.append(pass_result)
1006
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
1007
 
1008
+ if full_frame_can_early_return(best, source_class):
1009
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
1010
 
1011
  if best["score"] >= FULL_FRAME_STOP_LOW_SCORE and candidate_has_stable_vin_shape(best):
 
1020
  passes_used.append(pass_result)
1021
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
1022
 
1023
+ if full_frame_can_early_return(best, source_class):
1024
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
1025
 
1026
  if ocr_budget_exceeded(started_at, MAX_FULL_FRAME_ELAPSED_MS):
 
1042
  passes_used.append(pass_result)
1043
  best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
1044
 
1045
+ if full_frame_can_early_return(best, source_class):
1046
  return build_response(best, all_candidates, passes_used, started_at, image_debug)
1047
 
1048
  return build_response(best, all_candidates, passes_used, started_at, image_debug)