我第一次尝试使用 YOLO 对象检测,在此过程中,我在向程序证明图像后遇到类型错误。我正在使用 cv2.imread() 函数从 opencv 读取图像,并在进行一些图像处理后将其提供给 yolo,但我收到“类型错误”。
我提供了出现错误的代码图像:
def ocr_function(image):
start = time()
try:
ocr_threshold = 0.4
ocr_weights = b'data/ocr/ocr-net.weights'
ocr_netcfg = b'data/ocr/ocr-net.cfg'
ocr_dataset = b'data/ocr/ocr-net.data'
ocr_net = dn.load_net(ocr_netcfg, ocr_weights, 0)
ocr_meta = dn.load_meta(ocr_dataset)
print("performing OCR at: ", os.getcwd())
print("\tScanning %s" %image)
image = cv2.imread(image_path)
image = preprocess(image)
show(image, "provided image")
R,(width,height) = detect(ocr_net, ocr_meta, image, thresh=ocr_threshold, nms=None)
if len(R):
L = dknet_label_conversion(R,width,height)
L = nms(L,.45)
L.sort(key=lambda x: x.tl()[0])
lp_str = ''.join([chr(l.cl()) for l in L])
print ('\t\tLP: %s' % lp_str)
我收到此错误:
Traceback (most recent call last):
File "<ipython-input-1-c25d751af85c>", line 86, in ocr_function
R,(width,height) = detect(ocr_net, ocr_meta, imageROT, thresh=ocr_threshold, nms=None)
File "/home/infinity/Desktop/NEW ALPR/LPD/darknet/python/darknet.py", line 126, in detect
im = load_image(image, 0, 0)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
最佳答案
看起来load_image
函数需要一个指向数据数组的指针
。
来自darknet
库的Detect
函数的代码:
load_image = lib.load_image_color
load_image.argtypes = [c_char_p, c_int, c_int]
load_image.restype = IMAGE
所以我建议你使用__arrayinterface__
来传递数据数组。
https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.interface.html
image_data = image.__array_interface__['data'][0]
R,(width,height) = detect(ocr_net, ocr_meta, image_data, thresh=ocr_threshold, nms=None)
关于python - 从 OpenCV python 读取图像后提供图像后出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59104298/