3 import sys, os, os.path, glob, math, cv2, sft
4 from datetime import datetime
5 from optparse import OptionParser
9 def extractPositive(f, path, opath, octave, min_possible):
10 newobj = re.compile("^lbl=\'(\w+)\'\s+str=(\d+)\s+end=(\d+)\s+hide=0$")
11 pos = re.compile("^pos\s=(\[[((\d+\.+\d*)|\s+|\;)]*\])$")
12 occl = re.compile("^occl\s*=(\[[0-1|\s]*\])$")
14 whole_mod_w = int(64 * octave) + 2 * int(20 * octave)
15 whole_mod_h = int(128 * octave) + 2 * int(20 * octave)
29 if m.group(1) == "person":
31 start = int(m.group(2))
33 person_id = person_id + 1
34 print m.group(1), person_id, start, end
42 strarr = re.sub(r"\s", ", ", re.sub(r"\;\s+(?=\])", "]", re.sub(r"\;\s+(?!\])", "],[", re.sub(r"(\[)(\d)", "\\1[\\2", m.group(1)))))
47 occls = eval(re.sub(r"\s+(?!\])", ",", m.group(1)))
49 if len(boxes) > 0 and len(boxes) == len(occls):
50 for idx, box in enumerate(boxes):
59 id = int(start) - 1 + idx
60 file = os.path.join(path, "I0%04d.jpg" % id)
62 if (start + id) >= end or w < 10 or h < min_possible:
65 mat = cv2.imread(file)
66 mat_h, mat_w, _ = mat.shape
68 # let default height of person be 96.
70 rel_scale = scale / octave
72 d_w = whole_mod_w * rel_scale
73 d_h = whole_mod_h * rel_scale
78 x = int(round(x - lr))
79 y = int(round(y - tb))
81 w = int(round(w + lr * 2.0))
82 h = int(round(h + tb * 2.0))
84 inner = [max(5, x), max(5, y), min(mat_w - 5, x + w), min(mat_h - 5, y + h) ]
85 cropped = mat[inner[1]:inner[3], inner[0]:inner[2], :]
87 top = int(max(0, 0 - y))
88 bottom = int(max(0, y + h - mat_h))
89 left = int(max(0, 0 - x))
90 right = int(max(0, x + w - mat_w))
92 if top < -d_h / 4.0 or bottom > d_h / 4.0 or left < -d_w / 4.0 or right > d_w / 4.0:
95 cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right, cv2.BORDER_REPLICATE)
96 resized = sft.resize_sample(cropped, whole_mod_w, whole_mod_h)
97 flipped = cv2.flip(resized, 1)
99 cv2.imshow("resized", resized)
105 fname = re.sub(r"^.*\/(set[0-1]\d)\/(V0\d\d)\.(seq)/(I\d+).jpg$", "\\1_\\2_\\4", file)
106 fname = os.path.join(opath, fname + "_%04d." % person_id + "png")
107 fname_fl = os.path.join(opath, fname + "_mirror_%04d." % person_id + "png")
109 cv2.imwrite(fname, resized)
110 cv2.imwrite(fname_fl, flipped)
112 print "something wrong... go next."
115 if __name__ == "__main__":
116 parser = OptionParser()
117 parser.add_option("-i", "--input", dest="input", metavar="DIRECTORY", type="string",
118 help="Path to the Caltech dataset folder.")
120 parser.add_option("-d", "--output-dir", dest="output", metavar="DIRECTORY", type="string",
121 help="Path to store data", default=".")
123 parser.add_option("-o", "--octave", dest="octave", type="float",
124 help="Octave for a dataset to be scaled", default="0.5")
126 parser.add_option("-m", "--min-possible", dest="min_possible", type="int",
127 help="Minimum possible height for positive.", default="64")
129 (options, args) = parser.parse_args()
131 if not options.input:
132 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"))
137 gl = glob.iglob( os.path.join(options.input, "set[0][0]/V0[0-9][0-9].txt"))
139 path, ext = os.path.splitext(each)
142 extractPositive(open(each), path, opath, options.octave, options.min_possible)