无法将numpy对象数组转换为float数组

时间:2017-09-08 13:21:50

标签: python arrays numpy python-3.6

当我从.npy文件加载保存的数组时,我得到一个数组( data_train ),其中包含两个数组,类似于下面发布的数组。 当我读正确的文档时,它是一个带有两个数组的二维数组,不是吗?

所以我的问题是,我不知道如何从第一个数组( data_train )中“提取”第一个数组( img )。 npy文件。

创建.npy文件:

        pressed_k = get_keys.get_pressed_keys()
        img = screen_cap.grab_frame(size)
        tr_data.append([img, pressed_k])
        np.save('data.npy', tr_data)

pressed_k 是一个数组([0,0,0,0])
img 也是一个数组,其中包含格式为480,270

的图像数据

要“提取”我正在使用的得克_ 数组:

batch_y = np.array(np.hstack([i[1] for i in data_train]), dtype=np.float32)

返回:

Shape: (64953,)
Array: [ 0.  0.  0. ...,  0.  0.  0.]

到目前为止一切都很好。

但是当我为 img 数组尝试相同时:

batch_x = np.array([i[0] for i in data_train], dtype=np.float32)

它返回:

ValueError:使用序列设置数组元素。

当我尝试使用 dtype = object

batch_x = np.array([i[0] for i in data_train], dtype=object)

它可以工作,但是在稍后使用 batch_x 的功能中,它会再次抛出相同的错误。

GitHub页面:https://github.com/MrGrimod/gta_self_driving

data_train

  [[0 0]
  [0 0]
[0 0]
...,
[ array([[[212, 194, 179, 255],
      [212, 194, 179, 255],
      [212, 195, 179, 255],
      ...,
      [ 68,  69,  70, 255],
      [ 61,  65,  61, 255],
      [152, 134,  11, 255]],

     [[211, 194, 180, 255],
      [212, 194, 180, 255],
      [212, 195, 179, 255],
      ...,
      [ 66,  68,  63, 255],
      [ 63,  67,  59, 255],
      [153, 134,   9, 255]],

     [[210, 193, 181, 255],
      [211, 194, 181, 255],
      [212, 195, 180, 255],
      ...,
      [ 69,  71,  72, 255],
      [ 71,  72,  73, 255],
      [153, 134,   9, 255]],

     ...,
     [[ 40,  40,  47, 255],
      [ 45,  46,  51, 255],
      [ 37,  37,  43, 255],
      ...,
      [ 45,  47,  54, 255],
      [ 47,  49,  56, 255],
      [146, 130,   9, 255]],

     [[ 40,  40,  47, 255],
      [ 42,  42,  48, 255],
      [ 32,  32,  38, 255],
      ...,
      [ 40,  41,  48, 255],
      [ 42,  43,  48, 255],
      [147, 131,  10, 255]],

     [[ 39,  40,  46, 255],
      [ 35,  35,  41, 255],
      [ 32,  33,  38, 255],
      ...,
      [ 43,  44,  51, 255],
      [ 41,  42,  49, 255],
      [147, 131,  11, 255]]], dtype=uint8)
 array([0, 0, 0, 0])]
 [ array([[[212, 194, 179, 255],
      [212, 194, 179, 255],
      [212, 195, 179, 255],
      ...,
      [ 70,  71,  73, 255],
      [ 67,  68,  66, 255],
      [152, 134,  11, 255]],

     [[211, 193, 180, 255],
      [212, 194, 180, 255],
      [212, 195, 180, 255],
      ...,
      [ 68,  69,  70, 255],
      [ 68,  70,  69, 255],
      [153, 134,   9, 255]],

     [[209, 193, 182, 255],
      [211, 194, 181, 255],
      [212, 195, 180, 255],
      ...,
      [ 69,  72,  74, 255],
      [ 71,  72,  73, 255],
      [153, 134,   9, 255]],

     ...,
     [[ 41,  41,  49, 255],
      [ 46,  46,  52, 255],
      [ 37,  37,  43, 255],
      ...,
      [ 46,  47,  54, 255],
      [ 47,  49,  57, 255],
      [146, 130,   9, 255]],

     [[ 41,  41,  48, 255],
      [ 42,  42,  48, 255],
      [ 32,  32,  38, 255],
      ...,
      [ 41,  42,  48, 255],
      [ 41,  43,  49, 255],
      [147, 131,  10, 255]],

     [[ 40,  40,  46, 255],
      [ 32,  33,  38, 255],
      [ 32,  33,  38, 255],
      ...,
      [ 43,  44,  51, 255],
      [ 41,  42,  49, 255],
      [147, 131,  11, 255]]], dtype=uint8)
        array([0, 0, 0, 0])]
       [ array([[[211, 195, 180, 255],
      [212, 195, 180, 255],
      [212, 195, 180, 255],
      ...,
      [ 68,  69,  69, 255],
      [ 62,  64,  72, 255],
      [152, 134,  11, 255]],

     [[210, 193, 181, 255],
      [211, 194, 181, 255],
      [212, 195, 180, 255],
      ...,
      [ 63,  65,  57, 255],
      [ 49,  55,  61, 255],
      [153, 134,   9, 255]],

     [[211, 193, 181, 255],
      [212, 195, 181, 255],
      [212, 195, 181, 255],
      ...,
      [ 56,  59,  47, 255],
      [ 45,  50,  58, 255],
      [153, 134,   9, 255]],

     ...,
     [[ 42,  43,  50, 255],
      [ 47,  48,  55, 255],
      [ 37,  38,  44, 255],
      ...,
      [ 42,  44,  49, 255],
      [ 46,  48,  54, 255],
      [146, 130,   9, 255]],

     [[ 42,  44,  50, 255],
      [ 40,  41,  47, 255],
      [ 34,  35,  41, 255],
      ...,
      [ 37,  38,  44, 255],
      [ 37,  38,  43, 255],
      [147, 131,  10, 255]],

     [[ 40,  42,  48, 255],
      [ 34,  35,  41, 255],
      [ 34,  35,  41, 255],
      ...,
      [ 45,  48,  55, 255],
      [ 42,  43,  50, 255],
      [147, 131,  11, 255]]], dtype=uint8)
array([0, 0, 0, 0])]]
shape: (16614, 2)
created by appending two array, saving it and then loading it, by using 
np.load

0 个答案:

没有答案