簡體   English   中英

Google Colab 不顯示圖像

[英]Google Colab does not show image

我正在使用 YOLO 進行 object 檢測。 當我在 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() 不會拋出任何錯誤,但它不會顯示圖像和邊界框!

任何想法?

首先,由於您使用Pillow來讀取圖像,因此您可能應該將其用於show

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

我確信它可以在 Jupyter 上運行,因為PIL.show()在將圖像存儲在臨時文件中之后調用外部程序來顯示圖像。 在你的情況下,我建議這樣做:

import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(im)
plt.show()

將筆記本保存為 .ipynb 格式,每當您再次加載此文件時 - 繪圖將可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM