使用 tesseract + cv2 从图像中提取数字
extracting numbers from image with tesseract + cv2
我试了几天都没有成功,所以我决定寻求帮助 :)
我对 cv2 和 tesseract 很陌生,我正在尝试做一些我认为很容易的事情,但由于某种原因并不像我预期的那么容易。
这个image is a print screen of multiple values that I have to read and convert to text/int and this one是原文。
我可以将每个人从我拥有的多个图像中分离出来,但是当我尝试转换它们时,我做不到。有时,它给了我正确的价值,但 90% 的时间他都错过了。
这是我的工作:
#open the image
image = cv2.imread('image.png')
#resize it to give some help (this way i was able to get some good results)
image2 = cv2.resize(image, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
#getting the parts of the images with the text that i want to convert
name = image2[0:100, 100:500]
stats1 = image2[110:160, 460:580]
stats2 = image2[160:210, 460:580]
stats3 = image2[220:270, 460:580]
#using pytesseract to convert from image to string
name_str = pytesseract.image_to_string(name, lang='eng',config='--psm 6')
stats1_str = pytesseract.image_to_string(ally_stats_grass, lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789.%')
#print the values
print('name', name_str)
print('grass', stats1_str)
同时,我也尝试了不同的阈值方法和反转图像颜色,也尝试了一些膨胀和腐蚀但没有成功
image2 = cv2.threshold(image2, 1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
image2 = 255 - cv2.morphologyEx(image2, cv2.MORPH_CLOSE, kernel, iterations=1)
karnel = np.ones((1, 1), np.uint8)
image2 = cv2.dilate(image2, kernel, iterations=1)
image2 = cv2.erode(image2, kernel, iterations=1)
我只是在祈祷,希望有人能帮助我:)
谢谢你的时间
对我来说,百分比看起来是最难识别的,所以这个答案主要集中在这些方面。对于这张图片,我认为可能需要针对图像的不同区域使用不同的解决方案。
百分比很难,因为它们周围的黑色边框使数字在阈值处理后太粗。我使用 cv2.inRange
只保留整个图像中的绿色区域。如果您有其他具有不同颜色百分比的图像,则此方法将不起作用。值得指出的是,默认情况下,OpenCV 图像是 BGR,因此第二个数字是绿色分量。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = cv2.imread('plagued_void.png')
image = cv2.resize(image, None, fx=5, fy=5, interpolation=cv2.INTER_CUBIC)
mask = cv2.inRange(image, (0, 219, 0), (5, 255, 5))
image = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow('green', image)
cv2.waitKey(0)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.threshold(image, 1, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2.imshow('threshold', image)
cv2.waitKey(0)
config = '--oem 3 --psm 6'
txt = pytesseract.image_to_string(image, config = config, lang='eng')
print(txt)
txt = txt.replace('\u201D', '%').replace('Y', '%')
print(txt)
这是我在第一个打印语句后得到的结果:
+1.20”.
+1.18Y
+1.28Y.
Tesseract 很难处理 % 字符,将它列入白名单似乎也无济于事。所以我直接替换 Y,第二个打印语句变为:
+1.20%.
+1.18%
+1.28%.
末尾还有尾随句号,您必须将其删除。
我试了几天都没有成功,所以我决定寻求帮助 :)
我对 cv2 和 tesseract 很陌生,我正在尝试做一些我认为很容易的事情,但由于某种原因并不像我预期的那么容易。
这个image is a print screen of multiple values that I have to read and convert to text/int and this one是原文。 我可以将每个人从我拥有的多个图像中分离出来,但是当我尝试转换它们时,我做不到。有时,它给了我正确的价值,但 90% 的时间他都错过了。
这是我的工作:
#open the image
image = cv2.imread('image.png')
#resize it to give some help (this way i was able to get some good results)
image2 = cv2.resize(image, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
#getting the parts of the images with the text that i want to convert
name = image2[0:100, 100:500]
stats1 = image2[110:160, 460:580]
stats2 = image2[160:210, 460:580]
stats3 = image2[220:270, 460:580]
#using pytesseract to convert from image to string
name_str = pytesseract.image_to_string(name, lang='eng',config='--psm 6')
stats1_str = pytesseract.image_to_string(ally_stats_grass, lang='eng',config='--psm 13 --oem 3 -c tessedit_char_whitelist=0123456789.%')
#print the values
print('name', name_str)
print('grass', stats1_str)
同时,我也尝试了不同的阈值方法和反转图像颜色,也尝试了一些膨胀和腐蚀但没有成功
image2 = cv2.threshold(image2, 1, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
image2 = 255 - cv2.morphologyEx(image2, cv2.MORPH_CLOSE, kernel, iterations=1)
karnel = np.ones((1, 1), np.uint8)
image2 = cv2.dilate(image2, kernel, iterations=1)
image2 = cv2.erode(image2, kernel, iterations=1)
我只是在祈祷,希望有人能帮助我:) 谢谢你的时间
对我来说,百分比看起来是最难识别的,所以这个答案主要集中在这些方面。对于这张图片,我认为可能需要针对图像的不同区域使用不同的解决方案。
百分比很难,因为它们周围的黑色边框使数字在阈值处理后太粗。我使用 cv2.inRange
只保留整个图像中的绿色区域。如果您有其他具有不同颜色百分比的图像,则此方法将不起作用。值得指出的是,默认情况下,OpenCV 图像是 BGR,因此第二个数字是绿色分量。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
image = cv2.imread('plagued_void.png')
image = cv2.resize(image, None, fx=5, fy=5, interpolation=cv2.INTER_CUBIC)
mask = cv2.inRange(image, (0, 219, 0), (5, 255, 5))
image = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow('green', image)
cv2.waitKey(0)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.threshold(image, 1, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv2.imshow('threshold', image)
cv2.waitKey(0)
config = '--oem 3 --psm 6'
txt = pytesseract.image_to_string(image, config = config, lang='eng')
print(txt)
txt = txt.replace('\u201D', '%').replace('Y', '%')
print(txt)
这是我在第一个打印语句后得到的结果:
+1.20”.
+1.18Y
+1.28Y.
Tesseract 很难处理 % 字符,将它列入白名单似乎也无济于事。所以我直接替换 Y,第二个打印语句变为:
+1.20%.
+1.18%
+1.28%.
末尾还有尾随句号,您必须将其删除。