如何使用pytesseract从图像中提取文本?

时间:2019-06-23 22:35:58

标签: python image-recognition text-extraction python-tesseract

我正在使用pytesseract尝试从图像中提取文本编号。

我正在尝试从这张图片中提取三个数字。

使用pytesseract的简单方法是:

const array1 = [
  ['name1', 'name2', 'name3']
]

const array2 = [
  ['xyz', 'xyza', 'xyz2'],
  ['xyzaa', 'xyzas', 'xya']
]


const result = array2.map((item) => {

  /* Reduce items of array1[0] to an object
  that corresponds to current item of array2 */
  return array1[0].reduce((obj, value, index) => {

    return { ...obj,
      [value]: item[index]
    };
  }, {});

});

console.log(JSON.stringify(result, null, ' '));

但这会打印空白。

为什么不能像普通的普通文本一样提取数字?

1 个答案:

答案 0 :(得分:1)

您的图像需要进行一些预处理才能被pytesseract有效处理。

以下显示了使用cv2.adaptiveThreshold()cv2.findContours()cv2.drawContours()操作的此过程,然后将图像转换为黑白并将其反转:

import numpy as np
import cv2
from PIL import Image
import pytesseract

img = cv2.imread('uploaded_image.png', cv2.IMREAD_COLOR)
img = cv2.blur(img, (5, 5))

#HSV (hue, saturation, value)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)

#Applying threshold on pixels' Value (or Brightness)
thresh = cv2.adaptiveThreshold(v, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

#Finding contours
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#Filling contours
contours = cv2.drawContours(img,np.array(contours),-1,(255,255,255),-1)

#To black and white
grayImage = cv2.cvtColor(contours, cv2.COLOR_BGR2GRAY)

#And inverting it
#Setting all `dark` pixels to white
grayImage[grayImage > 200] = 0
#Setting relatively clearer pixels to black
grayImage[grayImage < 100] = 255
#Write the temp file
cv2.imwrite('temp.png',grayImage)

#Read it with tesseract
text = pytesseract.image_to_string(Image.open('temp.png'),config='tessedit_char_whitelist=0123456789 -psm 6 ')

#Output
print("####  Raw text ####")
print(text)
print()
print("#### Extracted digits ####")
print([''.join([y for y in x if y.isdigit()]) for x in text.split('\n')])

输出

####  Raw text ####
93
31
92

#### Extracted digits ####
['93', '31', '92']

处理后的图像

enter image description here

编辑

使用cv2库更新了答案,并从图像中获取了所有数字