2013-01-16 22:21:47 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import cv2, re, glob
|
2013-01-23 18:55:07 +08:00
|
|
|
import numpy as np
|
2013-01-18 16:22:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2013-01-24 02:21:56 +08:00
|
|
|
from itertools import izip
|
2013-01-18 16:22:03 +08:00
|
|
|
|
2013-01-23 18:55:07 +08:00
|
|
|
""" Convert numPy matrices with rectangles and confidences to sorted list of detections."""
|
2013-01-21 00:20:08 +08:00
|
|
|
def convert2detections(rects, confs, crop_factor = 0.125):
|
|
|
|
if rects is None:
|
|
|
|
return []
|
|
|
|
|
|
|
|
dts = zip(*[rects.tolist(), confs.tolist()])
|
|
|
|
dts = zip(dts[0][0], dts[0][1])
|
|
|
|
dts = [Detection(r,c) for r, c in dts]
|
|
|
|
|
|
|
|
dts.sort(lambda x, y : -1 if (x.conf - y.conf) > 0 else 1)
|
2013-01-23 18:55:07 +08:00
|
|
|
|
2013-01-21 00:20:08 +08:00
|
|
|
for dt in dts:
|
|
|
|
dt.crop(crop_factor)
|
|
|
|
|
|
|
|
return dts
|
|
|
|
|
2013-01-23 18:55:07 +08:00
|
|
|
""" Create new instance of soft cascade."""
|
2013-01-21 06:36:23 +08:00
|
|
|
def cascade(min_scale, max_scale, nscales, f):
|
2013-01-30 14:54:58 +08:00
|
|
|
# where we use nms cv::SoftCascadeDetector::DOLLAR == 2
|
2013-02-01 18:25:10 +08:00
|
|
|
c = cv2.softcascade_Detector(min_scale, max_scale, nscales, 2)
|
2013-01-21 06:36:23 +08:00
|
|
|
xml = cv2.FileStorage(f, 0)
|
|
|
|
dom = xml.getFirstTopLevelNode()
|
|
|
|
assert c.load(dom)
|
|
|
|
return c
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Compute prefix sum for en array."""
|
2013-01-21 06:36:23 +08:00
|
|
|
def cumsum(n):
|
|
|
|
cum = []
|
|
|
|
y = 0
|
|
|
|
for i in n:
|
|
|
|
y += i
|
|
|
|
cum.append(y)
|
|
|
|
return cum
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Compute x and y arrays for ROC plot."""
|
2013-01-24 02:21:56 +08:00
|
|
|
def computeROC(confidenses, tp, nannotated, nframes, ignored):
|
|
|
|
confidenses, tp, ignored = zip(*sorted(zip(confidenses, tp, ignored), reverse = True))
|
2013-01-21 06:36:23 +08:00
|
|
|
|
|
|
|
fp = [(1 - x) for x in tp]
|
2013-01-24 02:21:56 +08:00
|
|
|
fp = [(x - y) for x, y in izip(fp, ignored)]
|
|
|
|
|
2013-01-21 06:36:23 +08:00
|
|
|
fp = cumsum(fp)
|
|
|
|
tp = cumsum(tp)
|
|
|
|
miss_rate = [(1 - x / (nannotated + 0.000001)) for x in tp]
|
|
|
|
fppi = [x / float(nframes) for x in fp]
|
|
|
|
|
|
|
|
return fppi, miss_rate
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Crop rectangle by factor."""
|
2013-01-21 00:20:08 +08:00
|
|
|
def crop_rect(rect, factor):
|
|
|
|
val_x = factor * float(rect[2])
|
|
|
|
val_y = factor * float(rect[3])
|
|
|
|
x = [int(rect[0] + val_x), int(rect[1] + val_y), int(rect[2] - 2.0 * val_x), int(rect[3] - 2.0 * val_y)]
|
|
|
|
return x
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Initialize plot axises."""
|
2013-01-28 19:34:53 +08:00
|
|
|
def initPlot(name):
|
2013-01-18 16:22:03 +08:00
|
|
|
plt.xlabel("fppi")
|
|
|
|
plt.ylabel("miss rate")
|
2013-01-23 18:55:07 +08:00
|
|
|
plt.title(name)
|
2013-01-18 16:22:03 +08:00
|
|
|
plt.grid(True)
|
|
|
|
plt.xscale('log')
|
2013-01-21 06:36:23 +08:00
|
|
|
plt.yscale('log')
|
|
|
|
|
2013-01-28 19:34:53 +08:00
|
|
|
""" Draw plot."""
|
|
|
|
def plotLogLog(fppi, miss_rate, c):
|
|
|
|
plt.loglog(fppi, miss_rate, color = c, linewidth = 2)
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Show resulted plot."""
|
2013-01-28 19:34:53 +08:00
|
|
|
def showPlot(file_name, labels):
|
|
|
|
plt.axis((pow(10, -3), pow(10, 1), .035, 1))
|
2013-01-23 18:55:07 +08:00
|
|
|
plt.yticks( [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.64, 0.8, 1], ['.05', '.10', '.20', '.30', '.40', '.50', '.64', '.80', '1'] )
|
2013-01-28 19:34:53 +08:00
|
|
|
plt.legend(labels, loc = "lower left")
|
|
|
|
plt.savefig(file_name)
|
2013-01-18 16:22:03 +08:00
|
|
|
plt.show()
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Filter true positives and ignored detections for cascade detector output."""
|
2013-01-23 18:55:07 +08:00
|
|
|
def match(gts, dts):
|
2013-01-24 02:21:56 +08:00
|
|
|
matches_gt = [0]*len(gts)
|
|
|
|
matches_dt = [0]*len(dts)
|
|
|
|
matches_ignore = [0]*len(dts)
|
2013-01-23 18:55:07 +08:00
|
|
|
|
2013-01-24 18:45:11 +08:00
|
|
|
if len(gts) == 0:
|
|
|
|
return matches_dt, matches_ignore
|
|
|
|
|
|
|
|
# Cartesian product for each detection BB_dt with each BB_gt
|
|
|
|
overlaps = [[dt.overlap(gt) for gt in gts]for dt in dts]
|
|
|
|
|
2013-01-23 18:55:07 +08:00
|
|
|
for idx, row in enumerate(overlaps):
|
|
|
|
imax = row.index(max(row))
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
# try to match ground truth
|
2013-01-23 18:55:07 +08:00
|
|
|
if (matches_gt[imax] == 0 and row[imax] > 0.5):
|
|
|
|
matches_gt[imax] = 1
|
|
|
|
matches_dt[idx] = 1
|
2013-01-24 02:21:56 +08:00
|
|
|
|
|
|
|
for idx, dt in enumerate(dts):
|
|
|
|
# try to math ignored
|
|
|
|
if matches_dt[idx] == 0:
|
|
|
|
row = gts
|
|
|
|
row = [i for i in row if (i[3] - i[1]) < 53 or (i[3] - i[1]) > 256]
|
|
|
|
for each in row:
|
|
|
|
if dts[idx].overlapIgnored(each) > 0.5:
|
|
|
|
matches_ignore[idx] = 1
|
|
|
|
return matches_dt, matches_ignore
|
2013-01-23 18:55:07 +08:00
|
|
|
|
2013-01-21 19:53:25 +08:00
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Draw detections or ground truth on image."""
|
2013-01-16 22:21:47 +08:00
|
|
|
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)
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
|
2013-01-20 01:25:09 +08:00
|
|
|
def draw_dt(img, dts, color, l = lambda x, y : x + y):
|
|
|
|
if dts is not None:
|
|
|
|
for dt in dts:
|
|
|
|
bb = dt.bb
|
|
|
|
x1, y1, x2, y2 = dt.bb[0], dt.bb[1], dt.bb[2], dt.bb[3]
|
|
|
|
|
|
|
|
cv2.rectangle(img, (x1, y1), (l(x1, x2), l(y1, y2)), color, 2)
|
|
|
|
|
2013-01-18 00:36:39 +08:00
|
|
|
class Detection:
|
|
|
|
def __init__(self, bb, conf):
|
|
|
|
self.bb = bb
|
|
|
|
self.conf = conf
|
2013-01-18 16:22:03 +08:00
|
|
|
self.matched = False
|
|
|
|
|
2013-01-20 01:25:09 +08:00
|
|
|
def crop(self, factor):
|
|
|
|
self.bb = crop_rect(self.bb, factor)
|
2013-01-18 00:36:39 +08:00
|
|
|
|
2013-01-23 18:55:07 +08:00
|
|
|
# we use rect-style for dt and box style for gt. ToDo: fix it
|
2013-01-18 00:36:39 +08:00
|
|
|
def overlap(self, b):
|
2013-01-21 00:20:08 +08:00
|
|
|
|
2013-01-18 00:36:39 +08:00
|
|
|
a = self.bb
|
2013-01-18 16:22:03 +08:00
|
|
|
w = min( a[0] + a[2], b[2]) - max(a[0], b[0]);
|
|
|
|
h = min( a[1] + a[3], b[3]) - max(a[1], b[1]);
|
2013-01-18 00:36:39 +08:00
|
|
|
|
|
|
|
cross_area = 0.0 if (w < 0 or h < 0) else float(w * h)
|
|
|
|
union_area = (a[2] * a[3]) + ((b[2] - b[0]) * (b[3] - b[1])) - cross_area;
|
|
|
|
|
2013-01-18 16:22:03 +08:00
|
|
|
return cross_area / union_area
|
|
|
|
|
2013-01-24 02:21:56 +08:00
|
|
|
# we use rect-style for dt and box style for gt. ToDo: fix it
|
|
|
|
def overlapIgnored(self, b):
|
|
|
|
|
|
|
|
a = self.bb
|
|
|
|
w = min( a[0] + a[2], b[2]) - max(a[0], b[0]);
|
|
|
|
h = min( a[1] + a[3], b[3]) - max(a[1], b[1]);
|
|
|
|
|
|
|
|
cross_area = 0.0 if (w < 0 or h < 0) else float(w * h)
|
|
|
|
self_area = (a[2] * a[3]);
|
|
|
|
|
|
|
|
return cross_area / self_area
|
|
|
|
|
2013-01-18 16:22:03 +08:00
|
|
|
def mark_matched(self):
|
|
|
|
self.matched = True
|
2013-01-18 00:36:39 +08:00
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
"""Parse INPIA annotation format"""
|
2013-01-16 22:21:47 +08:00
|
|
|
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)
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
|
2013-01-16 22:21:47 +08:00
|
|
|
def glob_set(pattern):
|
2013-01-24 20:22:08 +08:00
|
|
|
return [__n for __n in glob.iglob(pattern)]
|
2013-01-16 22:21:47 +08:00
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Parse ETH idl file. """
|
2013-01-16 22:21:47 +08:00
|
|
|
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))
|
2013-01-18 00:36:39 +08:00
|
|
|
return map
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Normalize detection box to unified aspect ration."""
|
2013-01-20 01:25:09 +08:00
|
|
|
def norm_box(box, ratio):
|
|
|
|
middle = float(box[0] + box[2]) / 2.0
|
|
|
|
new_half_width = float(box[3] - box[1]) * ratio / 2.0
|
|
|
|
return (int(round(middle - new_half_width)), box[1], int(round(middle + new_half_width)), box[3])
|
|
|
|
|
2013-01-24 20:22:08 +08:00
|
|
|
""" Process array of boxes."""
|
2013-01-20 01:25:09 +08:00
|
|
|
def norm_acpect_ratio(boxes, ratio):
|
2013-01-24 20:22:08 +08:00
|
|
|
return [ norm_box(box, ratio) for box in boxes]
|
|
|
|
|
|
|
|
""" Filter detections out of extended range. """
|
|
|
|
def filter_for_range(boxes, scale_range, ext_ratio):
|
2013-01-28 19:34:53 +08:00
|
|
|
boxes = norm_acpect_ratio(boxes, 0.5)
|
2013-01-24 20:22:08 +08:00
|
|
|
boxes = [b for b in boxes if (b[3] - b[1]) > scale_range[0] / ext_ratio]
|
|
|
|
boxes = [b for b in boxes if (b[3] - b[1]) < scale_range[1] * ext_ratio]
|
|
|
|
return boxes
|
|
|
|
|
|
|
|
""" Resize sample for training."""
|
|
|
|
def resize_sample(image, d_w, d_h):
|
|
|
|
h, w, _ = image.shape
|
|
|
|
if (d_h < h) or (d_w < w):
|
|
|
|
ratio = min(d_h / float(h), d_w / float(w))
|
|
|
|
|
|
|
|
kernel_size = int( 5 / (2 * ratio))
|
|
|
|
sigma = 0.5 / ratio
|
|
|
|
image_to_resize = cv2.filter2D(image, cv2.CV_8UC3, cv2.getGaussianKernel(kernel_size, sigma))
|
|
|
|
interpolation_type = cv2.INTER_AREA
|
|
|
|
else:
|
|
|
|
image_to_resize = image
|
|
|
|
interpolation_type = cv2.INTER_CUBIC
|
|
|
|
|
2013-01-28 22:31:05 +08:00
|
|
|
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)
|
2013-01-28 22:41:22 +08:00
|
|
|
return eval(occl)
|
|
|
|
|
|
|
|
def parse_caltech(f):
|
|
|
|
(nFrame, nSample) = caltech.parse_header(f)
|
|
|
|
objects = caltech.extract_objects(f)
|
|
|
|
|
|
|
|
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 = caltech.parse_pos(obj[1])
|
|
|
|
posv = caltech.parse_pos(obj[2])
|
|
|
|
occl = caltech.parse_occl(obj[3])
|
|
|
|
|
|
|
|
for idx, (p, pv, oc) in enumerate(zip(*[pos, posv, occl])):
|
|
|
|
annotations[start + idx].append((type, p, oc, pv))
|
|
|
|
|
2013-08-27 18:23:26 +08:00
|
|
|
return annotations
|