【问题标题】:Google Colab does not show imageGoogle Colab 不显示图像
【发布时间】:2020-03-06 15:35:12
【问题描述】:

我正在使用 YOLO 进行对象检测。当我在 Google Colab 中运行以下代码时会显示图像,但是当我将代码保存在 py 文件中时,它不会显示图像。

import cv2
import numpy as np
import core.utils as utils
import tensorflow as tf
from PIL import Image

return_elements = ["input/input_data:0", "pred_sbbox/concat_2:0", "pred_mbbox/concat_2:0", "pred_lbbox/concat_2:0"]
pb_file         = "./yolov3_coco.pb"
image_path      = "./docs/images/road.jpeg"
num_classes     = 80
input_size      = 416
graph           = tf.Graph()

original_image = cv2.imread(image_path)
original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
original_image_size = original_image.shape[:2]
image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size])
image_data = image_data[np.newaxis, ...]

return_tensors = utils.read_pb_return_tensors(graph, pb_file, return_elements)


with tf.Session(graph=graph) as sess:
    pred_sbbox, pred_mbbox, pred_lbbox = sess.run(
        [return_tensors[1], return_tensors[2], return_tensors[3]],
                feed_dict={ return_tensors[0]: image_data})

pred_bbox = np.concatenate([np.reshape(pred_sbbox, (-1, 5 + num_classes)),
                            np.reshape(pred_mbbox, (-1, 5 + num_classes)),
                            np.reshape(pred_lbbox, (-1, 5 + num_classes))], axis=0)

bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3)
bboxes = utils.nms(bboxes, 0.45, method='nms')
image = utils.draw_bbox(original_image, bboxes)
image = Image.fromarray(image)
image # image works but image.show() does not work.

我也试过用

cv2_imshow(image)

但它不起作用。在这种情况下,它会抛出以下错误:

AttributeError: 'Image' object has no attribute 'clip'

如果使用 image.show() 不会抛出任何错误,但它不会显示图像和边界框!

有什么想法吗?

【问题讨论】:

    标签: python opencv yolo


    【解决方案1】:

    首先,由于您使用Pillow 读取图像,您可能应该将其用于show

    im = Image.open(path)
    im.show() 
    

    我确信它可以在 Jupyter 上运行,因为 PIL.show() 在将图像存储到临时文件后调用外部程序来显示图像。在你的情况下,我建议这样做:

    import matplotlib.pyplot as plt
    %matplotlib inline
    
    plt.imshow(im)
    plt.show()
    

    【讨论】:

    • 这适用于 Jupyter!假设我们将上述代码保存在 py 文件中。如果我们在 Jjupyter 笔记本中调用该文件。这是行不通的!你有什么建议!
    • @Naik, %matplotlib inline 是为 Jupiter 指定的,所以我认为它不会起作用,而且您正在使用 colab,如果您运行脚本并且 colab 无法打开图像查看来自网站的应用程序。您必须将代码复制并粘贴到单元格中。但是,您可能有一些方法可以“破解”木星,但我认为这不值得。
    【解决方案2】:

    将笔记本保存为 .ipynb 格式,无论何时再次加载此文件 - 绘图都将可见。

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2019-08-12
      • 1970-01-01
      • 2019-03-22
      • 2021-06-19
      • 2019-01-20
      • 2020-07-03
      • 2022-06-29
      • 2022-11-03
      相关资源
      最近更新 更多