feat: automatische Orientierungserkennung fuer umgedrehte Scans
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 23s
CI / test-go-edu-search (push) Successful in 25s
CI / test-python-klausur (push) Failing after 1m50s
CI / test-python-agent-core (push) Successful in 17s
CI / test-nodejs-website (push) Successful in 15s
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 23s
CI / test-go-edu-search (push) Successful in 25s
CI / test-python-klausur (push) Failing after 1m50s
CI / test-python-agent-core (push) Successful in 17s
CI / test-nodejs-website (push) Successful in 15s
Tesseract OSD erkennt 0/90/180/270° Rotation und korrigiert automatisch vor dem Deskew. Loest das Problem mit Buchscannern, bei denen jede 2. Seite auf dem Kopf steht. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -224,6 +224,54 @@ def render_image_high_res(image_data: bytes) -> np.ndarray:
|
||||
return img_bgr
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Stage 1b: Orientation Detection (0°/90°/180°/270°)
|
||||
# =============================================================================
|
||||
|
||||
def detect_and_fix_orientation(img_bgr: np.ndarray) -> Tuple[np.ndarray, int]:
|
||||
"""Detect page orientation via Tesseract OSD and rotate if needed.
|
||||
|
||||
Handles upside-down scans (180°) common with book scanners where
|
||||
every other page is flipped due to the scanner hinge.
|
||||
|
||||
Returns:
|
||||
(corrected_image, rotation_degrees) — rotation is 0, 90, 180, or 270.
|
||||
"""
|
||||
if pytesseract is None:
|
||||
return img_bgr, 0
|
||||
|
||||
try:
|
||||
# Tesseract OSD needs a grayscale or RGB image
|
||||
gray = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)
|
||||
pil_img = Image.fromarray(gray)
|
||||
|
||||
osd = pytesseract.image_to_osd(pil_img, output_type=pytesseract.Output.DICT)
|
||||
rotate = osd.get("rotate", 0)
|
||||
confidence = osd.get("orientation_conf", 0.0)
|
||||
|
||||
logger.info(f"OSD: orientation={rotate}° confidence={confidence:.1f}")
|
||||
|
||||
if rotate == 0 or confidence < 1.0:
|
||||
return img_bgr, 0
|
||||
|
||||
# Apply rotation
|
||||
if rotate == 180:
|
||||
corrected = cv2.rotate(img_bgr, cv2.ROTATE_180)
|
||||
elif rotate == 90:
|
||||
corrected = cv2.rotate(img_bgr, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
||||
elif rotate == 270:
|
||||
corrected = cv2.rotate(img_bgr, cv2.ROTATE_90_CLOCKWISE)
|
||||
else:
|
||||
return img_bgr, 0
|
||||
|
||||
logger.info(f"OSD: rotated {rotate}° to fix orientation")
|
||||
return corrected, rotate
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"OSD orientation detection failed: {e}")
|
||||
return img_bgr, 0
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Stage 2: Deskew (Rotation Correction)
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user