import re
import numpy as np
-def resize(image, d_w, d_h):
- if (d_h < image.shape[0]) or (d_w < image.shape[1]):
- ratio = min(d_h / float(image.shape[0]), d_w / float(image.shape[1]))
-
- 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
-
- return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type)
-
def extractPositive(f, path, opath, octave, min_possible):
newobj = re.compile("^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$")
pos = re.compile("^pos\s=(\[[((\d+\.+\d*)|\s+|\;)]*\])$")
continue
cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE)
- resized = resize(cropped, whole_mod_w, whole_mod_h)
+ resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h)
flipped = cv2.flip(resized, 1)
cv2.imshow("resized", resized)
return [mix, miy, max, may]
-def resize(image, d_w, d_h):
- if (d_h < image.shape[0]) or (d_w < image.shape[1]):
- ratio = min(d_h / float(image.shape[0]), d_w / float(image.shape[1]))
-
- 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
-
- return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type)
-
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-i", "--input", dest="input", metavar="DIRECTORY", type="string",
left = int(max(0, 0 - box[0]))
right = int(max(0, box[2] - mat_w))
cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE)
- resized = resize(cropped, whole_mod_w, whole_mod_h)
+ resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h)
out_name = ".png"
if round(math.log(scale)/math.log(2)) < each:
if (img.shape[0] <= min_shape[0]):
resized_size = (int(min_shape[0] * ratio), int(min_shape[0]))
- img = resize(img, resized_size[0], resized_size[1])
+ img = sft.resize_sample(img, resized_size[0], resized_size[1])
else:
out_name = "negative_sample_%i.png" % idx
assert c.load(dom)
return c
-""" Compute prefix sum for en array"""
+""" Compute prefix sum for en array."""
def cumsum(n):
cum = []
y = 0
cum.append(y)
return cum
-""" Compute x and y arrays for ROC plot"""
+""" Compute x and y arrays for ROC plot."""
def computeROC(confidenses, tp, nannotated, nframes, ignored):
confidenses, tp, ignored = zip(*sorted(zip(confidenses, tp, ignored), reverse = True))
return fppi, miss_rate
-""" Crop rectangle by factor"""
+""" Crop rectangle by factor."""
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
-"""Initialize plot axises"""
+""" Initialize plot axises."""
def initPlot(name = "ROC curve Bahnhof"):
fig, ax = plt.subplots()
plt.xscale('log')
plt.yscale('log')
-"""Show resulted plot"""
+""" Show resulted plot."""
def showPlot(file_name):
plt.savefig(file_name)
plt.axis((pow(10, -3), pow(10, 1), 0.0, 1))
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'] )
plt.show()
+""" Filter true positives and ignored detections for cascade detector output."""
def match(gts, dts):
matches_gt = [0]*len(gts)
matches_dt = [0]*len(dts)
for idx, row in enumerate(overlaps):
imax = row.index(max(row))
- # try to match ground thrush
+ # try to match ground truth
if (matches_gt[imax] == 0 and row[imax] > 0.5):
matches_gt[imax] = 1
matches_dt[idx] = 1
matches_ignore[idx] = 1
return matches_dt, matches_ignore
-
+""" Draw plot."""
def plotLogLog(fppi, miss_rate, c):
print
plt.loglog(fppi, miss_rate, color = c, linewidth = 2)
-
+""" Draw detections or ground truth on image."""
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)
+
def draw_dt(img, dts, color, l = lambda x, y : x + y):
if dts is not None:
for dt in dts:
cv2.rectangle(img, (x1, y1), (l(x1, x2), l(y1, y2)), color, 2)
-class Annotation:
- def __init__(self, bb):
- self.bb = bb
-
class Detection:
def __init__(self, bb, conf):
self.bb = bb
def mark_matched(self):
self.matched = True
-
+"""Parse INPIA annotation format"""
def parse_inria(ipath, f):
bbs = []
path = None
return Sample(path, bbs)
+
def glob_set(pattern):
- return [__n for __n in glob.iglob(pattern)] #glob.iglob(pattern)
+ return [__n for __n in glob.iglob(pattern)]
-# parse ETH idl file
+""" Parse ETH idl file. """
def parse_idl(f):
map = {}
for l in open(f):
map.update(eval(l))
return map
+""" Normalize detection box to unified aspect ration."""
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])
-
+""" Process array of boxes."""
def norm_acpect_ratio(boxes, ratio):
- return [ norm_box(box, ratio) for box in boxes]
\ No newline at end of file
+ return [ norm_box(box, ratio) for box in boxes]
+
+""" Filter detections out of extended range. """
+def filter_for_range(boxes, scale_range, ext_ratio):
+ boxes = sft.norm_acpect_ratio(boxes, 0.5)
+ 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
+
+ return cv2.resize(image_to_resize,(d_w, d_h), None, 0, 0, interpolation_type)
\ No newline at end of file