|
| 1 | +import cv2 |
| 2 | +import pytesseract |
| 3 | +import numpy as np |
| 4 | +from PIL import ImageGrab |
| 5 | +import time |
| 6 | + |
| 7 | + |
| 8 | +pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' |
| 9 | +img = cv2.imread('1.png') |
| 10 | +img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 11 | +pytesseract |
| 12 | +############################################## |
| 13 | +##### Image to String ###### |
| 14 | +############################################## |
| 15 | +# print(pytesseract.image_to_string(img)) |
| 16 | + |
| 17 | +############################################# |
| 18 | +#### Detecting Characters ###### |
| 19 | +############################################# |
| 20 | +hImg, wImg,_ = img.shape |
| 21 | +boxes = pytesseract.image_to_boxes(img) |
| 22 | +for b in boxes.splitlines(): |
| 23 | + print(b) |
| 24 | + b = b.split(' ') |
| 25 | + print(b) |
| 26 | + x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4]) |
| 27 | + cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 2) |
| 28 | + cv2.putText(img,b[0],(x,hImg- y+25),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2) |
| 29 | + |
| 30 | + |
| 31 | +############################################## |
| 32 | +##### Detecting Words ###### |
| 33 | +############################################## |
| 34 | +# #[ 0 1 2 3 4 5 6 7 8 9 10 11 ] |
| 35 | +# #['level', 'page_num', 'block_num', 'par_num', 'line_num', 'word_num', 'left', 'top', 'width', 'height', 'conf', 'text'] |
| 36 | +# boxes = pytesseract.image_to_data(img) |
| 37 | +# for a,b in enumerate(boxes.splitlines()): |
| 38 | +# print(b) |
| 39 | +# if a!=0: |
| 40 | +# b = b.split() |
| 41 | +# if len(b)==12: |
| 42 | +# x,y,w,h = int(b[6]),int(b[7]),int(b[8]),int(b[9]) |
| 43 | +# cv2.putText(img,b[11],(x,y-5),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2) |
| 44 | +# cv2.rectangle(img, (x,y), (x+w, y+h), (50, 50, 255), 2) |
| 45 | + |
| 46 | + |
| 47 | +############################################## |
| 48 | +##### Detecting ONLY Digits ###### |
| 49 | +############################################## |
| 50 | +# hImg, wImg,_ = img.shape |
| 51 | +# conf = r'--oem 3 --psm 6 outputbase digits' |
| 52 | +# boxes = pytesseract.image_to_boxes(img,config=conf) |
| 53 | +# for b in boxes.splitlines(): |
| 54 | +# print(b) |
| 55 | +# b = b.split(' ') |
| 56 | +# print(b) |
| 57 | +# x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4]) |
| 58 | +# cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 2) |
| 59 | +# cv2.putText(img,b[0],(x,hImg- y+25),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2) |
| 60 | + |
| 61 | + |
| 62 | +############################################## |
| 63 | +##### Webcam and Screen Capture Example ###### |
| 64 | +############################################## |
| 65 | +# cap = cv2.VideoCapture(0) |
| 66 | +# cap.set(3,640) |
| 67 | +# cap.set(4,480) |
| 68 | +# def captureScreen(bbox=(300,300,1500,1000)): |
| 69 | +# capScr = np.array(ImageGrab.grab(bbox)) |
| 70 | +# capScr = cv2.cvtColor(capScr, cv2.COLOR_RGB2BGR) |
| 71 | +# return capScr |
| 72 | +# while True: |
| 73 | +# timer = cv2.getTickCount() |
| 74 | +# _,img = cap.read() |
| 75 | +# #img = captureScreen() |
| 76 | +# #DETECTING CHARACTERES |
| 77 | +# hImg, wImg,_ = img.shape |
| 78 | +# boxes = pytesseract.image_to_boxes(img) |
| 79 | +# for b in boxes.splitlines(): |
| 80 | +# #print(b) |
| 81 | +# b = b.split(' ') |
| 82 | +# #print(b) |
| 83 | +# x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4]) |
| 84 | +# cv2.rectangle(img, (x,hImg- y), (w,hImg- h), (50, 50, 255), 2) |
| 85 | +# cv2.putText(img,b[0],(x,hImg- y+25),cv2.FONT_HERSHEY_SIMPLEX,1,(50,50,255),2) |
| 86 | +# fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer); |
| 87 | +# #cv2.putText(img, str(int(fps)), (75, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (20,230,20), 2); |
| 88 | +# cv2.imshow("Result",img) |
| 89 | +# cv2.waitKey(1) |
| 90 | +# |
| 91 | +# |
| 92 | + |
| 93 | +cv2.imshow('img', img) |
| 94 | +cv2.waitKey(0) |
0 commit comments