2017-12-04 6 views
-1

Tensorflowオブジェクト検出APIを使用していますが、すべて機能していますが、必要なものすべて{オブジェクト名、スコア}などの形式のdictまたは配列を出力したいと思いますオブジェクト名とスコアです。Tensorflowオブジェクト検出APIのクラス名とスコアの印刷

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}) 
     print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5]) 

     threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects 
     print(len(np.where(scores[0] > threshold)[0])/num_detections[0]) 

この部分は、それが印刷い

print ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5]) 

を作動している[{ '名前': 'コンピュータ'、 'ID':1}]は

Iは、次のコードを試みました彼らの任意の方法で私はそのオブジェクトのスコアを辞書に追加できますか?

私は、彼らが使用StackOverflowの上の別の質問を見ました:

threshold = 0.5 # in order to get higher percentages you need to lower this number; usually at 0.01 you get 100% predicted objects 
print(len(np.where(scores[0] > threshold)[0])/num_detections[0]) 

をこれは私にテンソル与えている( "truediv:0" を、DTYPE =のfloat32)が、本家のそれは私ドンので、それは十分ではありません働いていた場合オブジェクトの名前はありません。

ありがとうございました

答えて

3

ここでは私のために働いた解決策があります。 (解決策を探している場合は

# The following code replaces the 'print ([category_index...' statement 
objects = [] 
for index, value in enumerate(classes[0]): 
    object_dict = {} 
    if scores[0, index] > threshold: 
    object_dict[(category_index.get(value)).get('name').encode('utf8')] = \ 
         scores[0, index] 
    objects.append(object_dict) 
print objects 
関連する問題