import cv2
import pytesseract

#executable file location
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\aasai\\AppData\\Local\\Tesseract-OCR\\tesseract.exe' # Directory of tesseract.exe
#read image
img = cv2.imread("sample_2.png")
#convert our input image to rgb format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#cv2.imshow('Original',img)

#print each character start point, end point, height, width
#print(pytesseract.image_to_boxes(img))

#detect characters
hImg, wImg,_ = img.shape
boxes = pytesseract.image_to_boxes(img)

for b in boxes.splitlines():
    print(b)
    b = b.split(' ')
    print(b)
    x,y,w,h = int(b[1]),int(b[2]),int(b[3]),int(b[4])
    cv2.rectangle(img,(x,hImg-y),(w,hImg-h),(255,0,0),2)
    cv2.putText(img, b[0], (x, hImg - y + 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (50, 50, 255), 2)



cv2.imshow('result', img)
cv2.waitKey(0)