30361f29701770b2c6a0b7dd8bda72844103cfc5
[platform/upstream/opencv.git] / modules / softcascade / misc / roc_test.py
1 #!/usr/bin/env python
2
3 import argparse
4 import sft
5
6 import sys, os, os.path, glob, math, cv2
7 from datetime import datetime
8 import numpy
9
10 plot_colors = ['b', 'c', 'r', 'g', 'm']
11
12 #       "key"   : (  b,   g,   r)
13 bgr = { "red"   : (  0,   0, 255),
14         "green" : (  0, 255,   0),
15         "blue"  : (255,   0 ,  0)}
16
17 def range(s):
18     try:
19         lb, rb = map(int, s.split(','))
20         return lb, rb
21     except:
22         raise argparse.ArgumentTypeError("Must be lb, rb")
23
24 def call_parser(f, a):
25     return eval( "sft.parse_" + f + "('" + a + "')")
26
27 if __name__ == "__main__":
28     parser = argparse.ArgumentParser(description = 'Plot ROC curve using Caltech method of per image detection performance estimation.')
29
30     # positional
31     parser.add_argument("cascade",     help = "Path to the tested detector.",  nargs='+')
32     parser.add_argument("input",       help = "Image sequence pattern.")
33     parser.add_argument("annotations", help = "Path to the annotations.")
34
35     # optional
36     parser.add_argument("-m", "--min_scale", dest = "min_scale", type = float, metavar= "fl",   help = "Minimum scale to be tested.",               default = 0.4)
37     parser.add_argument("-M", "--max_scale", dest = "max_scale", type = float, metavar= "fl",   help = "Maximum scale to be tested.",               default = 5.0)
38     parser.add_argument("-o", "--output",    dest = "output",    type = str,   metavar= "path", help = "Path to store resulting image.",           default = "./roc.png")
39     parser.add_argument("-n", "--nscales",   dest = "nscales",   type = int,   metavar= "n",    help = "Preferred count of scales from min to max.", default = 55)
40
41     parser.add_argument("-r", "--scale-range",          dest = "scale_range", type = range,  default = (128 * 0.4, 128 * 2.4))
42     parser.add_argument("-e", "--extended-range-ratio", dest = "ext_ratio",   type = float,  default = 1.25)
43     parser.add_argument("-t", "--title",                dest = "title",       type = str,    default = "ROC curve Bahnhof")
44
45     # required
46     parser.add_argument("-f", "--anttn-format", dest = "anttn_format", choices = ['inria', 'caltech', "idl"], help = "Annotation file for test sequence.", required = True)
47     parser.add_argument("-l", "--labels", dest = "labels" ,required=True,     help = "Plot labels for legend.",       nargs='+')
48
49     args = parser.parse_args()
50
51     print args.scale_range
52
53     print args.cascade
54     # parse annotations
55     sft.initPlot(args.title)
56     samples = call_parser(args.anttn_format, args.annotations)
57     for idx, each in enumerate(args.cascade):
58         print each
59         cascade = sft.cascade(args.min_scale, args.max_scale, args.nscales, each)
60         pattern = args.input
61         camera =  cv2.VideoCapture(pattern)
62
63         # for plotting over dataset
64         nannotated  = 0
65         nframes     = 0
66
67         confidenses = []
68         tp          = []
69         ignored     = []
70
71         while True:
72             ret, img = camera.read()
73             if not ret:
74                 break;
75
76             name = pattern % (nframes,)
77             _, tail = os.path.split(name)
78
79             boxes = sft.filter_for_range(samples[tail], args.scale_range, args.ext_ratio)
80
81             nannotated = nannotated + len(boxes)
82             nframes = nframes + 1
83             rects, confs = cascade.detect(img, rois = None)
84
85             if confs is None:
86                 continue
87
88             dts = sft.convert2detections(rects, confs)
89
90             confs = confs.tolist()[0]
91             confs.sort(lambda x, y : -1  if (x - y) > 0 else 1)
92             confidenses = confidenses + confs
93
94             matched, skip_list = sft.match(boxes, dts)
95             tp = tp + matched
96             ignored = ignored + skip_list
97
98             print nframes, nannotated
99
100         fppi, miss_rate = sft.computeROC(confidenses, tp, nannotated, nframes, ignored)
101         sft.plotLogLog(fppi, miss_rate, plot_colors[idx])
102
103     sft.showPlot(args.output, args.labels)