import cv2
import pytesseract
import numpy as np

# Path to Tesseract (if necessary, set the correct path on your system)
# pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract"  # Default for Ubuntu

def process_id_card(image, lower_bound=(100, 20, 100), upper_bound=(200, 250, 250)):
    """
    Detects, crops, aligns, enhances an ID card from an input image for OCR.
    Keeps only the top-left 75% of the image.
    """
    if image is None:
        print("Error: Image not loaded correctly.")
        return None

    # Get the image dimensions
    height, width = image.shape[:2]

    # Crop the image to keep only the top-left 75% (height and width both reduced to 75%)
    #cropped_image = image[:, :int(width * 0.8)]
    crop_width = int(width * .7)  # 75% of the width
    crop_height = int(height * .8)  # 50% of the height

    # Crop the image
    cropped_image = image[0:, 0:crop_width]
    #cropped_image = image[ :,1:int(width * 0.7)]

    # Now process the cropped image
    hsv_image = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if contours:
        largest_contour = max(contours, key=cv2.contourArea)
        rect = cv2.minAreaRect(largest_contour)
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        
        # Order points
        box = sorted(box, key=lambda x: (x[1], x[0]))
        top_points = sorted(box[:2], key=lambda x: x[0])
        bottom_points = sorted(box[2:], key=lambda x: x[0])
        
        # Perspective transform
        src_pts = np.array([top_points[0], top_points[1], bottom_points[1], bottom_points[0]], dtype="float32")
        width, height = int(rect[1][0]), int(rect[1][1])
        if width < height:
            width, height = height, width
        dst_pts = np.array([[0, 0], [width - 1, 0], [width - 1, height - 1], [0, height - 1]], dtype="float32")
        
        M = cv2.getPerspectiveTransform(src_pts, dst_pts)
        aligned_id_card = cv2.warpPerspective(cropped_image, M, (width, height))

        gray_id_card = cv2.cvtColor(aligned_id_card, cv2.COLOR_BGR2GRAY)
        enhanced_id_card = cv2.adaptiveThreshold(gray_id_card, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

        return enhanced_id_card
    else:
        print("No matching ID card region found.")
        return None

def ocr_bahasa(image):
    """
    Perform OCR using Tesseract for Bahasa Indonesia.
    """
    custom_config = r'--oem 3 --psm 6 -l ind'  # Bahasa Indonesia language code
    text = pytesseract.image_to_string(image, config=custom_config)
    return text

# Example usage
image = cv2.imread("../ktp3.jpg")

if image is None:
    print("Error: Image not loaded.")
else:
    processed_image = process_id_card(image)

    if processed_image is not None:
        # Save the processed image (optional)
        cv2.imwrite("processed_id_card.jpg", processed_image)

        # Perform OCR on the processed image
        extracted_text = ocr_bahasa(processed_image)
        print("OCR Result (Bahasa Indonesia):")
        print(extracted_text)
