
from PIL import Image
import pytesseract
import argparse
import cv2
import os
import glob

from pathlib import Path


def ocr_file(filename):
    # load the example image and convert it to grayscale
    image = cv2.imread(filename)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # check to see if we should apply thresholding to preprocess the
    # image

    text = pytesseract.image_to_string(Image.open(filename))

    gray_mask = cv2.threshold(gray, 220, 255,
            cv2.THRESH_BINARY)[1]

    temp_file = "{}.png".format(os.getpid())
    cv2.imwrite(temp_file, gray_mask)

    # load the image as a PIL/Pillow image, apply OCR, and then delete
    # the temporary file
    text = text + pytesseract.image_to_string(Image.open(temp_file))
    os.remove(temp_file)
    #print(text)
    return text

# show the output images
#cv2.imshow("Image", image)
#cv2.imshow("Output", gray)
#cv2.waitKey(0)

def check_text(text):
    text = text.lower()
    strings = ("REF", "RK9", "RBZ", "RA9", "RH8", "Accession", "patient", "nhs")
    if any(s.lower() in text for s in strings):
        return True

def search_image(file_name):
    print("Process: {}".format(file_name))
    img = cv2.imread(file_name)

    img_final = cv2.imread(file_name)
    img2gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(img2gray, 240, 255, cv2.THRESH_BINARY)

    #cv2.imshow("mask", mask)
    
    image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
    #ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY)  # for black text , cv.THRESH_BINARY_INV
    '''
            line  8 to 12  : Remove noisy portion 
    '''
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,
                                                         3))  # to manipulate the orientation of dilution , large x means horizonatally dilating  more, large y means vertically dilating more
    dilated = cv2.dilate(image_final, kernel, iterations=9)  # dilate , more the iteration more the dilation

    # for cv2.x.x

    #_, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # findContours returns 3 variables for getting contours

    # for cv3.x.x comment above line and uncomment line below

    contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)



    for contour in contours:
        # get rectangle bounding contour
        [x, y, w, h] = cv2.boundingRect(contour)

        # Don't plot small false positives that aren't text
        if w < 35 and h < 35:
            continue


        #you can crop image and send to OCR  , false detected will return no text :)
        cropped = image_final[y :y +  h , x : x + w]

        s = 'temp.png' 
        cv2.imwrite(s , cropped)
        text = ocr_file(s)
        os.remove(s)

        #cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

        if check_text(text):
            # draw rectangle around contour on original image
            cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), -1)

            path, fn = file_name.rsplit("/", 1)

            edit_path = "{}/test/".format(path)

            Path(edit_path).mkdir(parents=True, exist_ok=True)

            print(edit_path)
            fn1, fn2 = fn.rsplit(".", 1)
            cv2.imwrite("{}{}_new.{}".format(edit_path, fn1, fn2), img)

            old_img = cv2.imread(file_name)
            cv2.imwrite("{}{}".format(edit_path, fn), old_img)


    # write original image with added contours to disk
    #cv2.imshow('captcha_result', img)
    #cv2.waitKey()



#file_name = 'ankle4.jpg'
#captch_ex(file_name)
#
#file_name = 'ankle1_n2mBhsm.jpg'
#captch_ex(file_name)
#ocr_file("ankle4.jpg")

# file_name = 'chest_UUSUv8E.jpg'
# captch_ex(file_name)
#file_name = 'l1_DvMrQpv.jpg'
#captch_ex(file_name)
#file_name = 'renalmets_ct.jpg'
#captch_ex(file_name)
image_list = []

file_types = ("gif", "jpg", "jpeg", "png")
for file_type in file_types:
    for filename in glob.glob('/home/ross/scripts/sites/backups/New/media/rapids/*.{}'.format(file_type)): #assuming gif
        image_list.append(filename)

for f in image_list:
    search_image(f)
