qshub / pdf_engine /scale_detector.py
basel00omar's picture
THE QS HUB v2.0 - Complete React + FastAPI Platform (clean deploy)
1b4b6d2
Raw History Blame Contribute Delete
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)