python - 带有 OpenCV 和 PIL 的 Python Tesseract 未检测到字符
问题描述
我正在尝试对英雄联盟大厅的文字进行图像识别,以便进行数据挖掘。
我猜它没有识别字体,因为程序的输出是:Doel seen aay
源代码:
import numpy as nm
import pytesseract
import cv2
from PIL import ImageGrab, Image
def imToString():
# Path of tesseract executable
pytesseract.pytesseract.tesseract_cmd ='C:\\Program Files\\Tesseract-OCR\\Tesseract.exe'
while(True):
cap = ImageGrab.grab(bbox =(242, 884, 561, 990))
cap.save('test.png')
tesstr = pytesseract.image_to_string(
cv2.cvtColor(nm.array(cap), cv2.COLOR_BGR2GRAY),
lang ='eng',config='--psm 7')
print(tesstr)
imToString()
解决方案
看来您需要对图像进行一些预处理。
试试这个。
import numpy as np
import cv2
img = cv2.imread('wXQMF.png', 0)
print(img.max(), img.min())
ret, thr1 = cv2.threshold(img, 10, 255, cv2.THRESH_BINARY_INV)
kernel_size_row = 3
kernel_size_col = 3
kernel = np.ones((3, 3), np.uint8)
erosion_image = cv2.erode(thr1, kernel, iterations=1) #// make erosion image
cv2.imwrite('a.png',thr1)
推荐阅读
- r - 如何将 sparlyr 连接到 spark 独立集群
- mysql - 公式 `mysql` 未安装
- php - 将嵌套数组中的“点表示法”键扩展到子数组
- mysql - AWS EB 操作错误“丢失与 MySQL 服务器的连接”
- java - java - 如何在java HashMap中根据产品类别获取产品详细信息?
- php - production.ERROR: stream_socket_enable_crypto(): SSL: Success (Laravel 5.5)
- python - C 运行速度比 PyPy 慢
- r - R将数组拆分为数据框
- php - 将表格导出为 ms word PHP & MS SQL
- python-3.x - 将地理区域划分为大小相等的网格并检索索引位置