From 8cf30c728e39e3a9923d58145487b798c57533de Mon Sep 17 00:00:00 2001 From: "marina.kolpakova" Date: Mon, 28 Jan 2013 18:31:05 +0400 Subject: [PATCH] parse Caltech annotations --- apps/sft/misc/roc_caltech.py | 33 +++++++++++++++++++++++++++++++++ apps/sft/misc/roc_test.py | 2 +- apps/sft/misc/sft.py | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100755 apps/sft/misc/roc_caltech.py diff --git a/apps/sft/misc/roc_caltech.py b/apps/sft/misc/roc_caltech.py new file mode 100755 index 0000000000..39ee6820c3 --- /dev/null +++ b/apps/sft/misc/roc_caltech.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import argparse +import sft + +import sys, os, os.path, glob, math, cv2, re +from datetime import datetime +import numpy + +if __name__ == "__main__": + path = "/home/kellan/datasets/caltech/set00/V000.txt" + # open annotation file + f = open(path) + (nFrame, nSample) = sft.caltech.parse_header(f) + objects = sft.caltech.extract_objects(f) + + caltechSamples = [] + annotations = [[] for i in range(nFrame)] + + for obj in objects: + (type, start, end) = re.search(r'^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$', obj[0]).groups() + print type, start, end + start = int(start) -1 + end = int(end) + pos = sft.caltech.parse_pos(obj[1]) + posv = sft.caltech.parse_pos(obj[2]) + occl = sft.caltech.parse_occl(obj[3]) + + for idx, (p, pv, oc) in enumerate(zip(*[pos, posv, occl])): + annotations[start + idx].append((type, p, oc, pv)) + + for each in annotations: + print each diff --git a/apps/sft/misc/roc_test.py b/apps/sft/misc/roc_test.py index f0fc3a809a..f91630aab7 100755 --- a/apps/sft/misc/roc_test.py +++ b/apps/sft/misc/roc_test.py @@ -9,7 +9,7 @@ import numpy plot_colors = ['b', 'c', 'r', 'g', 'm'] -# "key" : ( b, g, r) +# "key" : ( b, g, r) bgr = { "red" : ( 0, 0, 255), "green" : ( 0, 255, 0), "blue" : (255, 0 , 0)} diff --git a/apps/sft/misc/sft.py b/apps/sft/misc/sft.py index cac8094050..c4de7a090b 100644 --- a/apps/sft/misc/sft.py +++ b/apps/sft/misc/sft.py @@ -225,4 +225,38 @@ def resize_sample(image, d_w, d_h): image_to_resize = image interpolation_type = cv2.INTER_CUBIC - return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type) \ No newline at end of file + return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type) + +newobj = re.compile("^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$") + +class caltech: + @staticmethod + def extract_objects(f): + objects = [] + tmp = [] + for l in f: + if newobj.match(l) is not None: + objects.append(tmp) + tmp = [] + tmp.append(l) + return objects[1:] + + @staticmethod + def parse_header(f): + _ = f.readline() # skip first line (version string) + head = f.readline() + (nFrame, nSample) = re.search(r'nFrame=(\d+) n=(\d+)', head).groups() + return (int(nFrame), int(nSample)) + + @staticmethod + def parse_pos(l): + pos = re.match(r'^posv?\s*=(\[[\d\s\.\;]+\])$', l).group(1) + pos = re.sub(r"(\[)(\d)", "\\1[\\2", pos) + pos = re.sub(r"\s", ", ", re.sub(r"\;\s+(?=\])", "]", re.sub(r"\;\s+(?!\])", "],[", pos))) + return eval(pos) + + @staticmethod + def parse_occl(l): + occl = re.match(r'^occl\s*=(\[[\d\s\.\;]+\])$', l).group(1) + occl = re.sub(r"\s(?!\])", ",", occl) + return eval(occl) \ No newline at end of file