caltech ROC test:
authormarina.kolpakova <marina.kolpakova@itseez.com>
Wed, 16 Jan 2013 14:21:47 +0000 (18:21 +0400)
committermarina.kolpakova <marina.kolpakova@itseez.com>
Fri, 1 Feb 2013 10:35:28 +0000 (14:35 +0400)
- parse idl
- replase option parser with argument parser

apps/sft/misk/roc-test.py [deleted file]
apps/sft/misk/roc_test.py [new file with mode: 0755]
apps/sft/misk/sft.py [new file with mode: 0644]

diff --git a/apps/sft/misk/roc-test.py b/apps/sft/misk/roc-test.py
deleted file mode 100755 (executable)
index f4a83cb..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-#!/usr/bin/env python
-
-import sys, os, os.path, glob, math, cv2
-from datetime import datetime
-from optparse import OptionParser
-import numpy
-
-def draw_rects(img, rects, color):
-    if rects is None:
-        return
-    for x1, y1, x2, y2 in rects:
-        cv2.rectangle(img, (x1, y1), (x2 + x1, y2 + y1), color, 2)
-
-if __name__ == "__main__":
-    parser = OptionParser()
-
-    parser.add_option("-i", "--input", dest="input", type="string",
-                       help="Image sequence pattern.")
-
-    parser.add_option("-c", "--cascade", dest="cascade",  metavar="FILE", type="string",
-                       help="Path to the tested detector.")
-
-    parser.add_option("-m", "--min_scale", dest="min_scale", type = "float",
-                       help="Minimum scale to be tested.", default = 0.4)
-
-    parser.add_option("-M", "--max_scale", dest="max_scale", type = "float",
-                       help="Maximum scale to be tested.", default = 5.0)
-
-    parser.add_option("-o", "--output", dest="output", metavar="FILE", type="string",
-                       help="Path to store resultion image.", default="./roc.png")
-
-    parser.add_option("-n", "--nscales", dest="nscales", type="int",
-                       help="Prefered count of scales that should be tested from min to max.", default = 55)
-
-    (options, args) = parser.parse_args()
-
-    if not options.input:
-        parser.error("Test sequence is requared.")
-
-    if not options.cascade:
-        parser.error("Xml cascade file is requared.")
-
-    # where we use nms cv::SCascade::DOLLAR == 2
-    cascade = cv2.SCascade(options.min_scale, options.max_scale, options.nscales, 2)
-    xml = cv2.FileStorage(options.cascade, 0)
-    xml1 = xml.getFirstTopLevelNode()
-
-    cascade.load(xml1)
-
-    camera =  cv2.VideoCapture(options.input);
-    while True:
-        ret, img = camera.read();
-        if not ret:
-            break;
-
-        rects, confs = cascade.detect(img, rois = None)
-
-        # draw results
-        if rects is not None:
-            draw_rects(img, rects[0], (0, 255, 0))
-        cv2.imshow("result",img);
-        if (cv2.waitKey (5) != -1):
-            break;
\ No newline at end of file
diff --git a/apps/sft/misk/roc_test.py b/apps/sft/misk/roc_test.py
new file mode 100755 (executable)
index 0000000..0355ca3
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/env python
+
+import argparse
+import sft
+
+import sys, os, os.path, glob, math, cv2
+from datetime import datetime
+import numpy
+
+def call_parser(f, a):
+    return eval( "sft.parse_" + f + "('" + a + "')")
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser(description = 'Plot ROC curve using Caltech mathod of per image detection performance estimation.')
+
+    # positional
+    parser.add_argument("cascade",     help = "Path to the tested detector.")
+    parser.add_argument("input",       help = "Image sequence pattern.")
+    parser.add_argument("annotations", help = "Path to the annotations.")
+
+    # optional
+    parser.add_argument("-m", "--min_scale", dest = "min_scale", type = float, metavar= "fl",   help = "Minimum scale to be tested.",               default = 0.4)
+    parser.add_argument("-M", "--max_scale", dest = "max_scale", type = float, metavar= "fl",   help = "Maximum scale to be tested.",               default = 5.0)
+    parser.add_argument("-o", "--output",    dest = "output",    type = str,   metavar= "path", help = "Path to store resultiong image.",           default = "./roc.png")
+    parser.add_argument("-n", "--nscales",   dest = "nscales",   type = int,   metavar= "n",    help = "Prefered count of scales from min to max.", default = 55)
+
+    # required
+    parser.add_argument("-f", "--anttn-format", dest = "anttn_format", choices = ['inria', 'caltech', "idl"], help = "Annotation file for test sequence.", required = True)
+
+    args = parser.parse_args()
+
+    samples = call_parser(args.anttn_format, args.annotations)
+
+    # where we use nms cv::SCascade::DOLLAR == 2
+    cascade = cv2.SCascade(args.min_scale, args.max_scale, args.nscales, 2)
+    xml = cv2.FileStorage(args.cascade, 0)
+    dom = xml.getFirstTopLevelNode()
+    assert cascade.load(dom)
+
+    frame = 0
+    pattern = args.input
+    camera =  cv2.VideoCapture(args.input)
+    while True:
+        ret, img = camera.read()
+        if not ret:
+            break;
+
+        name = pattern % (frame,)
+        qq = pattern.format(frame)
+        _, tail = os.path.split(name)
+
+        boxes = samples[tail]
+        if boxes is not None:
+            sft.draw_rects(img, boxes, (255, 0, 0), lambda x, y : y)
+
+        frame = frame + 1
+
+        # sample = samples[]
+
+
+
+        rects, confs = cascade.detect(img, rois = None)
+
+    #     # draw results
+        if rects is not None:
+            sft.draw_rects(img, rects[0], (0, 255, 0))
+
+        cv2.imshow("result", img);
+        if (cv2.waitKey (5) != -1):
+            break;
\ No newline at end of file
diff --git a/apps/sft/misk/sft.py b/apps/sft/misk/sft.py
new file mode 100644 (file)
index 0000000..6a6e2fe
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import cv2, re, glob
+
+def draw_rects(img, rects, color, l = lambda x, y : x + y):
+    if rects is not None:
+        for x1, y1, x2, y2 in rects:
+            cv2.rectangle(img, (x1, y1), (l(x1, x2), l(y1, y2)), color, 2)
+
+class Sample:
+    def __init__(self, bbs, img):
+        self.image = img
+        self.bbs = bb
+
+def parse_inria(ipath, f):
+    bbs = []
+    path = None
+    for l in f:
+        box = None
+        if l.startswith("Bounding box"):
+            b = [x.strip() for x in l.split(":")[1].split("-")]
+            c = [x[1:-1].split(",") for x in b]
+            d = [int(x) for x in sum(c, [])]
+            bbs.append(d)
+
+        if l.startswith("Image filename"):
+            path = l.split('"')[-2]
+
+    return Sample(path, bbs)
+
+def glob_set(pattern):
+    return [__n for __n in glob.iglob(pattern)] #glob.iglob(pattern)
+
+# parse ETH idl file
+def parse_idl(f):
+    map = {}
+    for l in open(f):
+        l = re.sub(r"^\"left\/", "{\"", l)
+        l = re.sub(r"\:", ":[", l)
+        l = re.sub(r"(\;|\.)$", "]}", l)
+        map.update(eval(l))
+    return map
\ No newline at end of file