From 7eb03ca8d1834418be902ca8e85a3e84ce72bec7 Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Tue, 3 Mar 2026 13:21:49 +0100 Subject: [PATCH] fix(ocr-pipeline): IndentationError in auto-mode deskew block The try/except block for the deskew step had 4 extra spaces of indentation from a previous edit. Python rejected the file with IndentationError at startup. Co-Authored-By: Claude Sonnet 4.6 --- klausur-service/backend/ocr_pipeline_api.py | 108 ++++++++++---------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/klausur-service/backend/ocr_pipeline_api.py b/klausur-service/backend/ocr_pipeline_api.py index 6224660..9c75d28 100644 --- a/klausur-service/backend/ocr_pipeline_api.py +++ b/klausur-service/backend/ocr_pipeline_api.py @@ -2153,69 +2153,69 @@ async def run_auto(session_id: str, req: RunAutoRequest, request: Request): # ----------------------------------------------------------------- if req.from_step <= 1: yield await _auto_sse_event("deskew", "start", {}) + try: + t0 = time.time() + orig_bgr = cached.get("original_bgr") + if orig_bgr is None: + raise ValueError("Original image not loaded") + + # Method 1: Hough lines try: - t0 = time.time() - orig_bgr = cached.get("original_bgr") - if orig_bgr is None: - raise ValueError("Original image not loaded") + deskewed_hough, angle_hough = deskew_image(orig_bgr.copy()) + except Exception: + deskewed_hough, angle_hough = orig_bgr, 0.0 - # Method 1: Hough lines - try: - deskewed_hough, angle_hough = deskew_image(orig_bgr.copy()) - except Exception: - deskewed_hough, angle_hough = orig_bgr, 0.0 + # Method 2: Word alignment + success_enc, png_orig = cv2.imencode(".png", orig_bgr) + orig_bytes = png_orig.tobytes() if success_enc else b"" + try: + deskewed_wa_bytes, angle_wa = deskew_image_by_word_alignment(orig_bytes) + except Exception: + deskewed_wa_bytes, angle_wa = orig_bytes, 0.0 - # Method 2: Word alignment - success_enc, png_orig = cv2.imencode(".png", orig_bgr) - orig_bytes = png_orig.tobytes() if success_enc else b"" - try: - deskewed_wa_bytes, angle_wa = deskew_image_by_word_alignment(orig_bytes) - except Exception: - deskewed_wa_bytes, angle_wa = orig_bytes, 0.0 - - # Pick best method - if abs(angle_wa) >= abs(angle_hough) or abs(angle_hough) < 0.1: - method_used = "word_alignment" - angle_applied = angle_wa - wa_arr = np.frombuffer(deskewed_wa_bytes, dtype=np.uint8) - deskewed_bgr = cv2.imdecode(wa_arr, cv2.IMREAD_COLOR) - if deskewed_bgr is None: - deskewed_bgr = deskewed_hough - method_used = "hough" - angle_applied = angle_hough - else: + # Pick best method + if abs(angle_wa) >= abs(angle_hough) or abs(angle_hough) < 0.1: + method_used = "word_alignment" + angle_applied = angle_wa + wa_arr = np.frombuffer(deskewed_wa_bytes, dtype=np.uint8) + deskewed_bgr = cv2.imdecode(wa_arr, cv2.IMREAD_COLOR) + if deskewed_bgr is None: + deskewed_bgr = deskewed_hough method_used = "hough" angle_applied = angle_hough - deskewed_bgr = deskewed_hough + else: + method_used = "hough" + angle_applied = angle_hough + deskewed_bgr = deskewed_hough - success, png_buf = cv2.imencode(".png", deskewed_bgr) - deskewed_png = png_buf.tobytes() if success else b"" + success, png_buf = cv2.imencode(".png", deskewed_bgr) + deskewed_png = png_buf.tobytes() if success else b"" - deskew_result = { - "method_used": method_used, - "rotation_degrees": round(float(angle_applied), 3), - "duration_seconds": round(time.time() - t0, 2), - } + deskew_result = { + "method_used": method_used, + "rotation_degrees": round(float(angle_applied), 3), + "duration_seconds": round(time.time() - t0, 2), + } - cached["deskewed_bgr"] = deskewed_bgr - cached["deskew_result"] = deskew_result - await update_session_db( - session_id, - deskewed_png=deskewed_png, - deskew_result=deskew_result, - auto_rotation_degrees=float(angle_applied), - current_step=2, - ) - session = await get_session_db(session_id) + cached["deskewed_bgr"] = deskewed_bgr + cached["deskew_result"] = deskew_result + await update_session_db( + session_id, + deskewed_png=deskewed_png, + deskew_result=deskew_result, + auto_rotation_degrees=float(angle_applied), + current_step=2, + ) + session = await get_session_db(session_id) - steps_run.append("deskew") - yield await _auto_sse_event("deskew", "done", deskew_result) - except Exception as e: - logger.error(f"Auto-mode deskew failed for {session_id}: {e}") - error_step = "deskew" - yield await _auto_sse_event("deskew", "error", {"message": str(e)}) - yield await _auto_sse_event("complete", "error", {"error_step": error_step}) - return + steps_run.append("deskew") + yield await _auto_sse_event("deskew", "done", deskew_result) + except Exception as e: + logger.error(f"Auto-mode deskew failed for {session_id}: {e}") + error_step = "deskew" + yield await _auto_sse_event("deskew", "error", {"message": str(e)}) + yield await _auto_sse_event("complete", "error", {"error_step": error_step}) + return else: steps_skipped.append("deskew") yield await _auto_sse_event("deskew", "skipped", {"reason": "from_step > 1"})