#!/usr/bin/env python3
"""Test different approaches to OCR the voter ID"""

import cv2
import os
from google.cloud import vision

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'google-credentials.json'
client = vision.ImageAnnotatorClient()

# Load the extracted voter_id region
region = cv2.imread('/tmp/voter_id_region.png')
print(f"Region size: {region.shape[1]}x{region.shape[0]}")

# Show pixel values to check if image is readable
print(f"Min pixel: {region.min()}, Max pixel: {region.max()}")
print(f"Mean pixel: {region.mean():.1f}")

# Try 1: Original region
print("\n" + "="*80)
print("Test 1: Original region")
_, encoded = cv2.imencode('.png', region)
content = encoded.tobytes()
print(f"Encoded size: {len(content)} bytes")

image = vision.Image(content=content)
response = client.text_detection(image=image)

if response.text_annotations:
    print(f"✓ OCR Result: '{response.text_annotations[0].description.strip()}'")
else:
    print(f"✗ OCR Result: (empty)")

# Try 2: With binary threshold
print("\n" + "="*80)
print("Test 2: With binary threshold (Otsu)")
gray = cv2.cvtColor(region, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imwrite('/tmp/voter_id_binary.png', binary)

_, encoded = cv2.imencode('.png', binary)
content = encoded.tobytes()

image = vision.Image(content=content)
response = client.text_detection(image=image)

if response.text_annotations:
    print(f"✓ OCR Result: '{response.text_annotations[0].description.strip()}'")
else:
    print(f"✗ OCR Result: (empty)")

# Try 3: Inverted
print("\n" + "="*80)
print("Test 3: Inverted")
inverted = cv2.bitwise_not(region)
cv2.imwrite('/tmp/voter_id_inverted.png', inverted)

_, encoded = cv2.imencode('.png', inverted)
content = encoded.tobytes()

image = vision.Image(content=content)
response = client.text_detection(image=image)

if response.text_annotations:
    print(f"✓ OCR Result: '{response.text_annotations[0].description.strip()}'")
else:
    print(f"✗ OCR Result: (empty)")

# Try 4: Larger region (maybe we're cropping too tight?)
print("\n" + "="*80)
print("Test 4: Reading from original cell image directly")
cell = cv2.imread('gogoelocroutput/cells/page003_cell07.png')

# Extract a wider region around voter ID
# Based on the template match, voter_id label is at (7, 49), size (165, 35)
# Let's extract from after label to end of line
voter_id_region = cell[40:95, 170:970]  # Wider margins
cv2.imwrite('/tmp/voter_id_wide.png', voter_id_region)

_, encoded = cv2.imencode('.png', voter_id_region)
content = encoded.tobytes()

image = vision.Image(content=content)
response = client.text_detection(image=image)

if response.text_annotations:
    print(f"✓ OCR Result: '{response.text_annotations[0].description.strip()}'")
else:
    print(f"✗ OCR Result: (empty)")
