Download pdf_engine/scale_detector.py from Basel0/qshub: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/spaces/Basel0/qshub/resolve/main/pdf_engine/scale_detector.py
- Command line
-
hf download hf://spaces/Basel0/qshub/pdf_engine/scale_detector.py
-
curl -L -o scale_detector.py https://huggingface.co/spaces/Basel0/qshub/resolve/main/pdf_engine/scale_detector.py
3.93 kB
| """ | |
| ูุดู ู ููุงุณ ุงูุฑุณู ู ู scale bar | |
| ุงูุฃููููุฉ: 1) ูุต ู ุซู "1:100" 2) scale bar ุฑุณูู ู | |
| """ | |
| import cv2 | |
| import numpy as np | |
| import re | |
| from typing import Optional, Tuple | |
| def detect_scale_from_text(page_array: np.ndarray) -> Optional[float]: | |
| """ | |
| ูุจุญุซ ุนู ูุต ู ุซู "1:100" ุฃู "SCALE 1:200" ูู ุงูุตูุฑุฉ ุจุงุณุชุฎุฏุงู OCR | |
| Returns: pixels_per_meter ุฃู None ูู ู ุง ููู | |
| """ | |
| try: | |
| from pdf_engine.ocr_engine import extract_all_text, detect_scale_text_ocr | |
| texts = extract_all_text(page_array) | |
| scale_text = detect_scale_text_ocr(texts) | |
| if scale_text: | |
| return calculate_ppm(scale_text) | |
| except Exception as e: | |
| print(f"[Scale OCR Error] {e}") | |
| return None | |
| def detect_scale_bar_visual(page_array: np.ndarray) -> Optional[Tuple[float, dict]]: | |
| """ | |
| ูุญุงูู ููุดู ุงูู scale bar ุงูุจุตุฑู ูู ุงูู ุฎุทุท | |
| ูุฏูุฑ ุนูู ุฎุท ุฃููู ู ุน ูุต ุฃุฑูุงู ุจุฌุงูุจู | |
| Returns: (pixels_per_meter, debug_info) ุฃู None | |
| """ | |
| gray = cv2.cvtColor(page_array, cv2.COLOR_BGR2GRAY) | |
| h, w = gray.shape | |
| # Search in bottom 20% of page (where scale bars usually are) | |
| bottom_region = gray[int(h * 0.80):, :] | |
| # Detect horizontal lines (potential scale bars) | |
| edges = cv2.Canny(bottom_region, 50, 150) | |
| lines = cv2.HoughLinesP( | |
| edges, 1, np.pi/180, | |
| threshold=80, | |
| minLineLength=int(w * 0.05), | |
| maxLineGap=10 | |
| ) | |
| if lines is None: | |
| return None | |
| # Find longest horizontal line โ likely the scale bar | |
| best_line = None | |
| best_length = 0 | |
| for line in lines: | |
| x1, y1, x2, y2 = line[0] | |
| # Check if horizontal (angle < 5 degrees) | |
| if abs(y2 - y1) < 5: | |
| length = abs(x2 - x1) | |
| if length > best_length: | |
| best_length = length | |
| best_line = (x1, y1, x2, y2) | |
| if best_line is None or best_length < 50: | |
| return None | |
| debug = { | |
| "line": best_line, | |
| "pixel_length": best_length, | |
| "region": "bottom_20_percent" | |
| } | |
| # Without OCR we can't know the real-world length | |
| # Return pixel length and let user confirm | |
| return (best_length, debug) | |
| def calculate_ppm(scale_text: str, bar_pixel_length: float = None) -> Optional[float]: | |
| """ | |
| ูุญุณุจ pixels per meter ู ู ูุต ุงูู ููุงุณ | |
| scale_text: ู ุซู "1:100" ุฃู "1:200" | |
| bar_pixel_length: ุทูู ุงูู scale bar ุจุงูุจูุณู (ูู ู ุชุงุญ) | |
| Returns: pixels_per_meter | |
| """ | |
| # Parse scale ratio like 1:100 | |
| match = re.search(r'1\s*[:/]\s*(\d+)', scale_text) | |
| if not match: | |
| return None | |
| ratio = int(match.group(1)) # e.g., 100 from "1:100" | |
| if bar_pixel_length and bar_pixel_length > 0: | |
| # If we know bar represents e.g. 1m at 1:100 | |
| # pixels_per_meter = bar_pixel_length (if bar = 1m in drawing) | |
| # More commonly bar = some round number like 5m | |
| # This will be refined in Prompt 5 with OCR | |
| pixels_per_meter = bar_pixel_length # placeholder | |
| return pixels_per_meter | |
| # Fallback: estimate from DPI and scale ratio | |
| # At 150 DPI: 1 inch = 150 pixels = 25.4mm real | |
| # At scale 1:100: 25.4mm drawing = 2540mm real = 2.54m | |
| # pixels_per_meter = 150 / (25.4 * ratio / 1000) | |
| dpi = 150 | |
| pixels_per_mm_drawing = dpi / 25.4 | |
| pixels_per_meter = (pixels_per_mm_drawing * 1000) / ratio | |
| return round(pixels_per_meter, 4) | |
| def manual_scale_override(known_distance_pixels: float, known_distance_meters: float) -> float: | |
| """ | |
| ูุญุณุจ pixels per meter ู ู ููุงุณ ูุฏูู ู ุนุฑูู | |
| ุงูู ุณุชุฎุฏู ูุฎุชุงุฑ ููุทุชูู ุนูู ุงูู ุฎุทุท ููุฏุฎู ุงูุจุนุฏ ุงูุญูููู | |
| """ | |
| if known_distance_meters <= 0 or known_distance_pixels <= 0: | |
| return 0.0 | |
| return round(known_distance_pixels / known_distance_meters, 4) | |