//  [python]
def bboxes_matching(bboxes, gxs, gys, gignored, matching_threshold = 0.5, scope=None):
    """Matching a collection of detected boxes with groundtruth values.
    Does not accept batched-inputs.
    The algorithm goes as follows: for every detected box, check
    if one grountruth box is matching. If none, then considered as False Positive.
    If the grountruth box is already matched with another one, it also counts
    as a False Positive. We refer the Pascal VOC documentation for the details.

    Args:
      rbboxes: Nx4 Tensors. Detected objects, sorted by score;
      gbboxes: Groundtruth bounding boxes. May be zero padded, hence
        zero-class objects are ignored.
      matching_threshold: Threshold for a positive match.
    Return: Tuple of:
       n_gbboxes: Scalar Tensor with number of groundtruth boxes (may difer from
         size because of zero padding).
       tp_match: (N,)-shaped boolean Tensor containing with True Positives.
       fp_match: (N,)-shaped boolean Tensor containing with False Positives.
    """
    with tf.name_scope(scope, 'bboxes_matching_single',[bboxes, gxs, gys, gignored]):
        # Number of groundtruth boxes.
        gignored = tf.cast(gignored, dtype = tf.bool)
        n_gbboxes = tf.count_nonzero(tf.logical_not(gignored))
        # Grountruth matching arrays.
        gmatch = tf.zeros(tf.shape(gignored), dtype=tf.bool)
        grange = tf.range(tf.size(gignored), dtype=tf.int32)
        
        # Number of detected boxes        
        n_bboxes = tf.shape(bboxes)[0]
        rshape = (n_bboxes, )
        # True/False positive matching TensorArrays.
        # ta is short for TensorArray
        ta_tp_bool = tf.TensorArray(tf.bool, size=n_bboxes, dynamic_size=False, infer_shape=True)
        ta_fp_bool = tf.TensorArray(tf.bool, size=n_bboxes, dynamic_size=False, infer_shape=True)
        
        n_ignored_det = 0
        # Loop over returned objects.
        def m_condition(i, ta_tp, ta_fp, gmatch, n_ignored_det):
            r = tf.less(i, tf.shape(bboxes)[0])
            return r

        def m_body(i, ta_tp, ta_fp, gmatch, n_ignored_det):
            # Jaccard score with groundtruth bboxes.
            rbbox = bboxes[i, :]
#             rbbox = tf.Print(rbbox, [rbbox])
            jaccard = bboxes_jaccard(rbbox, gxs, gys)

            # Best fit, checking it's above threshold.
            idxmax = tf.cast(tf.argmax(jaccard, axis=0), dtype = tf.int32)
            
            jcdmax = jaccard[idxmax]
            match = jcdmax > matching_threshold
            existing_match = gmatch[idxmax]
            not_ignored = tf.logical_not(gignored[idxmax])

            n_ignored_det = n_ignored_det + tf.cast(gignored[idxmax], tf.int32)
            # TP: match & no previous match and FP: previous match | no match.
            # If ignored: no record, i.e FP=False and TP=False.
            tp = tf.logical_and(not_ignored, tf.logical_and(match, tf.logical_not(existing_match)))
            ta_tp = ta_tp.write(i, tp)
            
            fp = tf.logical_and(not_ignored, tf.logical_or(existing_match, tf.logical_not(match)))
            ta_fp = ta_fp.write(i, fp)
            
            # Update grountruth match.
            mask = tf.logical_and(tf.equal(grange, idxmax), tf.logical_and(not_ignored, match))
            gmatch = tf.logical_or(gmatch, mask)
            return [i+1, ta_tp, ta_fp, gmatch,n_ignored_det]
        # Main loop definition.
        i = 0
        [i, ta_tp_bool, ta_fp_bool, gmatch, n_ignored_det] = \
            tf.while_loop(m_condition, m_body,
                          [i, ta_tp_bool, ta_fp_bool, gmatch, n_ignored_det],
                          parallel_iterations=1,
                          back_prop=False)
        # TensorArrays to Tensors and reshape.
        tp_match = tf.reshape(ta_tp_bool.stack(), rshape)
        fp_match = tf.reshape(ta_fp_bool.stack(), rshape)

        # Some debugging information...
#         tp_match = tf.Print(tp_match,
#                             [n_gbboxes, n_bboxes, 
#                              tf.reduce_sum(tf.cast(tp_match, tf.int64)),
#                              tf.reduce_sum(tf.cast(fp_match, tf.int64)),
#                              n_ignored_det,
#                              tf.reduce_sum(tf.cast(gmatch, tf.int64))],
#                             'Matching (NG, ND, TP, FP, n_ignored_det,GM): ')
        return n_gbboxes, tp_match, fp_match