'numpy.ndarray' 在 jupyter notebook 中的第 2 个 运行 之后无法调用对象

'numpy.ndarray' object is not callable after 2nd run in jupyter notebook

非常简单的代码。一切正常。我把hls_binary和gradx放到了comb_binary.

的方法中
image = mpimg.imread('test_images/test4.jpg')
comb_binary = comb_binary(image)
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(28,16))
ax1.imshow(image2)
ax1.set_title('A', fontsize=20)
ax2.imshow(comb_binary, cmap = 'gray')
ax2.set_title('B', fontsize=20)

但是,如果我在笔记本中重新运行那个单元格,我会运行进入这个错误:

'numpy.ndarray' object is not callable

第一次。有用:

运行 再次显示该单元格:

以下是所有方法的定义以防万一:

def abs_sobel_thresh(img, orient, sobel_kernel, thresh):
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    if orient == 'x':
        sobel = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
    else:
        sobel = cv2.Sobel(gray, cv2.CV_64F, 0, 1)
    abs_sobel = np.absolute(sobel)
    scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
    grad_binary = np.zeros_like(scaled_sobel)
    grad_binary[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
    return grad_binary

def hls_select(img, thresh):
    hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)
    s_channel = hls[:,:,2]
    hls_binary = np.zeros_like(s_channel)
    hls_binary[(s_channel > thresh[0]) & (s_channel <= thresh[1])] = 1
    return hls_binary

def comb_binary(image):
    gradx = abs_sobel_thresh(image, orient='x', sobel_kernel=9, thresh=(20, 100))
    hls_binary = hls_select(image, thresh=(170, 255))
    combined_binary_final = np.zeros_like(gradx)
    combined_binary_final[(hls_binary == 1 ) | (gradx == 1)] = 1
    return combined_binary_final

每次你在 jupyter 中评估一个单元格时,它都会在以前的命令构建的环境中运行这些命令。所以,当你有这样一行时:

comb_binary = comb_binary(image)

第一次一切都好。您只需用它的结果替换 comb_binary (函数)。现在 comb_binary 是一个 numpy 数组……但是,如果您尝试再次执行该单元格,comb_binary 现在是一个 numpy 数组——而不是函数。就像你写的一样:

comb_binary = comb_binary(image)
comb_binary = comb_binary(image)

而且您不会期望 在大多数情况下都能成功 ;-)。