2017-02-27 26 views
1

所以我在.flo閱讀,因爲我做了一些翹曲。似乎我沒有Python2.7和numpy版本1.11.2的問題,但是當我升級到Python3.6和numpy版本1.12.0時。Numpy只有整數標量數組可以轉換爲標量索引 - 升級到3.6

但在轉換過程中,我知道得到一個錯誤only integer scalar arrays can be converted to a scalar index爲線data2d = np.fromfile(f, np.float32, count=2 * w * h)

import numpy as np 


def read_flow(filename): 
     f = open(filename, 'rb') 
     magic = np.fromfile(f, np.float32, count=1) 
     data2d = None 

     if 202021.25 != magic: 
      print('Magic number incorrect. Invalid .flo file') 
     else: 
      w = np.fromfile(f, np.int32, count=1) 
      h = np.fromfile(f, np.int32, count=1) 
      print("Reading %d x %d flo file" % (h, w)) 
      data2d = np.fromfile(f, np.float32, count=2 * w * h) 
      # reshape data into 3D array (columns, rows, channels) 
      data2d = np.resize(data2d, (h, w, 2)) 
     f.close() 
     return data2d 

.flo文件可以得到here

+0

工作嘗試'count = int(2 * w * h)' –

+0

@ juanpa.arrivillaga停止抱怨,但現在給出一個空數組,因此不能按預期工作 – redrubia

+0

是的,好吧,如果不知道值'w','h'和你期望的'data2d'爲 –

回答

1

如果我運行使用Python 2.7,我得到以下警告代碼:

VisibleDeprecationWarning:將ndim> 0的數組轉換爲索引會導致錯誤在未來返回重塑(newshape,order = order)

原因是np.fromfile()返回一個包含數據而不是數據的numpy數組 - 即使對於單個元素。這意味着W = np.fromfile(F,np.int32,計數= 1)是,例如[512]的東西,而不是512。

以下版本應爲2.7蟒和3.x

import numpy as np 
def read_flow(filename): 
     f = open(filename, 'rb') 
     magic = np.fromfile(f, np.float32, count=1) 
     data2d = None 

     if 202021.25 != magic: 
      print('Magic number incorrect. Invalid .flo file') 
     else: 
      w = np.fromfile(f, np.int32, count=1)[0] 
      h = np.fromfile(f, np.int32, count=1)[0] 
      print("Reading %d x %d flo file" % (h, w)) 
      data2d = np.fromfile(f, np.float32, count=2 * w * h) 
      # reshape data into 3D array (columns, rows, channels) 
      data2d = np.resize(data2d, (h, w, 2)) 
     f.close() 
     return data2d 
相關問題