report-detect/archive/tools/find_cma_position.py

93 lines
3.0 KiB
Python

"""
Find the position of CMA code 210020349096
"""
import fitz
import numpy as np
import cv2
from paddleocr import PaddleOCR
import os
import re
os.environ["DISABLE_MODEL_SOURCE_CHECK"] = "True"
pdf_path = "src/test/resources/data/pdfs/YDQ23_001838.pdf"
print("=" * 80)
print("FINDING POSITION OF 210020349096")
print("=" * 80)
# Extract page
doc = fitz.open(pdf_path)
page = doc[0]
mat = fitz.Matrix(300 / 72, 300 / 72)
pix = page.get_pixmap(matrix=mat)
img_data = pix.tobytes("png")
img_array = np.frombuffer(img_data, dtype=np.uint8)
page_img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
doc.close()
h, w = page_img.shape[:2]
print(f"\nPage size: {w}x{h}")
# Run OCR
print("\nRunning full-page OCR...")
ocr = PaddleOCR(lang='ch')
ocr_result = ocr.predict(page_img)
if ocr_result and len(ocr_result) > 0:
res = ocr_result[0]
# Check if result has boxes
if 'boxes' in res:
boxes = res['boxes']
texts = res['rec_texts']
scores = res['rec_scores']
# Find CMA code
for i, (text, score) in enumerate(zip(texts, scores)):
if "210020349096" in text:
print(f"\n✓ Found 210020349096 at line {i}")
print(f" Text: '{text}'")
print(f" Score: {score:.2f}")
# Get box
box = boxes[i]
print(f" Box: {box}")
# Calculate center
if len(box) == 4:
# [[x1,y1], [x2,y1], [x2,y2], [x1,y2]]
x_coords = [p[0] for p in box]
y_coords = [p[1] for p in box]
x_center = int(sum(x_coords) / 4)
y_center = int(sum(y_coords) / 4)
y_min = int(min(y_coords))
y_max = int(max(y_coords))
rel_x = x_center / w * 100
rel_y = y_center / h * 100
print(f" Center: ({x_center}, {y_center}) -> ({rel_x:.1f}%, {rel_y:.1f}%)")
print(f" Y-range: {y_min} - {y_max}")
# Compare with logo position
logo_x, logo_y = 1427, 885
print(f"\n Logo center: ({logo_x}, {logo_y}) -> ({logo_x/w*100:.1f}%, {logo_y/h*100:.1f}%)")
print(f" Difference: X+{x_center - logo_x}, Y+{y_center - logo_y}")
# Current ROI
roi_x1, roi_y1 = 1427, 835
roi_x2, roi_y2 = 2027, 1289
print(f"\n Current ROI: ({roi_x1}, {roi_y1}) -> ({roi_x2}, {roi_y2})")
if x_center < roi_x1 or x_center > roi_x2 or y_center < roi_y1 or y_center > roi_y2:
print(f" ❌ CMA code is OUTSIDE ROI!")
print(f" X: {x_center} not in [{roi_x1}, {roi_x2}]")
print(f" Y: {y_center} not in [{roi_y1}, {roi_y2}]")
else:
print(f" ✓ CMA code is INSIDE ROI")
break
print("\n" + "=" * 80)