import cv2
import pytesseract
import numpy as np
import re


pytesseract.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe"


# get grayscale image
def get_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


# noise removal
def remove_noise(image):
    return cv2.medianBlur(image, 5)


# thresholding
def thresholding(image):
    return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]


# dilation
def dilate(image):
    kernel = np.ones((5, 5), np.uint8)
    return cv2.dilate(image, kernel, iterations=1)


# erosion
def erode(image):
    kernel = np.ones((5, 5), np.uint8)
    return cv2.erode(image, kernel, iterations=1)


# opening - erosion followed by dilation
def opening(image):
    kernel = np.ones((5, 5), np.uint8)
    return cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)


# canny edge detection
def canny(image):
    return cv2.Canny(image, 100, 200)


def getSkewAngle(cvImage) -> float:
    # Prep image, copy, convert to gray scale, blur, and threshold
    newImage = cvImage.copy()
    gray = cv2.cvtColor(newImage, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (9, 9), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Apply dilate to merge text into meaningful lines/paragraphs.
    # Use larger kernel on X axis to merge characters into single line, cancelling out any spaces.
    # But use smaller kernel on Y axis to separate between different blocks of text
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 5))
    dilate = cv2.dilate(thresh, kernel, iterations=5)

    # Find all contours
    contours, hierarchy = cv2.findContours(dilate, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key = cv2.contourArea, reverse = True)

    # Find largest contour and surround in min area box
    largestContour = contours[0]
    minAreaRect = cv2.minAreaRect(largestContour)

    # Determine the angle. Convert it to the value that was originally used to obtain skewed image
    angle = minAreaRect[-1]
    if angle < -45:
        angle = 90 + angle
    return -1.0 * angle

# Rotate the image around its center
def rotateImage(cvImage, angle: float):
    newImage = cvImage.copy()
    (h, w) = newImage.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    newImage = cv2.warpAffine(newImage, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
    return newImage

# Deskew image
def deskew(cvImage):
    angle = getSkewAngle(cvImage)
    return rotateImage(cvImage, -1.0 * angle)
#
# top: value.bboxScaled[1] + "px",
# left: value.bboxScaled[0] + "px",
# width: value.bboxScaled[2] - value.bboxScaled[0] + "px",
# height: value.bboxScaled[3] - value.bboxScaled[1] + "px",

def getText(filename):
    s = [
    217,
    160,
    1505,
    390
]
    Y = s[1]
    X = s[0]
    # W = s[2] - s[0] - 40
    # H = s[3] - s[1] - 220
    W = s[2] - s[0]
    H = s[3] - s[1]
    img = cv2.imread(filename)
    cropped_image = img[Y:Y + H, X:X + W]
    cv2.imshow("cropped_image", cropped_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    grey_img = get_grayscale(cropped_image)
    cv2.imshow("grey_image", grey_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    binary = cv2.adaptiveThreshold(grey_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 7)
    cv2.imshow("binary_image", binary)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


    txt1 = pytesseract.image_to_string(binary, config="-l fra --psm 1 --oem 1")
    txt2 = pytesseract.image_to_string(grey_img, config="-l fra --psm 1 --oem 1")
    txt3 = pytesseract.image_to_string(cropped_image, config="-l fra --psm 1 --oem 1")
    txt4 = pytesseract.image_to_string(binary, config="-l fra --psm 12 --oem 1")
    txt5 = pytesseract.image_to_string(grey_img, config="-l fra --psm 12 --oem 1")
    txt6 = pytesseract.image_to_string(cropped_image, config="-l fra --psm 12 --oem 1")
    txt7 = pytesseract.image_to_string(binary, config="-l fra --psm 6 --oem 1")
    txt8 = pytesseract.image_to_string(grey_img, config="-l fra --psm 6 --oem 1")
    txt9 = pytesseract.image_to_string(cropped_image, config="-l fra --psm 6 --oem 1")

    txt10 = pytesseract.image_to_string(binary, config="-l fra+eng --psm 6 --oem 1")
    txt11 = pytesseract.image_to_string(grey_img, config="-l fra+eng --psm 6 --oem 1")
    txt12 = pytesseract.image_to_string(cropped_image, config="-l fra+eng --psm 6 --oem 1")
    txt13 = pytesseract.image_to_string(binary, config="-l fra+eng --psm 12 --oem 1")
    txt14 = pytesseract.image_to_string(grey_img, config="-l fra+eng --psm 12 --oem 1")
    txt15 = pytesseract.image_to_string(cropped_image, config="-l fra+eng --psm 12 --oem 1")
    txt16 = pytesseract.image_to_string(binary, config="-l fra+eng --psm 1 --oem 1")
    txt17 = pytesseract.image_to_string(grey_img, config="-l fra+eng --psm 1 --oem 1")
    txt18 = pytesseract.image_to_string(cropped_image, config="-l fra+eng --psm 1 --oem 1")
    print(txt1)
    print("----------------")
    print(txt2)
    print("----------------")
    print(txt3)
    print("----------------")
    print(txt4)
    print("----------------")
    print(txt5)
    print("----------------")
    print(txt6)
    print("----------------")
    print(txt7)
    print("----------------")
    print(txt8)
    print("----------------")
    print(txt9)
    print("----------------")
    print(txt10)
    print("----------------")
    print(txt11)
    print("----------------")
    print(txt12)
    print("----------------")
    print(txt13)
    print("----------------")
    print(txt14)
    print("----------------")
    print(txt15)
    print("----------------")
    print(txt16)
    print("----------------")
    print(txt17)
    print("----------------")
    print(txt18)
    # txt = pytesseract.image_to_string(cropped_image, config="-l fra --psm 6  --oem 3")
    # txt = pytesseract.image_to_string(cropped_image, config="-l fra --psm 7 ")

    # osd = pytesseract.image_to_osd(img)
    # angle = re.search('(?<=Rotate: )\d+', osd).group(0)
    # script = re.search('(?<=Script: )\d+', osd).group(0)
    # print("angle: ", angle)
    # print("script: ", script)
    return txt1


text = getText('../static/files/6260df4785e6a5b326b6b390/Page6.jpeg')
print(text)

