(rec, prec)
return ap, mrec, mpre
--- Official matlab code VOC2012---
  mrec=[0 ; rec ; 1];
  mpre=[0 ; prec ; 0];
  for i=numel(mpre)-1:-1:1
      mpre(i)=max(mpre(i),mpre(i+1));
  end
  i=find(mrec(2:end)~=mrec(1:end-1))+1;
  ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
--- Official matlab code VOC2012---
  mrec=[0 ; rec ; 1];
  mpre=[0 ; prec ; 0];
  for i=numel(mpre)-1:-1:1
      mpre(i)=max(mpre(i),mpre(i+1));
  end
  i=find(mrec(2:end)~=mrec(1:end-1))+1;
  ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
--- Official matlab code VOC2012 --- mrec = [ 0 ; rec ; 1 ] ; mpre = [ 0 ; prec ; 0 ] ; for i = numel ( mpre ) - 1 : - 1 : 1 mpre ( i ) = max ( mpre ( i ) mpre ( i + 1 )) ; end i = find ( mrec ( 2 : end ) ~ = mrec ( 1 : end - 1 )) + 1 ; ap = sum (( mrec ( i ) - mrec ( i - 1 )) . * mpre ( i )) ;
def voc_ap(rec, prec):
  """
  --- Official matlab code VOC2012---
  mrec=[0 ; rec ; 1];
  mpre=[0 ; prec ; 0];
  for i=numel(mpre)-1:-1:1
      mpre(i)=max(mpre(i),mpre(i+1));
  end
  i=find(mrec(2:end)~=mrec(1:end-1))+1;
  ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
  """
  rec.insert(0, 0.0) # insert 0.0 at begining of list
  rec.append(1.0) # insert 1.0 at end of list
  mrec = rec[:]
  prec.insert(0, 0.0) # insert 0.0 at begining of list
  prec.append(0.0) # insert 0.0 at end of list
  mpre = prec[:]
  """
   This part makes the precision monotonically decreasing
    (goes from the end to the beginning)
    matlab:  for i=numel(mpre)-1:-1:1
                mpre(i)=max(mpre(i),mpre(i+1));
  """
  # matlab indexes start in 1 but python in 0, so I have to do:
  #   range(start=(len(mpre) - 2), end=0, step=-1)
  # also the python function range excludes the end, resulting in:
  #   range(start=(len(mpre) - 2), end=-1, step=-1)
  for i in range(len(mpre)-2, -1, -1):
    mpre[i] = max(mpre[i], mpre[i+1])
  """
   This part creates a list of indexes where the recall changes
    matlab:  i=find(mrec(2:end)~=mrec(1:end-1))+1;
  """
  i_list = []
  for i in range(1, len(mrec)):
    if mrec[i] != mrec[i-1]:
      i_list.append(i) # if it was matlab would be i + 1
  """
   The Average Precision (AP) is the area under the curve
    (numerical integration)
    matlab: ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
  """
  ap = 0.0
  for i in i_list:
    ap += ((mrec[i]-mrec[i-1])*mpre[i])
  return ap, mrec, mpre
def voc_ap ( rec , prec ) : rec . insert ( 0 , 0.0 ) # insert 0.0 at begining of list rec . append ( 1.0 ) # insert 1.0 at end of list mrec = rec [ : ] prec . insert ( 0 , 0.0 ) # insert 0.0 at begining of list prec . append ( 0.0 ) # insert 0.0 at end of list mpre = prec [ : ] """
   This part makes the precision monotonically decreasing
    (goes from the end to the beginning)
    matlab:  for i=numel(mpre)-1:-1:1
                mpre(i)=max(mpre(i),mpre(i+1));
  """ # matlab indexes start in 1 but python in 0, so I have to do: #   range(start=(len(mpre) - 2), end=0, step=-1) # also the python function range excludes the end, resulting in: #   range(start=(len(mpre) - 2), end=-1, step=-1) for i in range ( len ( mpre ) - 2 , - 1 , - 1 ) : mpre [ i ] = max ( mpre [ i ] , mpre [ i + 1 ] ) """
   This part creates a list of indexes where the recall changes
    matlab:  i=find(mrec(2:end)~=mrec(1:end-1))+1;
  """ i_list = [ ] for i in range ( 1 , len ( mrec ) ) : if mrec [ i ] != mrec [ i - 1 ] : i_list . append ( i ) # if it was matlab would be i + 1 """
   The Average Precision (AP) is the area under the curve
    (numerical integration)
    matlab: ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
  """ ap = 0.0 for i in i_list : ap += ( ( mrec [ i ] - mrec [ i - 1 ] ) * mpre [ i ] ) return ap , mrec , mpre