'''
import json
import pylab as pl
import random
import numpy as np
import cv2
import anno_func
%matplotlib inline
datadir = "../../data/"
filedir = datadir + "/annotations.json"
ids = open(datadir + "/test/ids.txt").read().splitlines()
annos = json.loads(open(filedir).read())
result_anno_file = "./../results/ours_result_annos.json"
results_annos = json.loads(open(result_anno_file).read())
test_annos = results_annos
minscore=40
'''
'''sm = anno_func.eval_annos(annos, test_annos, iou=0.5, check_type=True, types=anno_func.type45,
minboxsize=0,maxboxsize=32,minscore=minscore)
'''
def eval_annos(annos_gd, annos_rt, iou=0.75, imgids=None, check_type=True, types=None, minscore=40, minboxsize=0, maxboxsize=400, match_same=True):
ac_n, ac_c = 0,0
rc_n, rc_c = 0,0
if imgids==None:
imgids = annos_rt['imgs'].keys()
if types!=None:
types = { t:0 for t in types }
miss = {"imgs":{}}
wrong = {"imgs":{}}
right = {"imgs":{}}
for imgid in imgids:
v = annos_rt['imgs'][imgid]
vg = annos_gd['imgs'][imgid]
convert = lambda objs: [ [ obj['bbox'][key] for key in ['xmin','ymin','xmax','ymax']] for obj in objs]
objs_g = vg["objects"]
objs_r = v["objects"]
bg = convert(objs_g)
br = convert(objs_r)
match_g = [-1]*len(bg)
match_r = [-1]*len(br)
if types!=None:
for i in range(len(match_g)):
if not types.has_key(objs_g[i][ 'category']):
match_g[i] = -2
for i in range(len(match_r)):
if not types.has_key(objs_r[i]['category']):
match_r[i] = -2
for i in range(len(match_r)):
if objs_r[i].has_key('score') and objs_r[i]['score']<minscore:
match_r[i] = -2
matches = []
for i,boxg in enumerate(bg):
for j,boxr in enumerate(br):
if match_g[i] == -2 or match_r[j] == -2:
continue
if match_same and objs_g[i]['category'] != objs_r[j]['category']: continue
tiou = calc_iou(boxg, boxr)
if tiou>iou:
matches.append((tiou, i, j))
matches = sorted(matches, key=lambda x:-x[0])
for tiou, i, j in matches:
if match_g[i] == -1 and match_r[j] == -1:
match_g[i] = j
match_r[j] = i
for i in range(len(match_g)):
boxsize = box_long_size(objs_g[i]['bbox'])
erase = False
if not (boxsize>=minboxsize and boxsize<maxboxsize):
erase = True
if erase:
if match_g[i] >= 0:
match_r[match_g[i]] = -2
match_g[i] = -2
for i in range(len(match_r)):
boxsize = box_long_size(objs_r[i]['bbox'])
if match_r[i] != -1: continue
if not (boxsize>=minboxsize and boxsize<maxboxsize):
match_r[i] = -2
miss["imgs"][imgid] = {"objects":[]}
wrong["imgs"][imgid] = {"objects":[]}
right["imgs"][imgid] = {"objects":[]}
miss_objs = miss["imgs"][imgid]["objects"]
wrong_objs = wrong["imgs"][imgid]["objects"]
right_objs = right["imgs"][imgid]["objects"]
tt = 0
for i in range(len(match_g)):
if match_g[i] == -1:
miss_objs.append(objs_g[i])
for i in range(len(match_r)):
if match_r[i] == -1:
obj = copy.deepcopy(objs_r[i])
obj['correct_catelog'] = 'none'
wrong_objs.append(obj)
elif match_r[i] != -2:
j = match_r[i]
obj = copy.deepcopy(objs_r[i])
if not check_type or objs_g[j]['category'] == objs_r[i]['category']:
right_objs.append(objs_r[i])
tt+=1
else:
obj['correct_catelog'] = objs_g[j]['category']
wrong_objs.append(obj)
rc_n += len(objs_g) - match_g.count(-2)
ac_n += len(objs_r) - match_r.count(-2)
ac_c += tt
rc_c += tt
if types==None:
styps = "all"
elif len(types)==1:
styps = types.keys()[0]
elif not check_type or len(types)==0:
styps = "none"
else:
styps = "[%s, ...total %s...]"%(types.keys()[0], len(types))
report = "iou:%s, size:[%s,%s), types:%s, accuracy:%s, recall:%s"% (
iou, minboxsize, maxboxsize, styps, 1 if ac_n==0 else ac_c*1.0/ac_n, 1 if rc_n==0 else rc_c*1.0/rc_n)
summury = {
"iou":iou,
"accuracy":1 if ac_n==0 else ac_c*1.0/ac_n,
"recall":1 if rc_n==0 else rc_c*1.0/rc_n,
"miss":miss,
"wrong":wrong,
"right":right,
"report":report
}
return summury