簡體   English   中英

cv2.error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

[英]cv2.error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

參考代碼:

http://blog.tramvm.com/2017/05/recognize-text-from-image-with-python.html

這是從圖像中提取文本的python代碼

當我嘗試運行代碼時,出現上述錯誤

代碼:

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

# Path of working folder on Disk
src_path = "C:\\Users\\preetha\\PycharmProjects\\OCR"

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 + "1.png"))

print ("------ Done -------")

我還嘗試將路徑目錄更改為“/”或“\”仍然沒有得到任何結果......請問有什么解決方案嗎?

請檢查:

  1. 如果圖像存在於提供的路徑中
  2. 如果通過打印正確加載圖像

嘗試打印img_path

我想src_path = "C:\\Users\\preetha\\PycharmProjects\\OCR\\"

最好用

os.path.join(src_path , "1.jpg")

比使用

src_path+"1.jpg"

如果路徑包含非 ASCII 字符,則無法加載圖像(例如,如果路徑中有toña ,則會收到 215 錯誤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM