Colab上的Tensorflow对象检测API

时间:2018-12-20 04:54:09

标签: tensorflow google-colaboratory object-detection-api

我尝试根据here在Colab上运行tensorflow对象检测API。

但是,发生了这样的错误,我该如何解决?

我尝试了步骤1.2.3.4。 他们也许会跑。

  with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess: 
    # Definite input and output Tensors for detection_graph
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Each box represents a part of the image where a particular object was detected.
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')
    for image_path in TEST_IMAGE_PATHS:
        image = Image.open(image_path)
        # 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)
        # 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.
        (boxes, scores, classes, num) = sess.run(
             [detection_boxes, detection_scores, detection_classes, num_detections],
             feed_dict={image_tensor: image_np_expanded})
        # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8)
        plt.figure(figsize=IMAGE_SIZE)
        plt.imshow(image_np)

我收到的错误是:

NameError                                
Traceback (most recent call last)
<ipython-input-24-7493eea60222> in <module>()
10     detection_classes = 
detection_graph.get_tensor_by_name('detection_classes:0')
11     num_detections = 
detection_graph.get_tensor_by_name('num_detections:0')
---> 12     for image_path in TEST_IMAGE_PATHS:
13       image = Image.open(image_path)
14       # the array based representation of the image will be used later in order to prepare the

NameError: name 'TEST_IMAGE_PATHS' is not defined

1 个答案:

答案 0 :(得分:0)

在分析代码时,它表明您没有在Colab中运行它。

发生此错误仅仅是因为您没有初始化测试图像目录的路径。

for循环在路径中搜索图像时会引发错误。 因此,请像提到的代码一样初始化路径。

PATH_TO_TEST_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image-{}.jpg'.format(i)) for i in range(1, 4) ]

尝试一下。