for i in tqdm(range(0, len(df))): filename = df.iloc[i]['filename'] x1 = df.iloc[i]['bbox_x1'] y1 = df.iloc[i]['bbox_y1'] x2 = df.iloc[i]['bbox_x2'] y2 = df.iloc[i]['bbox_y2'] src = Image.open(osp.join(opt.dataroot, 'cars_' + datamode, filename)) area = [x1, y1, x2, y2] cropped_img = src.crop(area) cropped_img.save(os.path.join(output_dir, filename)) if __name__ == '__main__': option_parser = BaseOptions() opt = option_parser.parse() devkit_path = 'data/devkit/' cars_train = 'data/cars_train/' cars_test = 'data/cars_test/' car_meta = loadmat(devkit_path + 'cars_meta.mat') cars_train_annos = loadmat(devkit_path + 'cars_train_annos.mat') cars_test_annos = loadmat(devkit_path + 'cars_test_annos.mat') train_df = load_meta(cars_train_annos, 'train') test_df = load_meta(cars_test_annos, 'test') crop_image(opt, train_df, osp.join(opt.dataroot, 'cars_train' + '_cropped'), 'train') crop_image(opt, test_df, osp.join(opt.dataroot, 'cars_test' + '_cropped'), 'test')
# parser.add_argument('--cuda', default=True, type=str2bool, # help='Use cuda to train model') # parser.add_argument('--voc_root', default=VOCroot, help='Location of VOC root directory') # # args = parser.parse_args() # # if not os.path.exists(args.save_folder): # os.mkdir(args.save_folder) # # if args.cuda and torch.cuda.is_available(): # torch.set_default_tensor_type('torch.cuda.FloatTensor') # else: # torch.set_default_tensor_type('torch.FloatTensor') options = BaseOptions() args = options.parse() options.setup_option() annopath = os.path.join(args.voc_root, 'VOC2007', 'Annotations', '%s.xml') imgpath = os.path.join(args.voc_root, 'VOC2007', 'JPEGImages', '%s.jpg') imgsetpath = os.path.join(args.voc_root, 'VOC2007', 'ImageSets', 'Main', '{:s}.txt') YEAR = '2007' devkit_path = VOCroot + 'VOC' + YEAR dataset_mean = (104, 117, 123) set_type = 'test' class Timer(object): """A simple timer.""" def __init__(self): self.total_time = 0.