├── README.md └── pytesser.py /README.md: -------------------------------------------------------------------------------- 1 | Pytesser 2 | ======== 3 | 4 | Python wrapper for the tesseract OCR engine. The module is based on OpenCV. 5 | 6 | 7 | Informations 8 | ------------ 9 | 10 | There is already multiples tesseract python modules, but none of them satisfied me. This one is different on the following point: 11 | 12 | * All the classes are put in the same file and all inessential class are removed 13 | * Use OpenCV instead of PIL (to really an advantage because PIL as far more widespread, but better fit my needs ;)) 14 | * Use subprocess.communicate instead of subprocess.wait to avoid any output in the shell or in the programs that use the module. 15 | * Management of the differents languages via the option '-l' because the original pytesser use the default language which is english. By this way the detection of french for instance is totally inacurrate. 16 | * Management of of the pagesegmode, which allow to modify the behavior of tesseract if we want for instance to detect only one character, a word or a line. 17 | * The code is far more straightforward (my opinion) 18 | 19 | Installation 20 | ------------ 21 | 22 | ```bash 23 | sudo apt-get install tesseract tesseract-ocr-all 24 | sudo pip install opencv-python 25 | ``` 26 | 27 | How to use it ? 28 | --------------- 29 | 30 | There is to ways to use it. Either you give it a filename, either directly an image. For a filename you can do: 31 | 32 | ```python 33 | import pytesser 34 | txt = pytesser.image_file_to_string("myimage.jpg") 35 | #By default language is eng, and page seg mode auto 36 | 37 | #To give specifify parameters: 38 | txt = pytesser.image_to_string("myimage.jpg","fra",pytesser.PSM_SINGLE_WORD) #Analyse image as a single french word 39 | ``` 40 | 41 | Or you can directly give it an OpenCV image like this: 42 | 43 | ```python 44 | image = cv2.imread("myimage.jpg") 45 | txt = pytesser.image_to_string(image) 46 | ``` -------------------------------------------------------------------------------- /pytesser.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from subprocess import Popen, PIPE 3 | import os 4 | import tempfile 5 | import cv2 6 | 7 | PROG_NAME = 'tesseract' 8 | TEMP_IMAGE = tempfile.mktemp()+'.bmp' 9 | TEMP_FILE = tempfile.mktemp() 10 | 11 | #All the PSM arguments as a variable name (avoid having to know them) 12 | PSM_OSD_ONLY = 0 13 | PSM_SEG_AND_OSD = 1 14 | PSM_SEG_ONLY = 2 15 | PSM_AUTO = 3 16 | PSM_SINGLE_COLUMN = 4 17 | PSM_VERTICAL_ALIGN = 5 18 | PSM_UNIFORM_BLOCK = 6 19 | PSM_SINGLE_LINE = 7 20 | PSM_SINGLE_WORD = 8 21 | PSM_SINGLE_WORD_CIRCLE = 9 22 | PSM_SINGLE_CHAR = 10 23 | 24 | class TesseractException(Exception): #Raised when tesseract does not return 0 25 | pass 26 | 27 | class TesseractNotFound(Exception): #When tesseract is not found in the path 28 | pass 29 | 30 | def check_path(): #Check if tesseract is in the path raise TesseractNotFound otherwise 31 | for path in os.environ.get('PATH', '').split(':'): 32 | filepath = os.path.join(path, PROG_NAME) 33 | if os.path.exists(filepath) and not os.path.isdir(filepath): 34 | return True 35 | raise TesseractNotFound() 36 | 37 | def process_request(input_file, output_file, lang=None, psm=None): 38 | args = [PROG_NAME, input_file, output_file] #Create the arguments 39 | if lang is not None: 40 | args.append("-l") 41 | args.append(lang) 42 | if psm is not None: 43 | args.append("-psm") 44 | args.append(str(psm)) 45 | proc = Popen(args, stdout=PIPE, stderr=PIPE) #Open process 46 | ret = proc.communicate() #Launch it 47 | 48 | code = proc.returncode 49 | if code != 0: 50 | if code == 2: 51 | raise TesseractException("File not found") 52 | if code == -11: 53 | raise TesseractException("Language code invalid: "+ret[1]) 54 | else: 55 | raise TesseractException(ret[1]) 56 | 57 | def image_to_string(im, lang=None, psm=None): 58 | grayscale_image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) 59 | cv2.imwrite(TEMP_IMAGE, grayscale_image) 60 | txt = image_file_to_string(TEMP_IMAGE, lang, psm) 61 | os.remove(TEMP_IMAGE) 62 | return txt 63 | 64 | def image_file_to_string(file, lang=None, psm=None): 65 | check_path() #Check if tesseract available in the path 66 | grayscale_image = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2GRAY) 67 | cv2.imwrite(TEMP_IMAGE, grayscale_image) 68 | #process_request(file, TEMP_FILE, lang, psm) 69 | process_request(TEMP_IMAGE, TEMP_FILE, lang, psm) 70 | f = open(TEMP_FILE+".txt", "r") #Open back the file 71 | txt = f.read() 72 | f.close() 73 | os.remove(TEMP_FILE+".txt") 74 | os.remove(TEMP_IMAGE) 75 | return txt 76 | 77 | 78 | if __name__ =='__main__': 79 | print(image_file_to_string(sys.argv[2], sys.argv[1], PSM_AUTO)) 80 | --------------------------------------------------------------------------------