def absolute_path_to_relative_path(files): for f in files: annotations = al.parse(f) for img in annotations: if img.imageName[0] == '/': img.imageName = '/'.join(img.imageName.split('/')[-2:]) al.save(f, annotations)
def create_tracking_files(): # files = glob.glob('/scail/group/deeplearning/driving_data/andriluka/IMAGES/driving_data_q50_data/all_extracted/*.al') # files = [f for f in files if f.find('edit') == -1 and f.find('track') == -1] files = glob.glob('/scail/group/deeplearning/driving_data/andriluka/IMAGES/driving_data_q50_data/all_extracted/benchmark_seq*_cleanup_trackid_every20.pal') absolute_path_to_relative_path(files) all_args = [] for f in files: all_args.append(('-a %s -o ./ ' % f).split(' ')) pool = multiprocessing.Pool(processes=9) for i, result in enumerate( pool.imap_unordered(m.main, all_args) ): print "done %0.1f%%" % (float(i+1) * 100 / len(all_args)) pool.close() pool.join() # remove partials partials = glob.glob('*partial.pal') for p in partials: os.remove(p) # change paths for f in files: tracked_file = os.path.basename(f).replace('.pal','-track.pal') imgs = al.parse(tracked_file) for img in imgs: if img.imageName[0] == '/': img.imageName = '/'.join(img.imageName.split('/')[-2:]) al.save(tracked_file, imgs)
def annotation_labelbox_from_result(result, columns, url_prefix): annotation = AnnotationLib.Annotation() image_url = get_path_from_s3(result[columns['annotation']], url_prefix) boxes = parse_boxes(result[columns['Answer.results']]) for box in boxes: annotation.rects.append(AnnotationLib.AnnoRect(*box)) # remove parameters from filename param_idx = image_url.index('&'); annotation.imageName = image_url[:param_idx]; return annotation
def postprocess_regular(image_info, np_pred_boxes, np_pred_confidences, H, options): pred_anno = al.Annotation() pred_anno.imageName = image_info['path'] pred_anno.imagePath = os.path.abspath(image_info['path']) _, rects = add_rectangles(H, [image_info['transformed']], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=options['min_conf'], tau=options['tau'], show_suppressed=False) h, w = image_info['original_shape'] if 'rotate90' in H['data'] and H['data']['rotate90']: # original image height is a width for rotated one rects = Rotate90.invert(h, rects) rects = [ r for r in rects if r.x1 < r.x2 and r.y1 < r.y2 and r.score > options['min_conf'] ] pred_anno.rects = rects pred_anno = rescale_boxes((H['image_height'], H['image_width']), pred_anno, h, w) return pred_anno
def annotation_labelact_from_result(result, columns, url_prefix): annotation = AnnotationLib.Annotation() image_url = get_path_from_s3(result[columns['annotation']], url_prefix) # remove parameters from filename param_idx = image_url.index('&'); image_url_split = image_url[:param_idx].split(','); assert(len(image_url_split) == 5); annotation.imageName = image_url_split[0]; rect = AnnotationLib.AnnoRect(); rect.x1 = int(image_url_split[1]) rect.y1 = int(image_url_split[2]) rect.x2 = int(image_url_split[3]) rect.y2 = int(image_url_split[4]) result_split = result[columns['Answer.results']].split(','); assert(len(result_split) == 1 + 1 + 4 + 4); # male/female gender_idx_to_val = {0: ATTR_VAL_GENDER_MALE, 1: ATTR_VAL_GENDER_FEMALE}; ptype_idx_to_val = {0: ATTR_VAL_PTYPE_SALES, 1: ATTR_VAL_PTYPE_CUST}; sales_act_idx_to_val = {0: ATTR_VAL_ACT_SALES_INT, 1: ATTR_VAL_ACT_SALES_CLEAN, 2: ATTR_VAL_ACT_SALES_OTHER}; cust_act_idx_to_val = {0: ATTR_VAL_ACT_CUST_QUEUE, 1: ATTR_VAL_ACT_CUST_INT, 2: ATTR_VAL_ACT_CUST_BROWSE, 3: ATTR_VAL_ACT_CUST_OTHER}; gender_idx = int(result_split[-4]); rect.at["gender"] = gender_idx_to_val[gender_idx]; # sales/cust ptype_idx = int(result_split[-3]); rect.at["ptype"] = ptype_idx_to_val[ptype_idx]; if ptype_idx == 0: # interact/clean/other act_idx = int(result_split[-2]); rect.at["act"] = sales_act_idx_to_val[act_idx]; else: # queue/interact/browse/other act_idx = int(result_split[-1]); rect.at["act"] = cust_act_idx_to_val[act_idx]; annotation.rects.append(rect); return annotation
def eval(self, weights, test_boxes, min_conf, tau, show_suppressed, expname): self.H["grid_width"] = self.H["image_width"] / self.H["region_size"] self.H["grid_height"] = self.H["image_height"] / self.H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) if self.H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = self.build_forward(tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = self.build_forward(tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, weights) pred_annolist = al.AnnoList() true_annolist = al.parse(test_boxes) data_dir = os.path.dirname(test_boxes) image_dir = self.get_image_dir(weights, expname, test_boxes) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] img = imresize(orig_img, (self.H["image_height"], self.H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(self.H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=self.H['rnn_len'], min_conf=min_conf, tau=tau, show_suppressed=show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((self.H["image_height"], self.H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def pal2al(_annolist): annotations = [] for _a in _annolist.annotation: anno = AnnotationLib.Annotation() anno.imageName = _a.imageName anno.rects = [] for _r in _a.rect: rect = AnnotationLib.AnnoRect() rect.x1 = _r.x1 rect.x2 = _r.x2 rect.y1 = _r.y1 rect.y2 = _r.y2 if _r.HasField("id"): rect.id = _r.id if _r.HasField("track_id"): rect.track_id = _r.track_id if _r.HasField("score"): rect.score = _r.score if _r.HasField("distance3d"): rect.distance3d = _r.distance3d if _r.HasField("width3d"): rect.width3d = _r.width3d if _r.HasField("height3d"): rect.height3d = _r.height3d if _r.HasField("length3d"): rect.length3d = _r.length3d anno.rects.append(rect) annotations.append(anno) return annotations
def calculate_medium_box(boxes): conf_sum = reduce(lambda t, b: t + b.score, boxes, 0) aggregation = {} for name in ['x1', 'y1', 'x2', 'y2']: aggregation[name] = reduce(lambda t, b: t + b.__dict__[name] * b.score, boxes, 0) / conf_sum new_box = al.AnnoRect(**aggregation) new_box.classID = boxes[0].classID new_box.score = conf_sum / len(boxes) return new_box
def save_results(image_path, anno, fname='result'): """Saves results of the prediction. Args: image_path (string): The path to source image to predict bounding boxes. anno (Annotation, list): The predicted annotations for source image or the list of bounding boxes. Returns: Nothing. """ # draw new_img = Image.open(image_path) d = ImageDraw.Draw(new_img) is_list = type(anno) is list rects = anno if is_list else anno.rects for r in rects: if is_list: d.rectangle([r['x1'], r['y1'], r['x2'], r['y2']], outline=(255, 0, 0)) else: d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(255, 0, 0)) # save prediction_image_path = os.path.join(os.path.dirname(image_path), fname + '.png') new_img.save(prediction_image_path) subprocess.call(['chmod', '644', prediction_image_path]) fpath = os.path.join(os.path.dirname(image_path), fname + '.json') if is_list: json.dump({ 'image_path': prediction_image_path, 'rects': anno }, open(fpath, 'w')) else: al.saveJSON(fpath, anno) subprocess.call(['chmod', '644', fpath])
def merge_labelact_annolist(annolist): name_to_idx = {}; res_annolist = AnnotationLib.AnnoList(); for a in annolist: if a.imageName in name_to_idx: aidx = name_to_idx[a.imageName]; res_annolist[aidx].rects.extend(a.rects); else: res_annolist.append(a); name_to_idx[a.imageName] = len(res_annolist) - 1; return res_annolist;
def do(image, anno=None): """ Does the rotation for image and rectangles for 90 degrees counterclockwise. Args: image (Image): The target image to rotate. anno (Annotation): The annotations to be rotated with the image. Returns (tuple): Rotated image and annotations for it. """ w = image.shape[1] new_image = imrotate(image, 90, reshape=True) if anno is not None: anno.rects = [al.AnnoRect(r.y1, w - r.x2, r.y2, w - r.x1) for r in anno.rects] return new_image, anno
def annotation_empty_from_result(result, columns, url_prefix): annotation = AnnotationLib.Annotation() image_url = get_path_from_s3(result[columns['annotation']], url_prefix) # remove parameters from filename param_idx = image_url.index('&'); annotation.imageName = image_url[:param_idx]; # MA: assume filenames don't contain "," cidx = annotation.imageName.find(',') if cidx >= 0: annotation.imageName = annotation.imageName[:cidx]; assert(len(annotation.imageName) > 0); return annotation
def get_cell_grid(cell_width, cell_height, region_size): cell_regions = [] for iy in xrange(cell_height): for ix in xrange(cell_width): cidx = iy * cell_width + ix ox = (ix + 0.5) * region_size oy = (iy + 0.5) * region_size r = al.AnnoRect(ox - 0.5 * region_size, oy - 0.5 * region_size, ox + 0.5 * region_size, oy + 0.5 * region_size) r.track_id = cidx cell_regions.append(r) return cell_regions
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [] for anno in annolist: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) annos.append(anno) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] for epoch in itertools.count(): random.shuffle(annos) for origin_anno in annos: tiles = preprocess_image(deepcopy(origin_anno), H) for I, anno in tiles: if jitter: jitter_scale_min = 0.9 jitter_scale_max = 1.1 jitter_offset = 16 I, anno = annotation_jitter( I, anno, target_width=H["image_width"], target_height=H["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(H, anno) yield {"image": I, "boxes": boxes, "flags": flags}
def pal2al(_annolist): #annotations = []; annotations = AnnotationLib.AnnoList() for adesc in _annolist.attribute_desc: annotations.attribute_desc[adesc.name] = adesc print "attribute: ", adesc.name, adesc.id for valdesc in adesc.val_to_str: annotations.add_attribute_val(adesc.name, valdesc.s, valdesc.id) attribute_name_from_id = { adesc.id: aname for aname, adesc in annotations.attribute_desc.iteritems() } attribute_dtype_from_id = { adesc.id: adesc.dtype for aname, adesc in annotations.attribute_desc.iteritems() } for _a in _annolist.annotation: anno = AnnotationLib.Annotation() anno.imageName = _a.imageName anno.rects = [] for _r in _a.rect: rect = AnnotationLib.AnnoRect() rect.x1 = _r.x1 rect.x2 = _r.x2 rect.y1 = _r.y1 rect.y2 = _r.y2 if _r.HasField("id"): rect.id = _r.id if _r.HasField("track_id"): rect.track_id = _r.track_id if _r.HasField("score"): rect.score = _r.score for _at in _r.attribute: try: cur_aname = attribute_name_from_id[_at.id] cur_dtype = attribute_dtype_from_id[_at.id] except KeyError as e: print "attribute: ", _at.id print e assert (False) if cur_dtype == AnnotationLib.AnnoList.TYPE_INT32: rect.at[cur_aname] = _at.val elif cur_dtype == AnnotationLib.AnnoList.TYPE_FLOAT: rect.at[cur_aname] = _at.fval elif cur_dtype == AnnotationLib.AnnoList.TYPE_STRING: rect.at[cur_aname] = _at.strval else: assert (False) anno.rects.append(rect) annotations.append(anno) return annotations
def add_rectangles(H, orig_image, confidences, boxes, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25, show_suppressed=True): image = np.copy(orig_image[0]) boxes_r = np.reshape(boxes, (-1, H["grid_height"], H["grid_width"], rnn_len, 4)) confidences_r = np.reshape( confidences, (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes'])) cell_pix_size = H['region_size'] all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])] for n in range(rnn_len): for y in range(H["grid_height"]): for x in range(H["grid_width"]): classID = np.argmax(confidences_r[0, y, x, n, 1:]) + 1 bbox = boxes_r[0, y, x, n, :] abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y w = bbox[2] h = bbox[3] conf = confidences_r[0, y, x, n, classID] all_rects[y][x].append( Rect(abs_cx, abs_cy, w, h, conf, classID)) all_rects_r = [r for row in all_rects for cell in row for r in cell] if use_stitching: from stitch_wrapper import stitch_rects acc_rects = stitch_rects(all_rects, tau) else: acc_rects = all_rects_r if show_suppressed: pairs = [(all_rects_r, (255, 0, 0))] else: pairs = [] pairs.append((acc_rects, (0, 255, 0))) for rect_set, color in pairs: for rect in rect_set: if rect.confidence > min_conf: cv2.rectangle(image, (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)), (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)), color, 2) rects = [] for rect in acc_rects: r = al.AnnoRect() r.x1 = rect.cx - rect.width / 2. r.x2 = rect.cx + rect.width / 2. r.y1 = rect.cy - rect.height / 2. r.y2 = rect.cy + rect.height / 2. r.score = rect.true_confidence # r.classID = rect.classID rects.append(r) return image, rects
def al2pal(annotations): _annolist = AnnoList_pb2.AnnoList(); #assert(isinstance(annotations, AnnotationLib.AnnoList)); # check type of attributes, add missing attributes for a in annotations: for r in a.rects: for k, v in r.at.iteritems(): if not k in annotations.attribute_desc: annotations.add_attribute(k, type(v)); else: assert(AnnotationLib.is_compatible_attr_type(annotations.attribute_desc[k].dtype, type(v))); # check attributes values for a in annotations: for r in a.rects: for k, v in r.at.iteritems(): if k in annotations.attribute_val_to_str: # don't allow undefined values if not v in annotations.attribute_val_to_str[k]: print "attribute: {}, undefined value: {}".format(k, v); assert(False); # store attribute descriptions in pal structure for aname, adesc in annotations.attribute_desc.iteritems(): _annolist.attribute_desc.extend([adesc]); for a in annotations: _a = _annolist.annotation.add(); _a.imageName = a.imageName; for r in a.rects: _r = _a.rect.add(); _r.x1 = r.x1; _r.y1 = r.y1; _r.x2 = r.x2; _r.y2 = r.y2; _r.score = float(r.score); if hasattr(r, 'id'): _r.id = r.id; if hasattr(r, 'track_id'): _r.track_id = r.track_id; if hasattr(r, 'at'): for k, v in r.at.items(): _at = _r.attribute.add(); _at.id = annotations.attribute_desc[k].id; if annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_INT32: assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_INT32, type(v))); _at.val = int(v); elif annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_FLOAT: assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_FLOAT, type(v))); _at.fval = float(v); elif annotations.attribute_desc[k].dtype == AnnotationLib.AnnoList.TYPE_STRING: assert(AnnotationLib.is_compatible_attr_type(AnnotationLib.AnnoList.TYPE_STRING, type(v))); _at.strval = str(v); else: assert(false); return _annolist;
if not os.path.exists(opts.output_dir): print "ERROR: output dir not found" exit() if not os.path.exists(opts.images_dir): print "ERROR: images dir not found" exit() imglist = glob.glob(opts.images_dir + "/*.jpeg") imglist.sort() # create image list annolist = [] for imgidx in xrange(0, len(imglist)): anno = AnnotationLib.Annotation() anno.imageName = imglist[imgidx] anno.rects = [] annolist.append(anno) # load vatic tracks vatic_dump = pickle.load(open(opts.vatic_filename, "rb")) num_tracks = len(vatic_dump) print "number of tracks: ", num_tracks for tidx in xrange(0, num_tracks): vatic_boxes = vatic_dump[tidx]["boxes"] track_len = len(vatic_boxes) print "track ", tidx, ", track_len: ", track_len
def main(): parser = argparse.ArgumentParser() parser.add_argument("hits_dir", help="hit directory") parser.add_argument("--print_empty", action="store_true", help="print info on empty hits"); parser.add_argument("--save_worker_results", action="store_true", help="save results for each individual worker"); #parser.add_argument("output_ext", default=".al", help="output format .idl/.al/.pal") #parser.add_argument("--url_prefix", help="path on S3 that should be removed when converting to annolist") args = parser.parse_args() #url_prefix = bash_var_to_py("./data_utils_init.sh", "S3_HOST_DIR") url_prefix = bash_var_to_py(args.hits_dir + "/hit_params.sh", "S3_HOST_DIR") args.output_ext = ".pal" if url_prefix[-1] != os.sep: url_prefix += os.sep; hit_name = get_hit_name(args.hits_dir) #pal_name = args.hits_dir + '/' + hit_name + '.pal' pal_name = args.hits_dir + '/' + hit_name + args.output_ext results_filename = args.hits_dir + '/' + hit_name + '.results' results_by_worker_dir = '%s/results_by_worker_%s' % (args.hits_dir, hit_name) subprocess.call(['mkdir', '-p', results_by_worker_dir]) try: with open(args.hits_dir + '/bad_workers.txt', 'r') as f: bad_workerids = set(x.strip() for x in f.readlines()) except IOError: bad_workerids = set() with open(results_filename, 'r') as results_file: results_list = list(csv.reader(results_file, delimiter='\t')) invert = lambda d: {v:k for k,v in d.iteritems()} columns = invert(dict(enumerate(results_list[0]))) annotation_list = AnnotationLib.AnnoList(); hit_type = ""; for each in results_list[1:]: if each[columns['hitstatus']] == 'Reviewable' and each[columns['workerid']] not in bad_workerids: cur_hit_type = each[columns['Answer.results']].split(',')[0]; if not hit_type: hit_type = cur_hit_type; else: assert(hit_type == cur_hit_type); a = annotation_from_result(each, columns, url_prefix); annotation_list.append(a); # MA: some hit types require special post-processing if hit_type == "label_act": annotation_list = merge_labelact_annolist(annotation_list); annotation_list.add_attribute("gender", int); annotation_list.add_attribute("ptype", int); annotation_list.add_attribute("act", int); annotation_list.add_attribute_val("gender", "male", ATTR_VAL_GENDER_MALE); annotation_list.add_attribute_val("gender", "female", ATTR_VAL_GENDER_FEMALE); annotation_list.add_attribute_val("ptype", "sales", ATTR_VAL_PTYPE_SALES); annotation_list.add_attribute_val("ptype", "customer", ATTR_VAL_PTYPE_CUST); annotation_list.add_attribute_val("act", "interact_with_customer", ATTR_VAL_ACT_SALES_INT); annotation_list.add_attribute_val("act", "clean", ATTR_VAL_ACT_SALES_CLEAN); annotation_list.add_attribute_val("act", "other_sales", ATTR_VAL_ACT_SALES_OTHER); annotation_list.add_attribute_val("act", "queue", ATTR_VAL_ACT_CUST_QUEUE); annotation_list.add_attribute_val("act", "interact_with_sales", ATTR_VAL_ACT_CUST_INT); annotation_list.add_attribute_val("act", "browse", ATTR_VAL_ACT_CUST_BROWSE); annotation_list.add_attribute_val("act", "other_customer", ATTR_VAL_ACT_CUST_OTHER); AnnotationLib.save(pal_name, annotation_list) if args.save_worker_results: for workerid in set(x[columns['workerid']] for x in results_list[1:]): if workerid == '': print '%s missing entries' % sum(1 for each in results_list[1:] if each[columns['workerid']] == '') continue annotation_list = AnnotationLib.AnnoList([annotation_from_result(each, columns, url_prefix) for each in results_list[1:] if each[columns['hitstatus']] == 'Reviewable' and each[columns['workerid']] == workerid]); output_filename = '%s/%s.pal' % (results_by_worker_dir, workerid); #print "saving ", output_filename; print "worker: {}, number of annotations: {}".format(workerid, len(annotation_list)); AnnotationLib.save(output_filename, annotation_list) if args.print_empty: for res in results_list[1:]: if res[columns['Answer.results']] == "": print "empty hit: ", res # show statistics on empty results (happens due to unknown bug in javascript tool) empty_res_workers = [each[columns['workerid']] for each in results_list[1:] if each[columns['Answer.results']] == ""]; print "empty output by workerid: " for workerid, worker_empty_count in collections.Counter(empty_res_workers).items(): print workerid, worker_empty_count num_empty = sum((1 for each in results_list[1:] if each[columns['Answer.results']] == "")); print "number of empty results: ", num_empty
parser.add_argument( "annolist_filename", help= "annotation list in idl/al/pal format, object bounding boxes in the annotation list will be pre-loaded to mturk" ) parser.add_argument("--empty_only", action="store_true", help="only add images without annotations") args = parser.parse_args() print "hit_dir: ", args.hit_dir print "annolist_filename: ", args.annolist_filename annolist = al.parse(args.annolist_filename) print "generating hits for {0} images".format(len(annolist)) # load hit-specific parameters if not os.path.isdir(args.hit_dir): print args.hit_dir, "does not exist, exiting..." sys.exit() else: hit_params_filename = args.hit_dir + "/hit_params.sh" print "loading hit parameters from: ", hit_params_filename if not os.path.isfile(hit_params_filename): print hit_params_filename, "does not exist, exiting..." sys.exit()
def __init__(self, al_file): self.anns = al.parse(al_file)
def al2pal(annotations): _annolist = AnnoList_pb2.AnnoList() #assert(isinstance(annotations, AnnotationLib.AnnoList)); # check type of attributes, add missing attributes for a in annotations: for r in a.rects: for k, v in r.at.iteritems(): if not k in annotations.attribute_desc: annotations.add_attribute(k, type(v)) else: assert (AnnotationLib.is_compatible_attr_type( annotations.attribute_desc[k].dtype, type(v))) # check attributes values for a in annotations: for r in a.rects: for k, v in r.at.iteritems(): if k in annotations.attribute_val_to_str: # don't allow undefined values if not v in annotations.attribute_val_to_str[k]: print "attribute: {}, undefined value: {}".format( k, v) assert (False) # store attribute descriptions in pal structure for aname, adesc in annotations.attribute_desc.iteritems(): _annolist.attribute_desc.extend([adesc]) for a in annotations: _a = _annolist.annotation.add() _a.imageName = a.imageName for r in a.rects: _r = _a.rect.add() _r.x1 = r.x1 _r.y1 = r.y1 _r.x2 = r.x2 _r.y2 = r.y2 _r.score = float(r.score) if hasattr(r, 'id'): _r.id = r.id if hasattr(r, 'track_id'): _r.track_id = r.track_id if hasattr(r, 'at'): for k, v in r.at.items(): _at = _r.attribute.add() _at.id = annotations.attribute_desc[k].id if annotations.attribute_desc[ k].dtype == AnnotationLib.AnnoList.TYPE_INT32: assert (AnnotationLib.is_compatible_attr_type( AnnotationLib.AnnoList.TYPE_INT32, type(v))) _at.val = int(v) elif annotations.attribute_desc[ k].dtype == AnnotationLib.AnnoList.TYPE_FLOAT: assert (AnnotationLib.is_compatible_attr_type( AnnotationLib.AnnoList.TYPE_FLOAT, type(v))) _at.fval = float(v) elif annotations.attribute_desc[ k].dtype == AnnotationLib.AnnoList.TYPE_STRING: assert (AnnotationLib.is_compatible_attr_type( AnnotationLib.AnnoList.TYPE_STRING, type(v))) _at.strval = str(v) else: assert (false) return _annolist
parser = argparse.ArgumentParser() parser.add_argument( "hit_dir", help="hit directory for storing mturk related data (eg. hit id's and worker results)" ) parser.add_argument( "annolist_filename", help="annotation list in idl/al/pal format, object bounding boxes in the annotation list will be pre-loaded to mturk", ) args = parser.parse_args() print "hit_dir: ", args.hit_dir print "annolist_filename: ", args.annolist_filename annolist = al.parse(args.annolist_filename) print "generating hits for {0} images".format(len(annolist)) # load hit-specific parameters if not os.path.isdir(args.hit_dir): print args.hit_dir, "does not exist, exiting..." sys.exit() else: hit_params_filename = args.hit_dir + "/hit_params.sh" print "loading hit parameters from: ", hit_params_filename if not os.path.isfile(hit_params_filename): print hit_params_filename, "does not exist, exiting..." sys.exit()
rect.x1 = vatic_boxes[bidx].xtl; rect.y1 = vatic_boxes[bidx].ytl; rect.x2 = vatic_boxes[bidx].xbr; rect.y2 = vatic_boxes[bidx].ybr cur_frame = vatic_boxes[bidx].frame; annolist[cur_frame].rects.append(rect); # save annolist fname_ext = os.path.basename(opts.vatic_filename); fname = os.path.splitext(fname_ext)[0]; pal_filename = opts.output_dir + "/" + fname + ".pal"; print "saving ", pal_filename; AnnotationLib.save(pal_filename, annolist); # if not os.path.exists(opts.output_dir): # print "creating ", opts.output_dir; # os.makedirs(opts.output_dir); # imglist = glob.glob(opts.input_dir + "/*.jpeg"); # imglist.sort(); # idxlist = range(0, len(imglist), opts.imgstep); # print idxlist;
def inv(r): rotated_back = al.AnnoRect(width - r.y2, r.x1, width - r.y1, r.x2) rotated_back.score = r.score return rotated_back