SHARE
TWEET

Untitled

a guest Sep 11th, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pytesseract
  2. import cv2
  3. import collections
  4.  
  5. def recognise(img):
  6.     img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  7.     ret, img_thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  8.     _, ctrs, hier = cv2.findContours(img_thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
  9.     ctrs = sorted(ctrs, key=lambda x: (cv2.boundingRect(x)[1], cv2.boundingRect(x)[0]))
  10.     heights = []
  11.     for ctr in ctrs:
  12.         x, y, w, h = cv2.boundingRect(ctr)
  13.         if abs(w-h)<2 and h>10:
  14.             heights.append(h)
  15.     height_common = collections.Counter(heights).most_common(1)
  16.     digits = []
  17.     if height_common[0][1]>=8:
  18.         height = height_common[0][0]
  19.         for ctr in ctrs:
  20.             x, y, w, h = cv2.boundingRect(ctr)
  21.             if abs(w-h)<=2 and h==height:
  22.                 if len(digits)<8:
  23.                     digits.append(img[y:(y+h), x:(x+w)])
  24.     digits_text = ''
  25.     for digit in digits:
  26.         h, w, c = digit.shape
  27.         digit_text = pytesseract.image_to_string(digit)
  28.         digits_text += digit_text
  29.     return digits_text
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top