1

当我使用Object_detection的API时,我按照说明进行操作,一切都很好。但是,当我开始测试我的图片时,我遇到了一个问题,似乎名为“visualize_boxes_and_labels_on_image_array”的函数(在57行)没有'不工作。这是我的源代码

import cv2
import numpy as np
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util


class TOD(object):
    def __init__(self):
        self.PATH_TO_CKPT = '/home/xiyou/Desktop/ssd_training/result/frozen_inference_graph.pb'
        self.PATH_TO_LABELS = '/home/xiyou/Desktop/ssd_training/detection_for_smoke.pbtxt'
        self.NUM_CLASSES = 1
        self.detection_graph = self._load_model()
        self.category_index = self._load_label_map()

    def _load_model(self):
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(self.PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
        return detection_graph

    def _load_label_map(self):
        label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(label_map,
                                                                    max_num_classes=self.NUM_CLASSES,
                                                                    use_display_name=True)
        category_index = label_map_util.create_category_index(categories)
        return category_index

    def detect(self, image):
        with self.detection_graph.as_default():
            with tf.Session(graph=self.detection_graph) as sess:
                 # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image, axis=0)
                image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')
                boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')
                scores = self.detection_graph.get_tensor_by_name('detection_scores:0')
                classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

                num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')
                # Actual detection.
                (boxes, scores, classes, num_detections) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})


                print(boxes, scores, classes, num_detections)
                #print(np.squeeze(boxes))

                # Visualization of the results of a detection.

                                                             #######Here is the problem  
               #  image1 = vis_util.visualize_boxes_and_labels_on_image_array(     
                    image,                              #######Here is the problem
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    self.category_index,
                    use_normalized_coordinates=True,
                    line_thickness=50,

                 )
                #print(np.squeeze(boxes),np.squeeze(classes))

        cv2.namedWindow("detection")
        cv2.imshow("detection", image1)
        cv2.waitKey(0)

if __name__ == '__main__':
    image = cv2.imread('/home/xiyou/Pictures/timg1.jpg')
    detecotr = TOD()
    detecotr.detect(image)

当我运行此代码时,图像确实显示了,但没有任何改变,图片中没有检测到区域,也没有其他信息。输入图片与输出图片相同。但是当我在 Debug 时,我发现 soucres 、 classes 、 box 等变量确实有值。
有人可以帮助我吗?谢谢!!!

我的 Tensorflow 版本是 1.4.0 , CUDA 8.0 在 Ubuntu 16.04

4

0 回答 0