Sapnesh Naik Sapnesh Naik - 1 year ago 69
Python Question

python-2.7 | SimpleCV - TypeError: 'float' object cannot be interpreted as an index

I'm trying to build a Image dewarping tool using simpleCV
and python2.7

The code below worked perfectly fine before(when I was on Ubuntu 16.04.1 LTS)
but I recently updated to the Ubuntu 17.04 and I get this error now.

Here is the function:

def buildMap(Ws, Hs, Wd, Hd, R1, R2, Cx, Cy):
map_x = np.zeros((Hd, Wd),np.float32)
map_y = np.zeros((Hd, Wd),np.float32)
rMap = np.linspace(R1, R1 + (R2 - R1), Hd)
thetaMap = np.linspace(0, 0 + float(Wd) * 2.0 * np.pi, Wd)
sinMap = np.sin(thetaMap)
cosMap = np.cos(thetaMap)

for y in xrange(0, int(Hd-1)):
map_x[y] = Cx + rMap[y] * sinMap
map_y[y] = Cy + rMap[y] * cosMap

return map_x, map_y

and this is the error I get:

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/", line 801, in __bootstrap_inner
File "/usr/lib/python2.7/", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "./", line 189, in new_dewarp
xmap, ymap = buildMap(Ws, Hs, Wd, Hd, R1, R2, Cx, Cy)
File "./", line 122, in buildMap
map_x = np.zeros((Hd, Wd),np.float32)
TypeError: 'float' object cannot be interpreted as an index

what is the problem in my code?

Answer Source

the np.zeros function needs Hd and Wd to be integers, not floats. Previous versions of Python would silently cast to integer, but newer versions give an error instead. Try adding this:

Hd = int(Hd)
Wd = int(Wd)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download