def perturb_frame(frame, params): dims = frame.image.get_hw() rand_affine = RandomPerturber.generate_random_affine( dims / 2, dims, params) perturbed_frame = Frame(frame.image_path, copy.deepcopy(frame.objects)) perturbed_frame.image = rand_affine.apply_to_image(frame.image, dims) for i, obj in enumerate(frame.objects): perturbed_frame.objects[i].box = rand_affine.apply_to_box(obj.box) return perturbed_frame
def perturb_frame(frame,params): dims = frame.image.get_hw() rand_affine = RandomPerturber.generate_random_affine(dims/2,dims,params) perturbed_frame = Frame(frame.image_path) perturbed_frame.image = rand_affine.apply_to_image(frame.image,dims) for i,obj in enumerate(frame.objects): # filter out completely out of bound objects perturbed_obj_box = rand_affine.apply_to_box(obj.box) perturbed_polygons = rand_affine.apply_to_polygons(obj.polygons) if Box.intersection(perturbed_obj_box,perturbed_frame.image.get_bounding_box()) is not None: obj_copy = copy.deepcopy(obj) obj_copy.box = perturbed_obj_box obj_copy.polygons = perturbed_polygons perturbed_frame.objects.append(obj_copy) return perturbed_frame
def apply_affine_to_frame(frame, affine, output_size): perturbed_frame = Frame(frame.image_path) perturbed_frame.image = affine.apply_to_image(frame.image, output_size) for i, obj in enumerate(frame.objects): # filter out completely out of bound objects perturbed_obj_box = affine.apply_to_box(obj.box) perturbed_polygons = affine.apply_to_polygons(obj.polygons) if Box.intersection( perturbed_obj_box, perturbed_frame.image.get_bounding_box()) is not None: obj_copy = copy.deepcopy(obj) obj_copy.box = perturbed_obj_box obj_copy.polygons = perturbed_polygons perturbed_frame.objects.append(obj_copy) return perturbed_frame
def __getitem__(self, index): image, labels = self.dataset[index] np_arr = np.asarray(image) ptimage = PTImage.from_numpy_array(np_arr) objects = [] for t in labels: box = Box.from_xywh(t['bbox']) obj_type = self.coco.loadCats([t['category_id']])[0]['name'] # convert segmentation to polygon using the pycocotools # note the segmentation could in one of several formats, for example the custom coco RLE, # to convert the RLE back to polygon is bit of a pain so I will just ignore those right now # according the COCO site, most of the data is in polygon form (not sure why theres a discrepency?) # and I'd rather not store 2D binary masks with every object. polygon = t.get('segmentation') # reshape to 2d poly, assume its convex hull? polys = [] if polygon and isinstance(polygon, list): for seg in polygon: polys.append( Polygon( np.array(seg).reshape((int(old_div(len(seg), 2)), 2)))) objects.append(Object(box, obj_type=obj_type, polygons=polys)) frame = Frame.from_image_and_objects(ptimage, objects) return frame
def __getitem__(self,index): pil_img,label = self.dataset[index] # assert 2D here np_arr = np.asarray(pil_img) np_arr = np.expand_dims(np_arr, axis=2) # create the PTImage, and object that span the frame # add extra channel dimension ptimage = PTImage.from_numpy_array(np_arr) obj = Object(Box(0,0,pil_img.size[0],pil_img.size[1])) frame = Frame.from_image_and_objects(ptimage,[obj]) return frame
def __load_labelled_frames(self, frame_dir, labels_file): files = [f for f in listdir(frame_dir) if isfile(join(frame_dir, f))] labels = KITTILabel.labels_from_file( labels_file) if labels_file is not None else [] frames = [] for f in files: file_index = int(splitext(basename(f))[0]) objects = [] for l in labels: if int(l.frame_idx) == file_index: objects.append(l.to_object()) frames.append(Frame(join(frame_dir, f), objects)) return frames
def __load_labelled_frames(self,frame_dir,labels_file,calib_file=None): files = [f for f in listdir(frame_dir) if isfile(join(frame_dir, f))] labels = KITTILabel.labels_from_file(labels_file) if labels_file is not None else [] calibration_mat = None if os.path.isfile(calib_file): calibration_mat = self.__load_camera_matrix_from_calib(calib_file) frames = [] sorted_files = sorted(files, key=lambda x: int(splitext(basename(x))[0])) for f in sorted_files: file_index = int(splitext(basename(f))[0]) objects = [] for l in labels: if int(l.frame_idx) == file_index: objects.append(l.to_object()) frames.append(Frame(join(frame_dir,f),objs=objects,calib_mat=calibration_mat)) return frames
def __load_frames(self, cars_dir, labels_mat): print('Loading Stanford Cars Frames') labels = scipy.io.loadmat(labels_mat)['annotations'][0] # load frames with labels for label in labels: if len(label) == 5: xmin, ymin, xmax, ymax, path = label elif len(label) == 6: xmin, ymin, xmax, ymax, _, path = label else: assert False, 'unable to parse label!' box = Box(float(xmin[0][0]), float(ymin[0][0]), float(xmax[0][0]), float(ymax[0][0])) obj = Object(box, obj_type='car') image_path = os.path.join(cars_dir, path[0]) self.frames.append(Frame(image_path, [obj]))