Upload app.py
Browse files
app.py
CHANGED
|
@@ -66,6 +66,14 @@ MIN_FULL_FRAME_OCR_HEIGHT = 150
|
|
| 66 |
MAX_FULL_FRAME_UPSCALE = 1.0
|
| 67 |
MAX_FULL_FRAME_LONG_SIDE = 1400
|
| 68 |
MAX_FULL_FRAME_PIXELS = 1_600_000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
@app.get("/")
|
|
@@ -89,13 +97,161 @@ async def debug_upload(file: UploadFile = File(...)):
|
|
| 89 |
|
| 90 |
|
| 91 |
def rotate_image(image, angle):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if angle == 90:
|
| 93 |
return cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
| 94 |
if angle == 180:
|
| 95 |
return cv2.rotate(image, cv2.ROTATE_180)
|
| 96 |
if angle == 270:
|
| 97 |
return cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
def preprocess_image(image, preprocess):
|
|
@@ -262,6 +418,7 @@ def run_ocr_pass(image, pass_config, index, source="unknown"):
|
|
| 262 |
"stage": pass_config["stage"],
|
| 263 |
"rotation": rotation,
|
| 264 |
"preprocess": preprocess,
|
|
|
|
| 265 |
"elapsed_ms": elapsed_ms,
|
| 266 |
"raw_text": raw_text,
|
| 267 |
"cleaned_text": cleaned_text,
|
|
@@ -295,7 +452,7 @@ def first_pass_gate_reasons(pass_result):
|
|
| 295 |
|
| 296 |
|
| 297 |
def pass_summary(pass_result):
|
| 298 |
-
|
| 299 |
"index": pass_result["index"],
|
| 300 |
"stage": pass_result["stage"],
|
| 301 |
"rotation": pass_result["rotation"],
|
|
@@ -311,6 +468,11 @@ def pass_summary(pass_result):
|
|
| 311 |
"best": pass_result["best"],
|
| 312 |
}
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
def pass_has_vin_signal(pass_result):
|
| 316 |
return (
|
|
@@ -346,6 +508,34 @@ def unique_promising_rotations(passes_used):
|
|
| 346 |
return rotations
|
| 347 |
|
| 348 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
def crop_first_pass_is_empty(pass_result):
|
| 350 |
return (
|
| 351 |
pass_result["cleaned_text_length"] < MIN_FIRST_PASS_TEXT_LENGTH
|
|
@@ -484,6 +674,36 @@ def run_crop_strategy(image, source_mode, started_at, image_debug):
|
|
| 484 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 485 |
|
| 486 |
if index == 2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
if all_candidates:
|
| 488 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 489 |
|
|
@@ -532,6 +752,19 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
|
|
| 532 |
image_debug,
|
| 533 |
)
|
| 534 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
for pass_config in FULL_FRAME_FOLLOWUP_PASSES:
|
| 536 |
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 537 |
pass_index += 1
|
|
@@ -541,7 +774,7 @@ def run_full_frame_strategy(image, source_mode, started_at, image_debug):
|
|
| 541 |
if confidence_from_score(best["score"]) == "high":
|
| 542 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 543 |
|
| 544 |
-
promising_rotations =
|
| 545 |
|
| 546 |
for rotation in promising_rotations:
|
| 547 |
pass_config = {
|
|
|
|
| 66 |
MAX_FULL_FRAME_UPSCALE = 1.0
|
| 67 |
MAX_FULL_FRAME_LONG_SIDE = 1400
|
| 68 |
MAX_FULL_FRAME_PIXELS = 1_600_000
|
| 69 |
+
ANGLE_PROBE_MIN_ABS = 12
|
| 70 |
+
ANGLE_PROBE_MAX_ABS = 60
|
| 71 |
+
ANGLE_PROBE_BUCKET_DEGREES = 5
|
| 72 |
+
ANGLE_PROBE_MAX_PASSES = 3
|
| 73 |
+
ANGLE_PROBE_DUPLICATE_TOLERANCE = 3
|
| 74 |
+
MAX_ARBITRARY_ROTATION_LONG_SIDE = 1800
|
| 75 |
+
MAX_ARBITRARY_ROTATION_PIXELS = 2_000_000
|
| 76 |
+
MAX_DEEP_PROMISING_ROTATIONS = 3
|
| 77 |
|
| 78 |
|
| 79 |
@app.get("/")
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
def rotate_image(image, angle):
|
| 100 |
+
normalized = angle % 360
|
| 101 |
+
|
| 102 |
+
if normalized == 0:
|
| 103 |
+
return image
|
| 104 |
+
|
| 105 |
if angle == 90:
|
| 106 |
return cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
| 107 |
if angle == 180:
|
| 108 |
return cv2.rotate(image, cv2.ROTATE_180)
|
| 109 |
if angle == 270:
|
| 110 |
return cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
| 111 |
+
|
| 112 |
+
height, width = image.shape[:2]
|
| 113 |
+
center = (width / 2, height / 2)
|
| 114 |
+
matrix = cv2.getRotationMatrix2D(center, -angle, 1)
|
| 115 |
+
cosine = abs(matrix[0, 0])
|
| 116 |
+
sine = abs(matrix[0, 1])
|
| 117 |
+
rotated_width = int((height * sine) + (width * cosine))
|
| 118 |
+
rotated_height = int((height * cosine) + (width * sine))
|
| 119 |
+
|
| 120 |
+
matrix[0, 2] += (rotated_width / 2) - center[0]
|
| 121 |
+
matrix[1, 2] += (rotated_height / 2) - center[1]
|
| 122 |
+
|
| 123 |
+
rotated = cv2.warpAffine(
|
| 124 |
+
image,
|
| 125 |
+
matrix,
|
| 126 |
+
(rotated_width, rotated_height),
|
| 127 |
+
flags=cv2.INTER_LINEAR,
|
| 128 |
+
borderMode=cv2.BORDER_CONSTANT,
|
| 129 |
+
borderValue=(255, 255, 255),
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
pixel_count = max(rotated_width * rotated_height, 1)
|
| 133 |
+
downscale = min(
|
| 134 |
+
MAX_ARBITRARY_ROTATION_LONG_SIDE / max(rotated_width, rotated_height),
|
| 135 |
+
(MAX_ARBITRARY_ROTATION_PIXELS / pixel_count) ** 0.5,
|
| 136 |
+
1,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
if downscale >= 1:
|
| 140 |
+
return rotated
|
| 141 |
+
|
| 142 |
+
return cv2.resize(
|
| 143 |
+
rotated,
|
| 144 |
+
(round(rotated_width * downscale), round(rotated_height * downscale)),
|
| 145 |
+
interpolation=cv2.INTER_AREA,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def normalized_small_angle(angle):
|
| 150 |
+
while angle <= -180:
|
| 151 |
+
angle += 360
|
| 152 |
+
while angle > 180:
|
| 153 |
+
angle -= 360
|
| 154 |
+
return angle
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def line_angle_degrees(x1, y1, x2, y2):
|
| 158 |
+
angle = np.degrees(np.arctan2(y2 - y1, x2 - x1))
|
| 159 |
+
|
| 160 |
+
while angle <= -90:
|
| 161 |
+
angle += 180
|
| 162 |
+
while angle > 90:
|
| 163 |
+
angle -= 180
|
| 164 |
+
|
| 165 |
+
return angle
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def detect_skew_probe_rotations(image):
|
| 169 |
+
height, width = image.shape[:2]
|
| 170 |
+
max_side = max(height, width)
|
| 171 |
+
scale = min(1, 900 / max(max_side, 1))
|
| 172 |
+
sample = image
|
| 173 |
+
|
| 174 |
+
if scale < 1:
|
| 175 |
+
sample = cv2.resize(
|
| 176 |
+
image,
|
| 177 |
+
(round(width * scale), round(height * scale)),
|
| 178 |
+
interpolation=cv2.INTER_AREA,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
gray = cv2.cvtColor(sample, cv2.COLOR_BGR2GRAY)
|
| 182 |
+
gray = cv2.GaussianBlur(gray, (3, 3), 0)
|
| 183 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 184 |
+
sample_height, sample_width = sample.shape[:2]
|
| 185 |
+
min_line_length = max(40, round(min(sample_width, sample_height) * 0.14))
|
| 186 |
+
lines = cv2.HoughLinesP(
|
| 187 |
+
edges,
|
| 188 |
+
1,
|
| 189 |
+
np.pi / 180,
|
| 190 |
+
threshold=60,
|
| 191 |
+
minLineLength=min_line_length,
|
| 192 |
+
maxLineGap=16,
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
if lines is None:
|
| 196 |
+
return []
|
| 197 |
+
|
| 198 |
+
buckets = {}
|
| 199 |
+
for line in lines:
|
| 200 |
+
x1, y1, x2, y2 = line[0]
|
| 201 |
+
length = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
|
| 202 |
+
if length < min_line_length:
|
| 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 |
+
|
| 209 |
+
bucket = round(angle / ANGLE_PROBE_BUCKET_DEGREES) * ANGLE_PROBE_BUCKET_DEGREES
|
| 210 |
+
buckets[bucket] = buckets.get(bucket, 0) + length
|
| 211 |
+
|
| 212 |
+
rotations = []
|
| 213 |
+
for angle, _weight in sorted(buckets.items(), key=lambda item: item[1], reverse=True):
|
| 214 |
+
rotation = normalized_small_angle(-angle)
|
| 215 |
+
if ANGLE_PROBE_MIN_ABS <= abs(rotation) <= ANGLE_PROBE_MAX_ABS:
|
| 216 |
+
rotations.append(rotation)
|
| 217 |
+
if len(rotations) >= 2:
|
| 218 |
+
break
|
| 219 |
+
|
| 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:
|
| 226 |
+
return
|
| 227 |
+
|
| 228 |
+
for existing in rotations:
|
| 229 |
+
delta = abs(normalized_small_angle(existing - rotation))
|
| 230 |
+
if delta <= ANGLE_PROBE_DUPLICATE_TOLERANCE:
|
| 231 |
+
return
|
| 232 |
+
|
| 233 |
+
rotations.append(rotation)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
def build_angle_probe_passes(image, stage, include_fixed_45=True):
|
| 237 |
+
rotations = []
|
| 238 |
+
|
| 239 |
+
for rotation in detect_skew_probe_rotations(image):
|
| 240 |
+
add_unique_rotation(rotations, rotation)
|
| 241 |
+
|
| 242 |
+
if include_fixed_45:
|
| 243 |
+
add_unique_rotation(rotations, 45)
|
| 244 |
+
add_unique_rotation(rotations, -45)
|
| 245 |
+
|
| 246 |
+
return [
|
| 247 |
+
{
|
| 248 |
+
"stage": stage,
|
| 249 |
+
"rotation": rotation,
|
| 250 |
+
"preprocess": "original",
|
| 251 |
+
"angle_source": "auto_or_45",
|
| 252 |
+
}
|
| 253 |
+
for rotation in rotations[:ANGLE_PROBE_MAX_PASSES]
|
| 254 |
+
]
|
| 255 |
|
| 256 |
|
| 257 |
def preprocess_image(image, preprocess):
|
|
|
|
| 418 |
"stage": pass_config["stage"],
|
| 419 |
"rotation": rotation,
|
| 420 |
"preprocess": preprocess,
|
| 421 |
+
"angle_source": pass_config.get("angle_source", ""),
|
| 422 |
"elapsed_ms": elapsed_ms,
|
| 423 |
"raw_text": raw_text,
|
| 424 |
"cleaned_text": cleaned_text,
|
|
|
|
| 452 |
|
| 453 |
|
| 454 |
def pass_summary(pass_result):
|
| 455 |
+
summary = {
|
| 456 |
"index": pass_result["index"],
|
| 457 |
"stage": pass_result["stage"],
|
| 458 |
"rotation": pass_result["rotation"],
|
|
|
|
| 468 |
"best": pass_result["best"],
|
| 469 |
}
|
| 470 |
|
| 471 |
+
if pass_result.get("angle_source"):
|
| 472 |
+
summary["angle_source"] = pass_result["angle_source"]
|
| 473 |
+
|
| 474 |
+
return summary
|
| 475 |
+
|
| 476 |
|
| 477 |
def pass_has_vin_signal(pass_result):
|
| 478 |
return (
|
|
|
|
| 508 |
return rotations
|
| 509 |
|
| 510 |
|
| 511 |
+
def ranked_promising_rotations(passes_used, limit=MAX_DEEP_PROMISING_ROTATIONS):
|
| 512 |
+
by_rotation = {}
|
| 513 |
+
|
| 514 |
+
for order, pass_result in enumerate(passes_used):
|
| 515 |
+
if not pass_has_vin_signal(pass_result):
|
| 516 |
+
continue
|
| 517 |
+
|
| 518 |
+
rotation = pass_result["rotation"]
|
| 519 |
+
best = pass_result.get("best") or {}
|
| 520 |
+
score = best.get("score", -999)
|
| 521 |
+
current = by_rotation.get(rotation)
|
| 522 |
+
|
| 523 |
+
if current is None or score > current["score"]:
|
| 524 |
+
by_rotation[rotation] = {
|
| 525 |
+
"rotation": rotation,
|
| 526 |
+
"score": score,
|
| 527 |
+
"order": order,
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
ranked = sorted(
|
| 531 |
+
by_rotation.values(),
|
| 532 |
+
key=lambda item: (item["score"], -item["order"]),
|
| 533 |
+
reverse=True,
|
| 534 |
+
)
|
| 535 |
+
|
| 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
|
|
|
|
| 674 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 675 |
|
| 676 |
if index == 2:
|
| 677 |
+
if best["score"] < MEDIUM_SCORE and any(pass_has_vin_signal(item) for item in passes_used):
|
| 678 |
+
next_index = index + 1
|
| 679 |
+
for angle_pass in build_angle_probe_passes(
|
| 680 |
+
image,
|
| 681 |
+
"crop_angle_probe",
|
| 682 |
+
include_fixed_45=False,
|
| 683 |
+
):
|
| 684 |
+
pass_result = run_ocr_pass(
|
| 685 |
+
image,
|
| 686 |
+
angle_pass,
|
| 687 |
+
next_index,
|
| 688 |
+
source=source_mode,
|
| 689 |
+
)
|
| 690 |
+
next_index += 1
|
| 691 |
+
passes_used.append(pass_result)
|
| 692 |
+
best, all_candidates = update_best_from_pass(
|
| 693 |
+
pass_result,
|
| 694 |
+
best,
|
| 695 |
+
all_candidates,
|
| 696 |
+
)
|
| 697 |
+
|
| 698 |
+
if confidence_from_score(best["score"]) == "high":
|
| 699 |
+
return build_response(
|
| 700 |
+
best,
|
| 701 |
+
all_candidates,
|
| 702 |
+
passes_used,
|
| 703 |
+
started_at,
|
| 704 |
+
image_debug,
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
if all_candidates:
|
| 708 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 709 |
|
|
|
|
| 752 |
image_debug,
|
| 753 |
)
|
| 754 |
|
| 755 |
+
for pass_config in build_angle_probe_passes(
|
| 756 |
+
image,
|
| 757 |
+
"angle_probe",
|
| 758 |
+
include_fixed_45=True,
|
| 759 |
+
):
|
| 760 |
+
pass_result = run_ocr_pass(image, pass_config, pass_index, source=source_mode)
|
| 761 |
+
pass_index += 1
|
| 762 |
+
passes_used.append(pass_result)
|
| 763 |
+
best, all_candidates = update_best_from_pass(pass_result, best, all_candidates)
|
| 764 |
+
|
| 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
|
|
|
|
| 774 |
if confidence_from_score(best["score"]) == "high":
|
| 775 |
return build_response(best, all_candidates, passes_used, started_at, image_debug)
|
| 776 |
|
| 777 |
+
promising_rotations = ranked_promising_rotations(passes_used)
|
| 778 |
|
| 779 |
for rotation in promising_rotations:
|
| 780 |
pass_config = {
|