scale_caltech.py 5.6 KB
Newer Older
M
marina.kolpakova 已提交
1 2
#!/usr/bin/env python

3
import sys, os, os.path, glob, math, cv2, sft
M
marina.kolpakova 已提交
4 5 6
from datetime import datetime
from optparse import OptionParser
import re
7
import numpy as np
M
marina.kolpakova 已提交
8

9
def extractPositive(f, path, opath, octave, min_possible):
10 11 12 13
    newobj = re.compile("^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$")
    pos    = re.compile("^pos\s=(\[[((\d+\.+\d*)|\s+|\;)]*\])$")
    occl   = re.compile("^occl\s*=(\[[0-1|\s]*\])$")

14
    whole_mod_w = int(64  * octave) + 2 * int(20 * octave)
15 16
    whole_mod_h = int(128 * octave) + 2 * int(20 * octave)

17
    goNext = 0
18 19
    start  = 0
    end    = 0
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    person_id = -1;

    boxes = []
    occls = []

    for l in f:
        m = newobj.match(l)
        if m is not None:
            if m.group(1) == "person":
                goNext = 1
                start = int(m.group(2))
                end   = int(m.group(3))
                person_id = person_id + 1
                print m.group(1), person_id, start, end
            else:
                goNext = 0
        else:
            m = pos.match(l)
            if m is not None:
                if not goNext:
                    continue
                strarr = re.sub(r"\s", ", ", re.sub(r"\;\s+(?=\])", "]", re.sub(r"\;\s+(?!\])", "],[", re.sub(r"(\[)(\d)", "\\1[\\2", m.group(1)))))
                boxes = eval(strarr)
            else:
                m = occl.match(l)
                if m is not None:
                    occls = eval(re.sub(r"\s+(?!\])", ",", m.group(1)))

                    if len(boxes) > 0 and len(boxes) == len(occls):
                        for idx, box in enumerate(boxes):
                            if occls[idx] == 1:
                                continue
53

54 55 56 57
                            x = box[0]
                            y = box[1]
                            w = box[2]
                            h = box[3]
58

59 60 61
                            id = int(start) - 1 + idx
                            file = os.path.join(path, "I0%04d.jpg" % id)

62
                            if (start + id) >= end or w < 10 or h < min_possible:
63 64 65 66 67
                                continue

                            mat = cv2.imread(file)
                            mat_h, mat_w, _ = mat.shape

68
                            # let default height of person be 96.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                            scale = h / float(96)
                            rel_scale = scale / octave

                            d_w = whole_mod_w * rel_scale
                            d_h = whole_mod_h * rel_scale

                            tb = (d_h - h) / 2.0
                            lr = (d_w - w) / 2.0

                            x = int(round(x - lr))
                            y = int(round(y - tb))

                            w = int(round(w + lr * 2.0))
                            h = int(round(h + tb * 2.0))

                            inner = [max(5, x), max(5, y), min(mat_w - 5, x + w), min(mat_h - 5, y + h) ]
                            cropped = mat[inner[1]:inner[3], inner[0]:inner[2], :]

                            top     = int(max(0, 0 - y))
                            bottom  = int(max(0, y + h - mat_h))
                            left    = int(max(0, 0 - x))
                            right   = int(max(0, x + w - mat_w))

                            if top < -d_h / 4.0 or bottom > d_h / 4.0 or left < -d_w / 4.0 or right > d_w / 4.0:
                                continue
94

95
                            cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE)
M
marina.kolpakova 已提交
96
                            resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h)
97
                            flipped = cv2.flip(resized, 1)
98

99
                            cv2.imshow("resized", resized)
100

101 102 103
                            c = cv2.waitKey(20)
                            if c == 27:
                                exit(0)
104

105 106 107 108 109 110 111 112 113
                            fname = re.sub(r"^.*\/(set[0-1]\d)\/(V0\d\d)\.(seq)/(I\d+).jpg$", "\\1_\\2_\\4", file)
                            fname = os.path.join(opath, fname + "_%04d." % person_id + "png")
                            fname_fl = os.path.join(opath, fname + "_mirror_%04d." % person_id + "png")
                            try:
                                cv2.imwrite(fname, resized)
                                cv2.imwrite(fname_fl, flipped)
                            except:
                                print "something wrong... go next."
                                pass
114 115 116 117

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-i", "--input", dest="input", metavar="DIRECTORY", type="string",
118 119 120 121 122 123 124
                       help="Path to the Caltech dataset folder.")

    parser.add_option("-d", "--output-dir", dest="output", metavar="DIRECTORY", type="string",
                       help="Path to store data", default=".")

    parser.add_option("-o", "--octave", dest="octave", type="float",
                       help="Octave for a dataset to be scaled", default="0.5")
125

126 127
    parser.add_option("-m", "--min-possible", dest="min_possible", type="int",
                       help="Minimum possible height for positive.", default="64")
128 129 130 131 132 133

    (options, args) = parser.parse_args()

    if not options.input:
        parser.error("Caltech dataset folder is required.")

134
    opath = os.path.join(options.output, datetime.now().strftime("raw_ge64_cr_mirr_ts" + "-%Y-%m-%d-%H-%M-%S"))
135 136
    os.mkdir(opath)

M
minor  
marina.kolpakova 已提交
137
    gl = glob.iglob( os.path.join(options.input, "set[0][0]/V0[0-9][0-9].txt"))
138 139 140 141
    for each in gl:
        path, ext = os.path.splitext(each)
        path = path + ".seq"
        print path
142
        extractPositive(open(each), path, opath, options.octave, options.min_possible)