2017-07-24 161 views
4

我有一个简单的问题,但我无法弄清楚如何去做。我正在使用TF Object检测API来检测图像,它工作正常,并给出了一个图像,它将绘制边界框,并给出它认为检测到的类别的标签和置信度分数。我的问题是,如何将检测到的类(作为字符串)打印到终端,即不仅在图像上,而且还作为输出到终端。Tensorflow对象检测API:将检测到的类打印为输出到终端

下面是负责图像检测

with detection_graph.as_default(): 
with tf.Session(graph=detection_graph) as sess: 
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) 
    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. 
    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. 
    scores = detection_graph.get_tensor_by_name('detection_scores:0') 
    classes = detection_graph.get_tensor_by_name('detection_classes:0') 
    num_detections = 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}) 
    # 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, min_score_thresh=.2) 
    plt.figure(figsize=IMAGE_SIZE) 
    plt.imshow(image_np) 
    plt.show() 

在此先感谢代码,堆栈溢出的第一篇文章,所以请去容易对我

回答

5

嗯,这是很容易的。该classes被加密的category_index这是一个dict,所以你可以做这样的事情:

with detection_graph.as_default(): 
with tf.Session(graph=detection_graph) as sess: 
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) 
    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. 
    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. 
    scores = detection_graph.get_tensor_by_name('detection_scores:0') 
    classes = detection_graph.get_tensor_by_name('detection_classes:0') 
    num_detections = 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}) 

    # Here output the category as string and score to terminal 
    print([category_index.get(i) for i in classes[0]]) 
    print(scores) 
+0

感谢,结果category_index实际上是一个嵌套字典,所以我加了 行' –

+0

@DanTran行['name']键:你知道我们如何改变图像中类标签的位置吗?目前它位于矩形的最左上角并且不可见!我怎样才能让它在图像中显示得更低? – Breeze

+1

@ Coderx7是看看[这里](https://github.com/tensorflow/models/blob/master/research/object_detection/utils/visualization_utils.py)。你需要调整'draw.text(...)'。 –

1

DAT和Omar..I有一个基本的问题。当我们打印的阵列,它包含数组排名前100的分数和分类。其中只有2到3个实际显示在输出图像中(带有有界的框和精度)。我怎样才能只对那些实际显示在输出图像中的值进行分类?是否可能或者我们是否需要设置一个固定的准确度阈值? (并且可能会丢失输出图像中显示的某些对象)。

+0

对于只有那些实际显示的值的子集,你是什么意思?阈值设置为0.5默认值,因此您可以更改阈值以控制通过'vis_util.visualize_boxes_and_labels_on_image_array(min_score_thresh = .5)'打印的内容。另一种按班级分类的方法也是可能的。看看这个[在这里回答](https://medium.com/@xavialex7/thanks-a-lot-for-this-its-been-really-helpful-so-far-3635e70e6703)。 –

+0

非常感谢Dat Tran ..!该链接真的很有帮助。通过坏..我没有注意到vis_util调用0.5阈值。 –

4

只需转到object_detection文件夹中的utils目录并打开脚本visualization_utils.py。你会发现一个函数,即visualize_boxes_and_labels_on_image_array,在函数的结尾添加一个打印命令来打印变量class_nameprint(class_name))。现在运行你的代码并看到魔法。