1

下面是来自 .ipynb 文件的代码片段。

for image_path in TEST_IMAGE_PATHS:
  print(image_path)  
  image = Image.open(image_path)
  print('yooo')  
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  image_np = load_image_into_numpy_array(image)
  print(image_np)
  # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  image_np_expanded = np.expand_dims(image_np, axis=0)
  # Actual detection.
  output_dict = run_inference_for_single_image(image_np, detection_graph)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks'),
      use_normalized_coordinates=True,
      line_thickness=8)
  plt.figure(figsize=IMAGE_SIZE)
  plt.imshow(image_np)

我正在尝试在图像数据集上测试我的模型的准确性。从上面的代码我得到以下错误

ValueError                                Traceback (most recent call last)
<ipython-input-30-ee1cf025b3f1> in <module>
      6   # the array based representation of the image will be used later in order to prepare the
      7   # result image with boxes and labels on it.
----> 8   image_np = load_image_into_numpy_array(image)
      9   print('yooo')
     10   print(image_np)

<ipython-input-15-af094dcdd84a> in load_image_into_numpy_array(image)
      2   (im_width, im_height) = image.size
      3   return np.array(image.getdata()).reshape(
----> 4       (im_height, im_width, 3)).astype(np.uint8)

ValueError: cannot reshape array of size 1048576 into shape (1024,1024,3)

有人可以帮我解决这个错误吗?

4

1 回答 1

0

有两种编码像素的方法:

1-您使用十六进制代码来表示颜色的值

2-您使用 0 到 255 之间的三元组值

在这里,您有 1024*1024 = 1048576 像素,这意味着它已被编码为十六进制值,并且您正试图将其加载到形状矩阵 (1024,1024,3) 中,这意味着它是 RGB 三元组的表示。

如何解决它:重塑为(1024,1024),然后通过将十六进制公式分解为三个值(RGB)扩展为(1024,1024,3)。(这是一种方法, 正如这里if image.format == "PNG":image = image.convert('RGB')提出的工作围绕这个问题

于 2019-03-13T08:57:49.720 回答