def __init__(self, root_dir, transform=None): samples = parse_car_csv(root_dir) jpeg_images = [ cv2.imread( os.path.join(root_dir, 'JPEGImages', sample_name + ".jpg")) for sample_name in samples ] positive_annotations = [ os.path.join(root_dir, 'Annotations', sample_name + '_1.csv') for sample_name in samples ] negative_annotations = [ os.path.join(root_dir, 'Annotations', sample_name + '_0.csv') for sample_name in samples ] # 边界框大小 positive_sizes = list() negative_sizes = list() # 边界框坐标 positive_rects = list() negative_rects = list() for annotation_path in positive_annotations: rects = np.loadtxt(annotation_path, dtype=np.int, delimiter=' ') # 存在文件为空或者文件中仅有单行数据 if len(rects.shape) == 1: # 是否为单行 if rects.shape[0] == 4: positive_rects.append(rects) positive_sizes.append(1) else: positive_sizes.append(0) else: positive_rects.extend(rects) positive_sizes.append(len(rects)) for annotation_path in negative_annotations: rects = np.loadtxt(annotation_path, dtype=np.int, delimiter=' ') # 和正样本规则一样 if len(rects.shape) == 1: if rects.shape[0] == 4: negative_rects.append(rects) negative_sizes.append(1) else: positive_sizes.append(0) else: negative_rects.extend(rects) negative_sizes.append(len(rects)) self.transform = transform self.jpeg_images = jpeg_images self.positive_sizes = positive_sizes self.negative_sizes = negative_sizes self.positive_rects = positive_rects self.negative_rects = negative_rects self.total_positive_num = int(np.sum(positive_sizes)) self.total_negative_num = int(np.sum(negative_sizes))
def __init__(self, root_dir, transform): """ 加载所有的图像以及正负样本边界框 """ self.transform = transform samples = util.parse_car_csv(root_dir) jpeg_images = list() annotation_dict = dict() for idx in range(len(samples)): sample_name = samples[idx] img = cv2.imread( os.path.join(root_dir, 'JPEGImages', sample_name + ".jpg")) h, w = img.shape[:2] jpeg_images.append(img) positive_annotation_path = os.path.join(root_dir, 'Annotations', sample_name + '_1.csv') positive_annotations = np.loadtxt(positive_annotation_path, dtype=np.float, delimiter=' ') if len(positive_annotations.shape) == 1: positive_annotations = positive_annotations[np.newaxis, :] # print(positive_annotations.shape) positive_annotations[:, 0] /= w positive_annotations[:, 1] /= h positive_annotations[:, 2] /= w positive_annotations[:, 3] /= h # if len(positive_annotations) < 16: # print(sample_name) negative_annotation_path = os.path.join(root_dir, 'Annotations', sample_name + '_0.csv') negative_annotations = np.loadtxt(negative_annotation_path, dtype=np.float, delimiter=' ') negative_annotations[:, 0] /= w negative_annotations[:, 1] /= h negative_annotations[:, 2] /= w negative_annotations[:, 3] /= h annotation_dict[str(idx)] = { 'positive': positive_annotations, 'negative': negative_annotations } self.jpeg_images = jpeg_images self.annotation_dict = annotation_dict
def __init__(self, root_dir, transform=None): super(BBoxRegressionDataset, self).__init__() self.transform = transform samples = util.parse_car_csv(root_dir) jpeg_list = list() # 保存{'image_id': ?, 'positive': ?, 'bndbox': ?} box_list = list() for i in range(len(samples)): sample_name = samples[i] jpeg_path = os.path.join(root_dir, 'JPEGImages', sample_name + '.jpg') bndbox_path = os.path.join(root_dir, 'bndboxs', sample_name + '.csv') positive_path = os.path.join(root_dir, 'positive', sample_name + '.csv') jpeg_list.append(cv2.imread(jpeg_path)) bndboxes = np.loadtxt(bndbox_path, dtype=np.int, delimiter=' ') positives = np.loadtxt(positive_path, dtype=np.int, delimiter=' ') if len(positives.shape) == 1: bndbox = self.get_bndbox(bndboxes, positives) box_list.append({ 'image_id': i, 'positive': positives, 'bndbox': bndbox }) else: for positive in positives: bndbox = self.get_bndbox(bndboxes, positive) box_list.append({ 'image_id': i, 'positive': positive, 'bndbox': bndbox }) self.jpeg_list = jpeg_list self.box_list = box_list
for name in ['train', 'val']: src_root_dir = os.path.join(car_root_dir, name) src_annotation_dir = os.path.join(src_root_dir, 'Annotations') src_jpeg_dir = os.path.join(src_root_dir, 'JPEGImages') dst_root_dir = os.path.join(finetune_root_dir, name) dst_annotation_dir = os.path.join(dst_root_dir, 'Annotations') dst_jpeg_dir = os.path.join(dst_root_dir, 'JPEGImages') check_dir(dst_root_dir) check_dir(dst_annotation_dir) check_dir(dst_jpeg_dir) total_num_positive = 0 total_num_negative = 0 samples = parse_car_csv(src_root_dir) # 复制csv文件 src_csv_path = os.path.join(src_root_dir, 'car.csv') dst_csv_path = os.path.join(dst_root_dir, 'car.csv') shutil.copyfile(src_csv_path, dst_csv_path) for sample_name in samples: since = time.time() src_annotation_path = os.path.join(src_annotation_dir, sample_name + '.xml') src_jpeg_path = os.path.join(src_jpeg_dir, sample_name + '.jpg') # 获取正负样本 positive_list, negative_list = parse_annotation_jpeg( src_annotation_path, src_jpeg_path, gs) total_num_positive += len(positive_list) total_num_negative += len(negative_list)
def __init__(self, root_dir, transform=None): samples = parse_car_csv(root_dir) jpeg_images = list() positive_list = list() negative_list = list() for idx in range(len(samples)): sample_name = samples[idx] jpeg_images.append( cv2.imread( os.path.join(root_dir, 'JPEGImages', sample_name + ".jpg"))) positive_annotation_path = os.path.join(root_dir, 'Annotations', sample_name + '_1.csv') positive_annotations = np.loadtxt(positive_annotation_path, dtype=np.int, delimiter=' ') # 考虑csv文件为空或者仅包含单个标注框 if len(positive_annotations.shape) == 1: # 单个标注框坐标 if positive_annotations.shape[0] == 4: positive_dict = dict() positive_dict['rect'] = positive_annotations positive_dict['image_id'] = idx # positive_dict['image_name'] = sample_name positive_list.append(positive_dict) else: for positive_annotation in positive_annotations: positive_dict = dict() positive_dict['rect'] = positive_annotation positive_dict['image_id'] = idx # positive_dict['image_name'] = sample_name positive_list.append(positive_dict) negative_annotation_path = os.path.join(root_dir, 'Annotations', sample_name + '_0.csv') negative_annotations = np.loadtxt(negative_annotation_path, dtype=np.int, delimiter=' ') # 考虑csv文件为空或者仅包含单个标注框 if len(negative_annotations.shape) == 1: # 单个标注框坐标 if negative_annotations.shape[0] == 4: negative_dict = dict() negative_dict['rect'] = negative_annotations negative_dict['image_id'] = idx # negative_dict['image_name'] = sample_name negative_list.append(negative_dict) else: for negative_annotation in negative_annotations: negative_dict = dict() negative_dict['rect'] = negative_annotation negative_dict['image_id'] = idx # negative_dict['image_name'] = sample_name negative_list.append(negative_dict) self.transform = transform self.jpeg_images = jpeg_images self.positive_list = positive_list self.negative_list = negative_list
classifier_car_train_dir = '../../data/finetune_car/train' # positive positive_annotation_dir = os.path.join(classifier_car_train_dir, 'Annotations') dst_root_dir = '../../data/bbox_regression/' dst_jpeg_dir = os.path.join(dst_root_dir, 'JPEGImages') dst_bndbox_dir = os.path.join(dst_root_dir, 'bndboxs') dst_positive_dir = os.path.join(dst_root_dir, 'positive') util.check_dir(dst_root_dir) util.check_dir(dst_jpeg_dir) util.check_dir(dst_bndbox_dir) util.check_dir(dst_positive_dir) samples = util.parse_car_csv(voc_car_train_dir) res_samples = list() total_positive_num = 0 for sample_name in samples: # 提取正样本边界框坐标(IoU>=0.5) positive_annotation_path = os.path.join(positive_annotation_dir, sample_name + '_1.csv') positive_bndboxes = np.loadtxt(positive_annotation_path, dtype=np.int, delimiter=' ') # 提取标注边界框 gt_annotation_path = os.path.join(gt_annotation_dir, sample_name + '.xml') bndboxs = util.parse_xml(gt_annotation_path) # 计算符合条件(IoU>0.6)的候选建议 positive_list = list() if len(positive_bndboxes.shape) == 1 and len(positive_bndboxes) != 0: scores = util.iou(positive_bndboxes, bndboxs) if np.max(scores) > 0.6: positive_list.append(positive_bndboxes)