import tkinter.messagebox
from tkinter import *
import tkinter as tk
from tkinter import filedialog
import numpy
import pytesseract #Python wrapper for Google-owned OCR engine known by the name of Tesseract.
import cv2
from PIL import Image, ImageTk
import os

root = tk.Tk()
root.title("Object Character Recognizer")
root.geometry("1280x720")
test_image = None


def browse_image():
    fin = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image File", filetypes=(("PNG Files", "*.png"), ("JPG Files", "*.jpg"), ("All Files",  "*.*")))
    global test_image
    image = Image.open(fin)
    test_image = image
    img = ImageTk.PhotoImage(image.resize((650, 400)))
    lb = tk.Label(image=img)
    lb.place(x=25, y=50)
    root.mainloop()

def use_ocr_default():
    try:
        global test_image
        messge = None
        #OEM stands for OCR Engine Mode and PSM stands for Page Segmentation Mode.
        #OEM defines what kind of OCR engine is to be used (this defines the dataset that would be used to cross-match
        #the available data with the testing data).
        #PSM defines how Tesseract will treat the image that supposedly contains characters and how it will extract the
        #data from the image.
        tess = pytesseract.image_to_string(test_image, config='-l eng --oem 1 --psm 3')
        label = Label(messge, text='Result:')
        label.place(x=850, y=320)
        display_message = Text(messge, width=46, height=15)
        display_message.insert(END, str(tess))
        display_message.config(state=DISABLED)
        display_message.delete(0, END)
        display_message.place(x=890, y=330)

    except: #Print a error message when the user inputs an incompatible image.
        tkinter.messagebox.showinfo('Something\'s Wrong!', 'Your picture may not contain English characters or you may have not selected a picture. Please select a picture with detectable English characters.')

def use_ocr_handwriting():
    try:
        global test_image
        opencv_img = numpy.array(test_image)
        opencv_img = opencv_img[:, :, ::-1].copy() #This line is used to convert RGB PIL image file to BGR cv2 image file.
        blurred_img = cv2.medianBlur(opencv_img, 5)
        gray_img = cv2.cvtColor(blurred_img, cv2.COLOR_BGR2GRAY)
        thresh, binary = cv2.threshold(gray_img, 122, 255, cv2.THRESH_BINARY)
        messge = None
        tess = pytesseract.image_to_string(binary, config='-l eng --oem 1 --psm 3')
        label = Label(messge, text='Result:')
        label.place(x=850, y=320)
        display_message = Text(messge, width=46, height=15)
        display_message.insert(END, str(tess))
        display_message.config(state=DISABLED)
        display_message.delete(0, END)
        display_message.place(x=890, y=330)

    except:
        tkinter.messagebox.showinfo('Something\'s Wrong!', 'Your picture may not contain English characters or you may have not selected a picture. Please select a picture with detectable English characters.')

def use_ocr_singletext():
    try:
        global test_image
        messge = None
        tess = pytesseract.image_to_string(test_image, config='-l eng --oem 1 --psm 7')
        label = Label(messge, text='Result:')
        label.place(x=850, y=320)
        display_message = Text(messge, width=46, height=15)
        display_message.insert(END, str(tess))
        display_message.config(state=DISABLED)
        display_message.delete(0, END)
        display_message.place(x=890, y=330)

    except:
        tkinter.messagebox.showinfo('Something\'s Wrong!', 'Your picture may not contain English characters or you may have not selected a picture. Please select a picture with detectable English characters.')

w = tk.LabelFrame(root, text="Image:", width=768, height=600)
w.place(x=20, y=10)
w.pack_propagate(0)
w1 = tk.LabelFrame(root, text="Extracted Text:", width=500, height=310)
w1.place(x=800, y=300)
w2 = tk.LabelFrame(root, text="Operations:", width=350, height=280)
w2.place(x=800, y=10)
btn1 = tk.Button(w2, text="Load Image", padx=40, pady=10, command=browse_image)
btn1.place(x=22, y=20)
btn1 = tk.Button(w2, text="Run Handwritten OCR", padx=40, pady=10, command=use_ocr_handwriting)
btn1.place(x=22, y=80)
btn1 = tk.Button(w2, text="Run Default OCR", padx=40, pady=10, command=use_ocr_default)
btn1.place(x=22, y=140)
btn1 = tk.Button(w2, text="Run Single Text OCR", padx=40, pady=10, command=use_ocr_singletext)
btn1.place(x=22, y=200)
root.mainloop()

[((11, 7, 11, 14), 'tkinter.Tk', 'tk.Tk', ({}, {}), '()', True, 'import tkinter as tk\n'), ((85, 4, 85, 61), 'tkinter.LabelFrame', 'tk.LabelFrame', (), '', True, 'import tkinter as tk\n'), ((88, 5, 88, 71), 'tkinter.LabelFrame', 'tk.LabelFrame', (), '', True, 'import tkinter as tk\n'), ((90, 5, 90, 67), 'tkinter.LabelFrame', 'tk.LabelFrame', (), '', True, 'import tkinter as tk\n'), ((92, 7, 92, 79), 'tkinter.Button', 'tk.Button', (), '', True, 'import tkinter as tk\n'), ((94, 7, 94, 95), 'tkinter.Button', 'tk.Button', (), '', True, 'import tkinter as tk\n'), ((96, 7, 96, 87), 'tkinter.Button', 'tk.Button', (), '', True, 'import tkinter as tk\n'), ((98, 7, 98, 94), 'tkinter.Button', 'tk.Button', (), '', True, 'import tkinter as tk\n'), ((20, 12, 20, 27), 'PIL.Image.open', 'Image.open', ({(20, 23, 20, 26): 'fin'}, {}), '(fin)', False, 'from PIL import Image, ImageTk\n'), ((23, 9, 23, 28), 'tkinter.Label', 'tk.Label', (), '', True, 'import tkinter as tk\n'), ((36, 15, 36, 87), 'pytesseract.image_to_string', 'pytesseract.image_to_string', (), '', False, 'import pytesseract\n'), ((51, 21, 51, 44), 'numpy.array', 'numpy.array', ({(51, 33, 51, 43): 'test_image'}, {}), '(test_image)', False, 'import numpy\n'), ((53, 22, 53, 51), 'cv2.medianBlur', 'cv2.medianBlur', ({(53, 37, 53, 47): 'opencv_img', (53, 49, 53, 50): '5'}, {}), '(opencv_img, 5)', False, 'import cv2\n'), ((54, 19, 54, 64), 'cv2.cvtColor', 'cv2.cvtColor', ({(54, 32, 54, 43): 'blurred_img', (54, 45, 54, 63): 'cv2.COLOR_BGR2GRAY'}, {}), '(blurred_img, cv2.COLOR_BGR2GRAY)', False, 'import cv2\n'), ((55, 25, 55, 77), 'cv2.threshold', 'cv2.threshold', ({(55, 39, 55, 47): 'gray_img', (55, 49, 55, 52): '122', (55, 54, 55, 57): '255', (55, 59, 55, 76): 'cv2.THRESH_BINARY'}, {}), '(gray_img, 122, 255, cv2.THRESH_BINARY)', False, 'import cv2\n'), ((57, 15, 57, 83), 'pytesseract.image_to_string', 'pytesseract.image_to_string', (), '', False, 'import pytesseract\n'), ((73, 15, 73, 87), 'pytesseract.image_to_string', 'pytesseract.image_to_string', (), '', False, 'import pytesseract\n'), ((18, 48, 18, 59), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n')]