python运行关于pytesseract的代码出现以下错误
pytesseract.pytesseract.TesseractError: (1, "Tesseract Open Source OCR Engine v3.05.00dev with Leptonica read_params_file: Can't open 11 read_params_file: Can't open txt Warning in pixReadMemPng: work-around: writing to a temp file libpng warning: Application built with libpng-1.4.3 but running with 1.5.14 Error in pixReadStreamPng: png_ptr not made Error in pixReadMemPng: pix not read Error in pixReadMem: png: no pix returned Error during processing.")
以下为原代码
import cv2
import pytesseract
import numpy as np
# 视频捕获对象,0表示默认摄像头
cap = cv2.VideoCapture(1)
while True:
# 读取当前帧
ret, frame = cap.read()
# 翻转帧以适应自然阅读顺序
frame = cv2.flip(frame, 1)
# 将图像转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 对图片进行二值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 找到并筛选出所有轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
# 遍历每一个轮廓
for contour in contours:
# 计算轮廓周长
perimeter = cv2.arcLength(contour, True)
# 近似估计轮廓的形状
approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
# 如果轮廓有四个点,就认为这可能是一个文档边缘
if len(approx) == 4:
# 在图像上绘制文档轮廓
cv2.drawContours(frame, [approx], -1, (0, 255, 0), 3)
# 透视变换矫正文档,得到一个矩形的图像
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
height, width = frame.shape[:2]
pts1 = np.float32(box)
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(frame, M, (width, height))
# 将矩形图像转为灰度图像并进行 OCR 文字识别
gray_rect = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(gray_rect, lang='eng', config='--psm 11')
print(text)
# 显示当前帧
cv2.imshow('frame', frame)
# 如果按下q键,退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源并关闭窗口
cap.release()
cv2.destroyAllWindows()