Created
August 27, 2014 03:06
-
-
Save scturtle/4283da531b3b6915f896 to your computer and use it in GitHub Desktop.
OCR with CV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import cv2.cv as cv | |
import tesseract | |
def show(im): | |
msg = 'press any key to continue' | |
cv2.namedWindow(msg, cv2.WINDOW_NORMAL) | |
cv2.imshow(msg, im) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
def main(): | |
# https://raw.githubusercontent.com/eidge/ruby-captcha-breaker/master/noaa_captchas/noaa_captcha_1.gif | |
im = cv2.imread('noaa_captcha_1.jpg') | |
h, w, _ = im.shape | |
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) | |
im[im > 240] = 0 | |
_, im = cv2.threshold(im, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) | |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) | |
im = cv2.dilate(im, kernel, iterations = 2) | |
im = cv2.erode(im, kernel, iterations = 2) | |
im = cv2.GaussianBlur(im, (3, 3), 0) | |
show(im) | |
api = tesseract.TessBaseAPI() | |
api.Init(".","eng",tesseract.OEM_DEFAULT) | |
api.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
api.SetPageSegMode(tesseract.PSM_SINGLE_BLOCK) | |
h, w = im.shape | |
ipl = cv.CreateImageHeader((w,h), cv.IPL_DEPTH_8U, 1) | |
cv.SetData(ipl, im.tostring(), im.dtype.itemsize * 1 * (w)) | |
tesseract.SetCvImage(ipl, api) | |
text = api.GetUTF8Text().strip() | |
print text | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment