Ben*_*nji 13 python ocr tesseract python-tesseract cv2
我正在尝试从图像中识别文本,然后输出文本;然而,这个错误吐出来了:
回溯(最近一次通话):文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 41 行,打印中(get_string(src_path + "cont.jpg") ) ) 文件“C:/Users/Benji's Beast/AppData/Local/Programs/Python/Python37-32/imageDet.py”,第 15 行,在 get_string img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv:: cvt颜色'
图像分辨率为 1371x51。我曾尝试将 src_path 上的“/”更改为“\”,但这没有用。有任何想法吗?
这是我的代码:
import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string
# Path of working folder on Disk
src_path = "C:/Users/Benji's Beast/Desktop/image.PNG"
def get_string(img_path):
# Read image with opencv
img = cv2.imread(img_path)
# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion to remove some noise
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
# Write image after removed noise
cv2.imwrite(src_path + "removed_noise.png", img)
# Apply threshold to get image with only black and white
#img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres.png", img)
# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
# Remove template file
#os.remove(temp)
return result
print('--- Start recognize text from image ---')
print(get_string(src_path + "cont.jpg") )
print("------ Done -------")
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题,谢谢。
小智 13
这意味着您将一个未初始化的变量传递给
> cv2.cvtColor()
Run Code Online (Sandbox Code Playgroud)
在此声明之后:
# Read image with opencv
img = cv2.imread(img_path)
Run Code Online (Sandbox Code Playgroud)
您可以尝试在传递给 cv2.cvtColor() 函数之前打印 img 变量吗
> print(img) or print(img.shape)
Run Code Online (Sandbox Code Playgroud)
确保读取图像的函数调用成功
小智 8
错误:函数 'cv::cvtColor 中的 !_src.empty() 表示传递给函数 cvtColor 的对象为空或无。这里,在下面的行中,img 是 none
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Run Code Online (Sandbox Code Playgroud)
img 是以下函数的结果 -
img = cv2.imread(img_path)
Run Code Online (Sandbox Code Playgroud)
img 可以为空对象的可能原因是 -
为了避免此错误,请检查 img 对象是否为 None,如果不是 None,则仅将其传递给 cvtColor 函数。
img = cv2.imread(img_path)
if(img is not None):
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Run Code Online (Sandbox Code Playgroud)
如果有人来到这里在 Google 上搜索此错误消息,但此错误针对的是视频而不是图像,一个可能的原因可能是您刚刚用完了从视频中读取的帧数。参考
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
Run Code Online (Sandbox Code Playgroud)
使用 ret 检查视频的结尾是常见的做法,但您可能像我一样忘记提及它或在检查帧是否可用(ret == True)之前对帧进行操作。干杯。
我认为你的源路径应该是:
src_path = "C:/Users/Benji's Beast/Desktop/"
Run Code Online (Sandbox Code Playgroud)
因为在这里get_string(src_path + "cont.jpg")你已经连接了图像名称。
| 归档时间: |
|
| 查看次数: |
71074 次 |
| 最近记录: |