Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pytesseract
- import cv2
- import collections
- def recognise(img):
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- ret, img_thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
- _, ctrs, hier = cv2.findContours(img_thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
- ctrs = sorted(ctrs, key=lambda x: (cv2.boundingRect(x)[1], cv2.boundingRect(x)[0]))
- heights = []
- for ctr in ctrs:
- x, y, w, h = cv2.boundingRect(ctr)
- if abs(w-h)<2 and h>10:
- heights.append(h)
- height_common = collections.Counter(heights).most_common(1)
- digits = []
- if height_common[0][1]>=8:
- height = height_common[0][0]
- for ctr in ctrs:
- x, y, w, h = cv2.boundingRect(ctr)
- if abs(w-h)<=2 and h==height:
- if len(digits)<8:
- digits.append(img[y:(y+h), x:(x+w)])
- digits_text = ''
- for digit in digits:
- h, w, c = digit.shape
- digit_text = pytesseract.image_to_string(digit)
- digits_text += digit_text
- return digits_text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement