类型错误:“key"是此函数的无效关键字参数 [英] TypeError: 'key' is an invalid keyword argument for this function
本文介绍了类型错误:“key"是此函数的无效关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 opencv 将图像转换为文本,但代码出现以下错误:
<块引用>contours.sort(key=lambda x: get_contour_precedence(x, img.shape[1]))TypeError: 'key' 是此函数错误的无效关键字参数.
有什么办法可以解决吗?这是代码:
导入 cv2导入 pytesseract将 numpy 导入为 np将 PIL.Image 导入为 Imagepytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'def get_contour_precedence(contour, cols):容差系数 = 20原点 = cv2.boundingRect(contour)return ((origin[0]//tolerance_factor) * tolerance_factor) * cols +起源[1]img = cv2.imread("C:/Users/Akshatha/Desktop/text_detection_from_image/images/news1.jpg")灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY_INV +cv2.THRESH_OTSU)内核 = np.ones((5, 5), np.uint8)侵蚀= cv2.erode(阈值,内核,迭代= 1)dilation = cv2.dilate(thresh, kernel, 迭代=3)(contours, heirarchy,_) = cv2.findContours(dilation, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)层次结构=层次结构[0]打印(开始")打印(np.array(层次结构).形状,np.array(轮廓).形状)打印(停止")轮廓.sort(key=lambda x: get_contour_precedence(x, img.shape[1]))# 打印(轮廓[0])idx = 0文本列表 = []我 = 0rect_list = []对于轮廓中的 c:# 获取边界矩形x, y, w, h = cv2.boundingRect(c)rect_list.append((x, y, x + w, y + h))# 绘制一个绿色矩形来可视化边界矩形cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3)roi = img[y:y + h, x:x + w]text = pytesseract.image_to_string(roi, lang='eng', config='--oem 1 --psm 6 -c preserve_interword_spaces=1 ')打印(文本)cv2.putText(img, "#{}".format(i + 1), (x, y - 15),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)我 += 1cv2.namedWindow('扩张', cv2.WINDOW_NORMAL)cv2.imshow('扩张', img)cv2.waitKey(0)
解决方案
您使用的 sort()
函数不接受 key
参数.如果 contours
是可迭代的,您可以尝试使用 sorted()
代替,如下所示:
sorted(contours, key=lambda x:x)
请注意,这将返回一个列表.
I'm trying to convert an image to text using opencv, but the code gives the following error:
contours.sort(key=lambda x: get_contour_precedence(x, img.shape[1])) TypeError: 'key' is an invalid keyword argument for this function error.
Is there any way to fix it? This is the code:
import cv2
import pytesseract
import numpy as np
import PIL.Image as Image
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-
OCR\\tesseract'
def get_contour_precedence(contour, cols):
tolerance_factor = 20
origin = cv2.boundingRect(contour)
return ((origin[0] // tolerance_factor) * tolerance_factor) * cols +
origin[1]
img = cv2.imread("C:/Users/Akshatha/Desktop/text_detection_from
_image/images/news1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY_INV +
cv2.THRESH_OTSU)
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(thresh, kernel, iterations=1)
dilation = cv2.dilate(thresh, kernel, iterations=3)
(contours, heirarchy,_) = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
heirarchy = heirarchy[0]
print("start")
print(np.array(heirarchy).shape, np.array(contours).shape)
print("stop")
contours.sort(key=lambda x: get_contour_precedence(x, img.shape[1]))
# print(contours[0])
idx = 0
textlist = []
i = 0
rect_list = []
for c in contours:
# get the bounding rect
x, y, w, h = cv2.boundingRect(c)
rect_list.append((x, y, x + w, y + h))
# draw a green rectangle to visualize the bounding rect
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 3)
roi = img[y:y + h, x:x + w]
text = pytesseract.image_to_string(roi, lang='eng', config='--oem 1 --
psm 6 -c preserve_interword_spaces=1 ')
print(text)
cv2.putText(img, "#{}".format(i + 1), (x, y - 15),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 4)
i += 1
cv2.namedWindow('Dilation', cv2.WINDOW_NORMAL)
cv2.imshow('Dilation', img)
cv2.waitKey(0)
解决方案
The sort()
function you're using doesn't take a key
argument. If contours
is an iterable, you can try using sorted()
instead like this:
sorted(contours, key=lambda x:x)
Note that this will return a list.
这篇关于类型错误:“key"是此函数的无效关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文