上传前人工裁剪错题区域,OCR 原文排除手写作答。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-28 16:01:46 +08:00
parent 23be608521
commit acfe002fbf
18 changed files with 975 additions and 448 deletions
+48
View File
@@ -107,3 +107,51 @@ def split_solution_sections(text: str) -> tuple[str | None, str]:
approach = parts[0].replace("## 解题思路", "").strip()
rest = "## " + parts[1]
return approach or None, rest.strip()
def union_bbox(bboxes: list[list[float]], img_w: int, img_h: int, padding_ratio: float = 0.06) -> list[int]:
if not bboxes:
return [0, 0, img_w, img_h]
x1 = min(b[0] for b in bboxes)
y1 = min(b[1] for b in bboxes)
x2 = max(b[2] for b in bboxes)
y2 = max(b[3] for b in bboxes)
pad_x = max(8, (x2 - x1) * padding_ratio)
pad_y = max(8, (y2 - y1) * padding_ratio)
return [
int(max(0, x1 - pad_x)),
int(max(0, y1 - pad_y)),
int(min(img_w, x2 + pad_x)),
int(min(img_h, y2 + pad_y)),
]
def cropped_rel_path(original_rel: str) -> str:
p = Path(original_rel)
return str(p.parent / f"{p.stem}_crop.jpg")
def crop_wrong_region(
src_path: str,
lines: list[dict],
wrong_ids: list[int],
dest_rel_path: str,
img_width: int,
img_height: int,
) -> str | None:
if not wrong_ids:
return None
bboxes = [lines[i].get("bbox") or [0, 0, 0, 0] for i in wrong_ids if i < len(lines)]
if not bboxes:
return None
box = union_bbox(bboxes, img_width, img_height, padding_ratio=0.12)
x1, y1, x2, y2 = box
if x2 <= x1 or y2 <= y1:
return None
img = Image.open(src_path).convert("RGB")
cropped = img.crop((x1, y1, x2, y2))
full_path = Path(settings.UPLOAD_DIR) / dest_rel_path
full_path.parent.mkdir(parents=True, exist_ok=True)
cropped.save(full_path, format="JPEG", quality=92)
return dest_rel_path