Esempio n. 1
0
    def _check_load_bbox(self, entry, instance_id):
        """
        Check and load ground-truth labels
        """
        entry_id = entry['id']
        entry_id = [entry_id] if not isinstance(entry_id,
                                                (list, tuple)) else entry_id
        ann_ids = self.dataset.getAnnIds(imgIds=entry_id, iscrowd=None)
        objs = self.dataset.loadAnns(ann_ids)

        # check valid bboxes
        valid_objs = []
        width = entry['width']
        height = entry['height']
        _instance_count = 0
        _redudant_count = 0
        _amodal_count = 0
        unique_bbs = set()
        for obj in objs:
            if obj.get('ignore', 0) == 1:
                continue
            if not self._use_crowd and obj.get('iscrowd', 0):
                continue
            if self._amodal:
                xmin, ymin, xmax, ymax = bbox_xywh_to_xyxy(obj['bbox'])
                if xmin < 0 or ymin < 0 or xmax > width or ymax > height:
                    _amodal_count += 1
            else:
                xmin, ymin, xmax, ymax = bbox_clip_xyxy(
                    bbox_xywh_to_xyxy(obj['bbox']), width, height)

            if (xmin, ymin, xmax, ymax) in unique_bbs:
                _redudant_count += 1
                continue

            box_w = (xmax - xmin)
            box_h = (ymax - ymin)
            area = box_w * box_h
            if area <= self._min_object_area:
                continue

            # require non-zero box area
            if xmax > xmin and ymax > ymin:
                unique_bbs.add((xmin, ymin, xmax, ymax))
                contiguous_cid = self.json_category_id_to_contiguous_id[
                    obj['category_id']]
                valid_objs.append([
                    xmin, ymin, xmax, ymax, contiguous_cid,
                    instance_id + _instance_count
                ])
                _instance_count += 1
        if not valid_objs:
            if not self._skip_empty:
                # dummy invalid labels if no valid objects are found
                valid_objs.append([-1, -1, -1, -1, -1, -1])
        return valid_objs, _instance_count, _redudant_count, _amodal_count
Esempio n. 2
0
    def _loadtxt(self):
        """Load all annotations into memory."""
        logging.debug("Loading %s annotations into memory...", str(self))

        items = []
        labels = []
        for name in self._splits:
            img_root = os.path.join(self._root, 'WIDER_{}',
                                    'images').format(name)
            anno_txt = os.path.join(self._root, 'wider_face_split',
                                    'wider_face_{}_bbx_gt.txt').format(name)
            with open(anno_txt, 'r') as f:
                while True:
                    img_path = f.readline().strip()
                    if img_path == '':
                        break
                    num = int(f.readline().strip())
                    label = []
                    for i in range(num):
                        annos = [int(a) for a in f.readline().strip().split()]
                        annos[2] += 1  # inter-w to real-w
                        annos[3] += 1  # inter-h to real-h
                        if 'train' in self._splits:
                            attr = edict()
                            attr.width = annos[2]
                            attr.height = annos[3]
                            attr.blur = annos[4]
                            attr.expression = annos[5]
                            attr.illumination = annos[6]
                            attr.invalid = annos[7]
                            attr.occlusion = annos[8]
                            attr.pose = annos[9]
                            if self._ignore_face(attr):
                                continue
                        if len(annos) == 7:
                            # annos = bbox_xywh_to_xyxy(map(float, annos[:4])) + tuple(annos[4:])
                            annos = bbox_xywh_to_xyxy(
                                ([float(item)
                                  for item in annos[:4]])) + tuple(annos[4:])
                        else:
                            # annos = bbox_xywh_to_xyxy(map(float, annos[:4]))
                            annos = bbox_xywh_to_xyxy(
                                ([float(item) for item in annos[:4]]))
                        label.append(annos)
                    if len(label) > 0 or not self._skip_empty:
                        items.append(os.path.join(img_root, img_path))
                        labels.append(np.array(label, dtype=np.float32))
        return items, labels
Esempio n. 3
0
 def _check_load_bbox(self, coco, entry):
     """Check and load ground-truth labels"""
     entry_id = entry['id']
     # fix pycocotools _isArrayLike which don't work for str in python3
     entry_id = [entry_id] if not isinstance(entry_id,
                                             (list, tuple)) else entry_id
     ann_ids = coco.getAnnIds(imgIds=entry_id, iscrowd=None)
     objs = coco.loadAnns(ann_ids)
     # check valid bboxes
     valid_objs = []
     width = entry['width']
     height = entry['height']
     for obj in objs:
         if obj['area'] < self._min_object_area:
             continue
         if obj.get('ignore', 0) == 1:
             continue
         if not self._use_crowd and obj.get('iscrowd', 0):
             continue
         # convert from (x, y, w, h) to (xmin, ymin, xmax, ymax) and clip bound
         xmin, ymin, xmax, ymax = bbox_clip_xyxy(
             bbox_xywh_to_xyxy(obj['bbox']), width, height)
         # require non-zero box area
         if obj['area'] > 0 and xmax > xmin and ymax > ymin:
             contiguous_cid = self.json_id_to_contiguous[obj['category_id']]
             valid_objs.append([xmin, ymin, xmax, ymax, contiguous_cid])
     if not valid_objs:
         if self._allow_empty:
             # dummy invalid labels if no valid objects are found
             valid_objs.append([-1, -1, -1, -1, -1])
     return valid_objs
Esempio n. 4
0
    def _load_txt(self):
        """load all annotations into memory"""
        logging.debug('Loading %s annotations into memory....', str(self))

        items = []
        labels = []

        data_root = os.path.join(self._root, self._dataset)
        for name in self._splits:
            anno_txt = os.path.join(data_root, 'yuncong_face_split',
                                    "{}_{}.txt").format(self._dataset, name)
            with open(anno_txt, 'r') as f:
                while True:
                    record = f.readline().strip().split()
                    if len(record) == 0:
                        break
                    imgpath = record[0].strip()
                    num = int(record[1].strip())
                    label = []
                    for i in range(num):
                        bbox = record[2 + i * 5:2 + (i + 1) * 5]
                        bbox = [float(x) for x in bbox]
                        bbox = bbox_xywh_to_xyxy(bbox[1:])
                        label.append(bbox)

                    if len(label) > 0 or not self._skip_empty:
                        items.append(os.path.join(data_root, imgpath))
                        labels.append(np.array(label, dtype=np.float32))
        return items, labels
Esempio n. 5
0
 def xywh_to_xyxy(self, pred):
     _pred = []
     for rec in pred:
         rec[2] += 1  # inter-w to real-w
         rec[3] += 1  # inter-h to real-h
         rec = list(bbox_xywh_to_xyxy(rec[:4])) + [rec[4]]
         _pred.append(rec)
     return mx.nd.array(_pred, dtype='float64')
Esempio n. 6
0
            def parse_obj(obj):
                contiguous_cid = self.json_id_to_contiguous[obj['category_id']]
                if contiguous_cid >= self.num_class:
                    # not class of interest
                    return {"reason": 1}
                if max(obj['keypoints']) == 0:
                    return {"reason": 2}
                # convert from (x, y, w, h) to (xmin, ymin, xmax, ymax) and clip bound
                xmin, ymin, xmax, ymax = bbox_clip_xyxy(
                    bbox_xywh_to_xyxy(obj['bbox']), width, height)
                # require non-zero box area
                if obj['area'] <= 0 or xmax <= xmin or ymax <= ymin:
                    return {"reason": 3}

                # joints 3d: (num_joints, 3, 2); 3 is for x, y, z; 2 is for position, visibility
                joints_3d = np.zeros((self.num_joints, 3, 2), dtype=np.float32)
                for i in range(self.num_joints):
                    joints_3d[i, 0, 0] = obj['keypoints'][i * 3 + 0]
                    joints_3d[i, 1, 0] = obj['keypoints'][i * 3 + 1]
                    # joints_3d[i, 2, 0] = 0
                    visible = min(1, obj['keypoints'][i * 3 + 2])
                    joints_3d[i, :2, 1] = visible
                    # joints_3d[i, 2, 1] = 0

                if np.sum(joints_3d[:, 0, 1]) < 1:
                    # no visible keypoint
                    return {"reason": 4}

                if self._check_centers:
                    bbox_center, bbox_area = self._get_box_center_area(
                        (xmin, ymin, xmax, ymax))
                    kp_center, num_vis = self._get_keypoints_center_count(
                        joints_3d)
                    ks = np.exp(-2 *
                                np.sum(np.square(bbox_center - kp_center)) /
                                bbox_area)
                    if (num_vis / 80.0 + 47 / 80.0) > ks:
                        return {"reason": 5}

                return {
                    'bbox': (xmin, ymin, xmax, ymax),
                    'joints_3d': joints_3d,
                    "reason": 0
                }