import cv2
import pytesseract
from pytesseract import Output
import UtilPack as util

img_path = util.NormalizePath('/Volumes/ExSSD/Working/용공추 사진/2,3월 데이터/Pole/20250307_153821.jpg')

# 이미지 경로
img = cv2.imread(img_path)

# 전처리
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
thresh = cv2.adaptiveThreshold(
    blur, 255,
    cv2.ADAPTIVE_THRESH_MEAN_C,
    cv2.THRESH_BINARY_INV,
    11, 2
)

# OCR
custom_config = r'--oem 3 --psm 6'
data = pytesseract.image_to_data(
    thresh, lang='kor+eng',
    config=custom_config,
    output_type=Output.DICT
)

# 보안등 아래 숫자 찾기
target_text = '보안등'
number_below = None

for i, word in enumerate(data['text']):
    if word.strip() == target_text:
        for j in range(i + 1, len(data['text'])):
            if data['text'][j].strip().isdigit() and data['top'][j] > data['top'][i]:
                number_below = data['text'][j].strip()
                break
        break

print(f"보안등 아래 숫자: {number_below}")

# 디버깅용: 전체 텍스트 출력
print("\n전체 인식 텍스트:")
for word in data['text']:
    if word.strip():
        print(word.strip())