0

I have this image input image on which I am attempting to apply text detection and ocr, however even after preprocessing (binary thresholding etc) pytesseract doesn't return any output. The purpose of text detection is to improve the ocr output, I'm not too concerned with obtaining bounding boxes.

Here is my code below:

image = cv2.imread('image.jpg')

grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

ret,thresh1 = cv2.threshold(grey,127,255,cv2.THRESH_BINARY)

image = pytesseract.image_to_data(thresh1, output_type=Output.DICT)
image = cv2.bitwise_not(image)

Inspecting the results there is none to nonsensical output, is there anyway to improve this?

Nathan
  • 19
  • 4
  • take a look at: [Improving the quality of the output](https://tesseract-ocr.github.io/tessdoc/ImproveQuality.html). ... The answer is: with tesseract you need to preprocess your image. at least it need to be in black and white. – Alfredo Maussa Jan 08 '21 at 14:54
  • Thanks for the comment, although my image has been preprocessed and is in black and white, which was achived by the code posted – Nathan Jan 08 '21 at 16:11
  • What is output_type=Output.DICT? – Alfredo Maussa Jan 08 '21 at 18:27
  • its stores the output of image_to_data in a dictionary – Nathan Jan 09 '21 at 00:43
  • Then, why are you trying to apply an openCV function (matrix array) to a dictionary? What is exactly what do you want? it is to draw the rectangles (in the image) for each word/number? – Alfredo Maussa Jan 09 '21 at 00:54
  • `pytesseract.image_to_data()` returns these keys: `['level', 'page_num', 'block_num', 'par_num', 'line_num', 'word_num', 'left', 'top', 'width', 'height', 'conf', 'text']` you have to use the values in `'left', 'top', 'width', 'height'` to draw each rentangle. – Alfredo Maussa Jan 09 '21 at 00:59

1 Answers1

0

Try this code:

import pytesseract
import cv2
image = cv2.imread('ccl6t.png')
pytesseract.pytesseract.tesseract_cmd = r'k:\Tesseract\tesseract.exe' #need change!
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,thresh1 = cv2.threshold(grey,127,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('tresh.png', thresh1)
words = pytesseract.image_to_data(thresh1, lang='eng',config='--psm 3 --oem 1 ')
print(str(words))
Alex Alex
  • 1,893
  • 1
  • 6
  • 12
  • I tried your code, but still don't get the expected output. I'd like to locate the text in the image and extract/identify the names – Nathan Jan 07 '21 at 17:10