2017-07-24 157 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))。現在運行你的代碼並看到魔法。