我正在尝试使用文本检测器的this OpenCV class来确定是否可以将其用于我的项目。我正在寻找这样的图像:
src = cv2.imread(img_path)
然后我试图像这样使用前面提到的功能:
bbox, confidence = cv2.text_TextDetectorCNN.detect(src)
但是此代码存在以下错误:
TypeError: descriptor 'detect' for 'cv2.text_TextDetectorCNN' objects doesn't apply to a 'numpy.ndarray' object
我已经尝试过使用
image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB, cv2.CV_8UC3)
,但是它没有做任何事情-image
仍然是ndarray。UPD:附加整个代码和有问题的图片。
import os
# borrowed from https://github.com/lengstrom/fast-style-transfer/blob/master/src/utils.py
def list_images(in_path, formats=[".jpg", ".jpeg", ".gif", ".png", ".pgm", ".bmp"]):
img_files = []
for (dirpath, _, filenames) in os.walk(in_path):
for file in filenames:
_, ext = os.path.splitext(file)
ext = str.lower(ext)
if ext in formats:
img_files.append(os.path.join(dirpath, file))
return img_files
def test_accuracy(folder_path):
image_list = list_images(folder_path)
for img_path in image_list:
src = cv2.imread(img_path)
bbox, confidence = cv2.text_TextDetectorCNN.detect(src)
print(img_path, bbox, confidence) # To check format of output
测试的图像:
(我已经测试了3个不同的
.jpg
图像,但没有一个起作用)问题:有没有一种方法可以将ndarray转换为上述函数可以接受的某些OpenCV格式?
最佳答案
您的问题是您没有创建文本检测器的实例。看到这个:
import cv2
import numpy as np
# After downloading the caffemodel and
# textbox.prototxt you have to create
# an instance of your model
textSpotter = cv2.text.TextDetectorCNN_create(
"textbox.prototxt", "TextBoxes_icdar13.caffemodel"
)
src = cv2.imread("1.png")
bbox, confidence = textSpotter.detect(src)
print("HELLO", bbox, confidence) # To check format of output
关于python - OpenCV不接受ndarray到TextDecectorCNN.detect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61329523/