api.SetVariable in tesserocr does't work at all

Viewed 16

I'm trying to use tesseract in python with tesserocr library. My text has a very strict pattern so I'm trying to set white list characters and patterns for it.

My code:

import cv2
import numpy as np
from tesserocr import PyTessBaseAPI, RIL

inp = cv2.VideoCapture('inp_vid.mp4')
out = cv2.VideoWriter(filename='out.mp4', 
                      fourcc=cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
                      fps=inp.get(cv2.CAP_PROP_FPS),
                      frameSize=(int(inp.get(cv2.CAP_PROP_FRAME_HEIGHT)),
                                 int(inp.get(cv2.CAP_PROP_FRAME_WIDTH))),
                      isColor = True
                      )
api = PyTessBaseAPI(psm=6)
api.SetVariable('tessedit_char_whitelist', '1234567890QWERTYUIOPASDFGHJKLZXCVBNM')
api.SetVariable('user_patterns_file', '/home/user/Files/patterns.txt')
bpp = 1
                       
while(inp.isOpened()):
    ret, frame = inp.read()
    if ret == False:
        break
    
    frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
    img_bytes = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img_bytes = cv2.medianBlur(img_bytes,5)
    img_bytes = img_bytes.tobytes()
    
    h, w, c = frame.shape
    
    bpl = bpp * w
    
    api.SetImageBytes(
               imagedata=img_bytes,
               width=w,
               height=h,
               bytes_per_pixel=bpp,
               bytes_per_line=bpl)
    
    boxes = api.GetComponentImages(RIL.TEXTLINE, True)
    for i, (im, box, _, _) in enumerate(boxes):
        api.SetRectangle(box['x']-20, box['y']-20, box['w']+40, box['h']+40)
        text = api.GetUTF8Text()
        conf = api.MeanTextConf()
        frame = cv2.putText(frame, text, (int(box['x']), int(box['y'])), cv2.FONT_HERSHEY_SIMPLEX,
                            2, (0, 0, 255), 3)
                          
        
    out.write(frame)
    
inp.release()
out.release()

My patterns.txt:

[\d]{6}
[\d]{2}[A-Z][\d]

But sometimes I get strings like "123456 78", "123456 7.", "12R1 12R" etc.
What is wrong? Thanks in advance!

1 Answers

First of all: you did not provide your input image, so it is difficult to reproduce the problem.

Next: it seems you are expecting from user_patterns_file something it never promised + patterns in your file did not correspond to examples in trie.h

Related