Hey guys,
TL;DR: I’m writing my own __array__
method, but once calling it, I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-23ae7b1bc4a5> in <module>()
1 im = RAWImage.create(fn=items[1])
----> 2 x=array(im)
3 x
TypeError: 'dict' object is not callable
Long story short:
I’m trying to add either an __array__
or __array_interface__
method (property) under my class RAWImage
(which is to replace PILImage
, which is based on Image.Image
).
Why? That’s so I can pass a RAWImage
object in array()
(NumPy method) and get back an array of the image alongside its dtype
. It’s supposed to look like this: (but RAWImage
instead of PILImage
)
Output:
(fastai.vision.core.RAWImage, array([[[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3],
...,
[20, 20, 22],
[19, 21, 20],
[19, 21, 20]],
[[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3],
...,
[20, 20, 22],
[20, 20, 22],
[20, 20, 22]],
[[ 3, 3, 3],
[ 3, 3, 3],
[ 3, 3, 3],
...,
[20, 20, 22],
[21, 21, 23],
[20, 20, 22]],
...,
[[ 3, 3, 3],
[ 3, 3, 3],
[ 2, 2, 2],
...,
[37, 37, 35],
[29, 29, 27],
[28, 28, 26]],
[[ 3, 3, 3],
[ 2, 2, 2],
[ 2, 2, 2],
...,
[33, 33, 31],
[23, 23, 21],
[22, 22, 20]],
[[ 3, 3, 3],
[ 2, 2, 2],
[ 2, 2, 2],
...,
[29, 29, 27],
[21, 21, 19],
[23, 23, 21]]], dtype=np.float32))
So I copied the Image.Image
equivalent __array_interface__
and made some changes to this:
@property
def __array__(self):
# numpy array interface support
new = {}
new["shape"] = self.ndarr.shape #Here is the image array
new["typestr"] = "|f4" #to support float32
new["data"] = id(self) #it's supposed to hold the memory address of the object, but it doesn't work
return new #It's supposed to return a dict type of object
I tried to run this:
im = RAWImage.create(fn=items[1])
x=array(im)
x
But the error is as the following:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-23ae7b1bc4a5> in <module>()
1 im = RAWImage.create(fn=items[1])
----> 2 x=array(im)
3 x
TypeError: 'dict' object is not callable
What did I miss out here? How to fix it?
Thanks for helping with this!