jmunsch

Python Tesseract OCR skew detection broken

Jul 11th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. import pylab as pl
  2. import numpy as np
  3. from PIL import Image, ImageEnhance, ImageColor
  4. from scipy import ndimage, misc
  5. import os, time, subprocess, sys
  6. import cv2.cv as cv
  7. import cv2
  8. import math
  9.  
  10. pd = 'C:/Users/UserBob/Desktop/RANDOM/delete_this/test_photos'
  11. for image in os.listdir(pd):
  12.     fp = os.path.join(pd,image)
  13. #    print('Opening: '+str(fp))
  14.     try:
  15.         if os.path.isfile(fp):
  16.             o_img = Image.open(fp).convert('L')
  17.             img = Image.new("RGBA",o_img.size)
  18.             img.paste(o_img)
  19.             size = img.size[0]/3,img.size[1]/3
  20.             img.thumbnail(size, Image.ANTIALIAS)
  21.             contrast = ImageEnhance.Contrast(img)
  22.             img = contrast.enhance(4) #20
  23.             brightness = ImageEnhance.Brightness(img)
  24.             img = brightness.enhance(1.5) #2
  25.             sharp = ImageEnhance.Sharpness(img)
  26.             img = sharp.enhance(2)
  27.             tmp = 'tmp.JPEG'
  28.             img.save(tmp)
  29.  
  30.             img = pl.imread(tmp)[:, :, 0].astype(np.uint8)
  31.             #img = ndimage.gaussian_filter(img, 2)
  32.             img2 = ndimage.binary_erosion(img, iterations=30) #40
  33.             img3 = ndimage.binary_dilation(img2, iterations=30) #40
  34.             labels, n = ndimage.label(img3)
  35.             counts = np.bincount(labels.ravel())
  36.             counts[0] = 0
  37.             img4 = labels==np.argmax(counts)
  38.             img5 = ndimage.binary_fill_holes(img4)
  39.             result = ~img & img5
  40.             result = ndimage.binary_erosion(result, iterations=3) #3
  41.             result = ndimage.binary_dilation(result, iterations=3) #3
  42.             result = -result
  43.             misc.imsave(tmp,result)
  44.            
  45.             p = subprocess.Popen('tesseract '+str(tmp)+' out -psm 6 ' , stdout=subprocess.PIPE)
  46.             with open('out.txt','rb') as f:
  47.                 text = f.readlines()
  48.             print(len(text),str(text))
  49.  
  50.     except Exception, e:
  51.         import traceback
  52.         print(traceback.format_exc())
  53.         print(e)
  54.         continue
  55. print('Done')          
  56.            
  57.            
  58.            
  59.            
  60.             #result = cv2.imdecode(np.frombuffer(result, np.uint8), 1)
  61.             #to binary
  62.             #result = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
  63.             #(thresh, result) = cv2.threshold(result, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  64. #            (conts, _) = cv2.findContours(result, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  65.  
  66.             #cv2.imwrite(tmp,conts)
  67.              #cv2.imshow('result',result)
  68.              #blur
  69.             # kernel = np.ones((30,30),np.float32)/180
  70.             # result = cv2.filter2D(result,-1,kernel)      
  71. # skew detection            
  72.             # result = cv2.imread(tmp,0)
  73.  
  74.             # edges = cv2.Canny(result, 50, 150)
  75.             # cv2.imwrite(tmp,result)
  76.             # minLineLength = 30
  77.             # maxLineGap = 1
  78.             # lines = cv2.HoughLinesP(result,1,np.pi/2,2,minLineLength,maxLineGap)
  79.             # angle = 0
  80.             # try:
  81.                 # print(edges)
  82.                 # print(lines)
  83.                 # for x1,y1,x2,y2 in lines[0]:
  84.                     # print(y2,y1,x2,x1,math.atan2(y2 - y1, x2 - x1))
  85.                     # angle += math.atan2(y2 - y1, x2 - x1)
  86.                     # cv2.line(result,(x1,y1),(x2,y2),(0,255,0),2)
  87.                 # cv2.imwrite(tmp,result)
  88.                 # print(angle)
  89.                 # time.sleep(2)
  90.             # except Exception, e:
  91.                 # print(e)
  92.                 # pass
  93.             # buffer=tesseract.pixRead(tmp)
  94.             # #buffer = open(tmp,'rb').read()
  95.             # h,w = result.shape
  96.             # chan = 1
  97.             # checkImg = cv.CreateImageHeader((w,h), cv.IPL_DEPTH_8U, chan)
  98.             # cv.SetData(checkImg, result.tostring(), result.dtype.itemsize * chan * (w))
  99.             # tesseract.SetCvImage(result, api)
  100.             # text = api.GetUTF8Text()
  101.             # p = subprocess.Popen('tesseract '+str(tmp)+' out -psm 6 ' , stdout=subprocess.PIPE)
  102.             # with open('out.txt','rb') as f:
  103.                 # text = f.readlines()
  104.             # print(len(text),str(text))
  105.  
  106.     # except Exception, e:
  107.         # import traceback
  108.         # print(traceback.format_exc())
  109.         # print(e)
  110.         # continue
  111. # print('Done')
Advertisement
Add Comment
Please, Sign In to add comment