简体   繁体   中英

how to fix 'TypeError: 'numpy.ndarray' object is not callable'

I'm trying to resize a 1920X1080 and copy the grayscale onto a white background of size 128X32. But I'm getting this error:

 Traceback (most recent call last):
  File "C:/Users/bnsid/Desktop/SimpleHTR-master - Copy/src/SamplePreprocessor.py", line 39, in <module>
    main()
  File "C:/Users/bnsid/Desktop/SimpleHTR-master - Copy/src/SamplePreprocessor.py", line 32, in main
    cv2.imshow('Greyscale_Stretched', target('float32'))
TypeError: 'numpy.ndarray' object is not callable

My code:

from __future__ import division
from __future__ import print_function

import random
import numpy as np
import cv2

def main():
    "put img into target img of size imgSize, transpose for TF and normalize gray-values"

    img=cv2.imread("C:\\Users\\bnsid\\OneDrive\\Pictures\\Windows Spotlight Images\\fe22f9acd3313c5e21f8a78dc61a7875a42b489d2f3168336d360c050e85dee0.jpg", cv2.IMREAD_GRAYSCALE)
    imgSize=(128,32)
    if img is None:
        img = np.zeros([imgSize[1], imgSize[0]])

    # dataaugmentation
    stretch = (random.random() - 0.5) # -0.5 .. +0.5
    wStretched = max(int(img.shape[1] * (1 + stretch)), 1) # random width, but at least 1
    img = cv2.resize(img, (wStretched, img.shape[0])) # stretch horizontally by factor 0.5 .. 1.5

    # create target image and copy sample image into it
    (wt, ht) = imgSize
    (h, w) = img.shape
    fx = w / wt
    fy = h / ht
    f = max(fx, fy)
    newSize = (max(min(wt, int(w / f)), 1), max(min(ht, int(h / f)), 1)) # scale according to f (result at least 1 and at most wt or ht)
    img = cv2.resize(img, newSize)
    target = np.ones([ht, wt]) * 255
    target[0:newSize[1], 0:newSize[0]] = img

    cv2.imshow('Greyscale_Stretched', target('float32'))
    k= cv2.waitKey(0) & 0xFF
    if k == 27:  # wait for ESC key to exit
        cv2.destroyAllWindows()
    elif k == ord('s'):  # wait for 's' key to save and exit
        cv2.imwrite('grey.png', target('float32'))
        cv2.destroyAllWindows()
main()

I'm expecting a greyscale image on a white background.

Typically, this problem occurs when you're trying to call something from numpy as a function() instead of it's type[] . For reference, see the accepted comment here why numpy.ndarray is object is not callable in my simple for python loop

To solve this issue, try changing some of the parenthesis to brackets on line 32, since parenthesis are for functions, and brackets are for data types. I think one of these combinations will work, but I could still be wrong.

cv2.imshow('Greyscale_Stretched', target['float32'])
cv2.imshow['Greyscale_Stretched', target('float32')]
cv2.imshow['Greyscale_Stretched', target['float32']]

Hope that helps.

the problem is with target('float32') target is the numpy.ndarray, and by having the () after it, you are attempting to call it like a function

With opencv images, float32 is in the range 0.0-1.0 while uint8 is in the range 0-255

I can see you already converted target to the range 0-255 with target = np.ones([ht, wt]) * 255 so you would want to use 'uint8'

Now to address the initial problem. to change the numpy array to a different data type

target = target.astype('uint8') or

target = target.astype('float32') / 255. (divide by 255 to return it to the range 0-1.0)

or you can just use it without storing the new type cv2.imshow('Greyscale_Stretched', target.astype('uint8'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM