Esempio n. 1
0
def show_result(img, result, dataset='coco', score_thr=0.3, out_file=None):
    img = mmcv.imread(img)
    class_names = get_classes(dataset)
    if isinstance(result, tuple):
        bbox_result, segm_result = result
    else:
        bbox_result, segm_result = result, None
    bboxes = np.vstack(bbox_result)
    # draw segmentation masks
    if segm_result is not None:
        segms = mmcv.concat_list(segm_result)
        inds = np.where(bboxes[:, -1] > score_thr)[0]
        for i in inds:
            color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)
            mask = maskUtils.decode(segms[i]).astype(np.bool)
            img[mask] = img[mask] * 0.5 + color_mask * 0.5
    # draw bounding boxes
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)
    mmcv.imshow_det_bboxes(img.copy(),
                           bboxes,
                           labels,
                           class_names=class_names,
                           score_thr=score_thr,
                           show=out_file is None)
Esempio n. 2
0
    def show_result(self,
                    data,
                    result,
                    img_norm_cfg,
                    dataset='coco',
                    score_thr=0.3):
        if isinstance(result, tuple):
            bbox_result, segm_result = result
        else:
            bbox_result, segm_result = result, None

        img_tensor = data['img'][0]
        img_metas = data['img_meta'][0].data[0]
        imgs = tensor2imgs(img_tensor, **img_norm_cfg)
        assert len(imgs) == len(img_metas)

        if isinstance(dataset, str):
            class_names = get_classes(dataset)
        elif isinstance(dataset, (list, tuple)) or dataset is None:
            class_names = dataset
        else:
            raise TypeError(
                'dataset must be a valid dataset name or a sequence'
                ' of class names, not {}'.format(type(dataset)))

        for img, img_meta in zip(imgs, img_metas):
            h, w, _ = img_meta['img_shape']
            img_show = img[:h, :w, :]

            bboxes = np.vstack(bbox_result)
            # draw segmentation masks
            if segm_result is not None:
                segms = mmcv.concat_list(segm_result)
                inds = np.where(bboxes[:, -1] > score_thr)[0]
                for i in inds:
                    color_mask = np.random.randint(
                        0, 256, (1, 3), dtype=np.uint8)
                    mask = maskUtils.decode(segms[i]).astype(np.bool)
                    img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5
            # draw bounding boxes
            labels = [
                np.full(bbox.shape[0], i, dtype=np.int32)
                for i, bbox in enumerate(bbox_result)
            ]
            labels = np.concatenate(labels)
            mmcv.imshow_det_bboxes(
                img_show,
                bboxes,
                labels,
                class_names=class_names,
                score_thr=score_thr)
Esempio n. 3
0
    def _process_result(i, result):
        # mask가 없을 때만 1번이다. mask 학습이었다면 2번이어야 한다... 지금은 귀찮음
        bb_result, img_feats = result

        class_names = get_classes(dataset)
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bb_result)
        ]
        labels = np.concatenate(labels)
        bboxes = np.vstack(bb_result)

        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5

        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        bboxes = bboxes[:, :-1]

        # print(i, scores, labels)
        # if i == 23:
        #     exit()

        _scores = list()
        _bboxes = list()
        _labels = list()
        # CLASS = {"사람": 0, "소화기":1, "소화전":2, "차량":3, "자전거":4, "오토바이":5}
        if i == 0:
            for now_label_index, label in enumerate(labels):
                limit_score = checked_score(label)
                score = scores[now_label_index]
                if score < limit_score:
                    continue

                feature = img_feats[now_label_index]
                bbox = bboxes[now_label_index]

                # label, feature, bbox, score, id
                object_info = ObjectInfo(label, feature, bbox, score,
                                         objects_counter[label])
                frame_dict[i][label].append(object_info)
                objects_counter[label] += 1

                _scores.append(score)
                _labels.append(label)
                _bboxes.append(bbox)
        else:
            for now_label_index, label in enumerate(labels):
                # temp_dist = list()
                limit_score = checked_score(label)
                score = scores[now_label_index]
                if score < limit_score:
                    continue

                feature = img_feats[now_label_index]
                bbox = bboxes[now_label_index]

                _scores.append(score)
                _labels.append(label)
                _bboxes.append(bbox)

                # 같은 라벨에 대해서 전부다 하는 것이 아닌 bbox가 가까운 아이들만 검사한다.
                # 좌표 가져오기
                x1, y1, x2, y2 = bbox

                more_before_dist = list()
                # 사람, 소화기, 소화전, 자전거 // 자동차,오토바이 따로 구분해서 처리
                if label in [0, 1, 2, 4]:
                    # 원래는 검사하는 프레임을 i-20으로 20프레임 전만 검사하지만 현재는 테스트 중이므로 50 프레임을 검사한다.
                    # max(i - 20, 0)
                    for before_frame_index in range(max(i - 50, 0), i):
                        more_before_objects = frame_dict[before_frame_index]

                        more_before_label_objects = more_before_objects[label]

                        # debug 용
                        # if len(more_before_label_objects)>0:
                        #     print("index:", before_frame_index, "label:", label)

                        for object_index, before_object in enumerate(
                                more_before_label_objects):
                            # 엇나갈 좌표의 범위는 사람 기준 dist_frame*50.0 px로 둔다.
                            # CLASS = {"사람": 0, "소화기":1, "소화전":2, "차량":3, "자전거":4, "오토바이":5}
                            # 소화기 소화전은 드론 속도만을 고려 하여 프레임당 30px 이동으로 준다.
                            dist_frame = i - before_frame_index
                            if label == 0:
                                dist_px = min(650.0, dist_frame * 200.0)
                            elif label == 1:
                                dist_px = min(650.0, dist_frame * 200.0)
                            elif label == 2:
                                dist_px = min(650.0, dist_frame * 200.0)
                            elif label == 4:
                                dist_px = min(650.0, dist_frame * 200.0)

                            b_x1, b_y1, b_x2, b_y2 = before_object.bbox

                            # a = x1 - b_x1  # 선 a의 길이
                            # b = y1 - b_y1  # 선 b의 길이

                            # print((x1, y1), "//", (b_x1, b_y1), "//", before_object.id)
                            # 테스트용 코드 작성
                            pred_coord_dist = 1000.0
                            predict_coordinate = before_object.get_predict_bbox(
                            )
                            if len(predict_coordinate) > 0:
                                center_x = (x2 + x1) / 2
                                center_y = (y2 + y1) / 2
                                _a = center_x - predict_coordinate[
                                    0]  # 선 a의 길이
                                _b = center_y - predict_coordinate[
                                    1]  # 선 b의 길이

                                # _a = x1 - predict_coordinate[0]  # 선 a의 길이
                                # _b = y1 - predict_coordinate[1]  # 선 b의 길이
                                pred_coord_dist = math.sqrt((_a * _a) +
                                                            (_b * _b))

                            if label == 0 or label == 1 or label == 2:
                                # 90 이었음
                                limit_coord_dist = 140
                            else:
                                limit_coord_dist = 200

                            if dist_frame != 1:
                                for num in range(1, dist_frame + 1):
                                    limit_coord_dist += (limit_coord_dist *
                                                         np.exp(-num) * 2
                                                         )  #np.exp2(-num))

                            edge_dist = 8
                            if x1 < edge_dist or y1 < edge_dist or \
                                x2 > img_size[0] - edge_dist or y2 > img_size[1] - edge_dist and \
                                    (img_size[1] - 100) > pred_coord_dist: # 왜냐하면 왼쪽테두리로 나갔는데 오른쪽 테두리에 나왔을 때를 가정
                                limit_coord_dist *= 1.5

                            # bbox가 최대치 일때 비교와 이전 프레임의 feature 비교를 한다.
                            # 넓이는 최대 8*8 = 64 px 까지는 여유로 둔다.
                            max_dist_feat = 1000.0
                            if max((before_object.area - 64),
                                   0) <= (x2 - x1) * (y2 - y1):
                                max_dist_feat = feature - before_object.max_bbox_feature
                                max_dist_feat = np.dot(max_dist_feat,
                                                       max_dist_feat)

                            _dist_feat = feature - before_object.feature
                            _dist_feat = np.dot(_dist_feat, _dist_feat)

                            dist_feat_list = [_dist_feat]
                            for _index, before_freature in enumerate(
                                    before_object.feature_list):
                                _dist_feat = feature - before_freature
                                _dist_feat = np.dot(_dist_feat, _dist_feat)

                                # 임시로.. 오래된 이미지는 피쳐 신뢰도가 높을 수가 있으므로 약간 가중치를 준다.
                                _dist_feat = _dist_feat * np.exp(-_index)
                                dist_feat_list.append(_dist_feat)

                            dist_feat = min(dist_feat_list)

                            # 일단은 주위 100px 사이로만 검색 한다. (드론 이동이 있음) // 15 프레임까지는 거리 비교를 해본다.
                            # 또한 너무 비슷 하다면 기회를(?) 줘본다.
                            # print(dist_feat, max_dist_feat, dist_frame, "//", before_object.id, "//", pred_coord_dist, "//", b_x1, b_y1, "//", x1, y1)
                            # dist_px = dist_px if dist_frame < 15 else 100
                            # if min(x1+dist_px, img_size[0]) >= b_x1 and max(x1-dist_px,0) <= b_x1 and \
                            #         min(y1 + dist_px, img_size[1]) >= b_y1 and max(y1 - dist_px, 0) <= b_y1 or \
                            #         (pred_coord_dist < 130 and dist_frame < 30):

                            # 원래 13, 9 임
                            # if (dist_frame < 3 and min(x1 + limit_coord_dist, img_size[0]) >= b_x1 and max(x1 - limit_coord_dist, 0) <= b_x1 and \
                            #     min(y1 + limit_coord_dist, img_size[1]) >= b_y1 and max(y1 - limit_coord_dist, 0) <= b_y1) or \
                            #         pred_coord_dist < limit_coord_dist:
                            if (dist_frame < 2 and pred_coord_dist < dist_px
                                ) or pred_coord_dist < limit_coord_dist:
                                # boundary 안에 있더라도 너무 안닮았으면 패스 한다. 200 이었음
                                if dist_feat < 150:
                                    more_before_dist.append([
                                        dist_feat * pred_coord_dist,
                                        before_frame_index, object_index
                                    ])
                            elif dist_feat < 13:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                            elif max_dist_feat < 8:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                else:
                    # 원본 max(i - 5, 0) 자동차 30프레임이였음
                    for before_frame_index in range(max(i - 100, 0), i):
                        # print("index:", before_frame_index)
                        more_before_objects = frame_dict[before_frame_index]

                        more_before_label_objects = more_before_objects[label]
                        for object_index, before_object in enumerate(
                                more_before_label_objects):
                            b_x1, b_y1 = before_object.bbox[:2]

                            # a = x1 - b_x1  # 선 a의 길이
                            # b = y1 - b_y1  # 선 b의 길이
                            # coord_dist = math.sqrt((a * a) + (b * b))

                            dist_frame = i - before_frame_index
                            # if label == 3:
                            #     dist_px = min(650.0, dist_frame * 250.0)
                            # else:
                            #     dist_px = min(650.0, dist_frame * 250.0)

                            # 테스트용 코드 작성
                            is_out_line = False
                            pred_coord_dist = 1500.0
                            predict_coordinate = before_object.get_predict_bbox(
                            )
                            if len(predict_coordinate) > 0:
                                pred_x = predict_coordinate[0]
                                pred_y = predict_coordinate[1]

                                center_x = (x2 + x1) / 2
                                center_y = (y2 + y1) / 2
                                _a = center_x - pred_x  # 선 a의 길이
                                _b = center_y - pred_y  # 선 b의 길이

                                # _a = x1 - predict_coordinate[0]  # 선 a의 길이
                                # _b = y1 - predict_coordinate[1]  # 선 b의 길이
                                pred_coord_dist = math.sqrt((_a * _a) +
                                                            (_b * _b))

                                # 차량 같은 경우 테두리를 아예 벗어나면 드론이 직접 돌리지 않는 이상 되돌아오지 않는다.
                                max_bbox_size = before_object.max_bbox_size
                                half_w = max_bbox_size[0] / 2
                                half_h = max_bbox_size[1] / 2

                                # pred 오차를 생각하여 100 px 이 벗어나면 다시 보이는 차들은 새로운 차임
                                line_limit = 100
                                if pred_x < 0 and pred_x + half_w <= 0 - line_limit:
                                    is_out_line = True
                                elif pred_x > img_size[
                                        0] and pred_x - half_w >= img_size[
                                            0] + line_limit:
                                    is_out_line = True
                                elif pred_y < 0 and pred_y + half_h <= 0 - line_limit:
                                    is_out_line = True
                                elif pred_y > img_size[
                                        1] and pred_y - half_h >= img_size[
                                            1] + line_limit:
                                    is_out_line = True

                            # 240 원래
                            limit_coord_dist = 230

                            if dist_frame != 1:
                                for num in range(1, dist_frame + 1):
                                    limit_coord_dist += (limit_coord_dist *
                                                         np.exp2(-num))

                            limit_coord_dist = min(500.0, limit_coord_dist)

                            edge_dist = 8
                            if x1 < edge_dist or y1 < edge_dist or \
                                x2 > img_size[0] - edge_dist or y2 > img_size[1] - edge_dist and \
                                    (img_size[1] - 100) > pred_coord_dist: # 왜냐하면 왼쪽테두리로 나갔는데 오른쪽 테두리에 나왔을 때를 가정:
                                limit_coord_dist *= 1.3

                            # bbox가 최대치 일때 비교와 이전 프레임의 feature 비교를 한다.
                            # 넓이는 최대 25*25 = 625 px 까지는 여유로 둔다.
                            max_dist_feat = 1000.0
                            if max((before_object.area - 625),
                                   0) <= (x2 - x1) * (y2 - y1):
                                max_dist_feat = feature - before_object.max_bbox_feature
                                max_dist_feat = np.dot(max_dist_feat,
                                                       max_dist_feat)

                            # dist_feat = feature - before_object.feature
                            # dist_feat = np.dot(dist_feat, dist_feat)
                            _dist_feat = feature - before_object.feature
                            _dist_feat = np.dot(_dist_feat, _dist_feat)

                            dist_feat_list = [_dist_feat]
                            for _index, before_freature in enumerate(
                                    before_object.feature_list):
                                _dist_feat = feature - before_freature
                                _dist_feat = np.dot(_dist_feat, _dist_feat)

                                # 임시로.. 오래된 이미지는 피쳐 신뢰도가 높을 수가 있으므로 약간 가중치를 준다.
                                _dist_feat = _dist_feat * np.exp(-_index)
                                dist_feat_list.append(_dist_feat)

                            dist_feat = min(dist_feat_list)

                            # 일단은 주위 100px 사이로만 검색 한다. (드론 이동이 있음) // 15 프레임까지는 거리 비교를 해본다.
                            # 또한 너무 비슷 하다면 기회를(?) 줘본다.
                            # print((x1, y1), "//", (b_x1, b_y1), "//", before_object.id, "//", dist_feat, max_dist_feat)
                            # print(dist_feat, max_dist_feat, dist_frame, "//", before_object.id, "//", pred_coord_dist)
                            # dist_px = dist_px if dist_frame < 10 else 300
                            # if min(x1 + dist_px, img_size[0]) >= b_x1 and max(x1 - dist_px, 0) <= b_x1 and \
                            #         min(y1 + dist_px, img_size[1]) >= b_y1 and max(y1 - dist_px, 0) <= b_y1  or \
                            #         (pred_coord_dist < limit_coord_dist and dist_frame < 30):

                            # 원래 feat 5, 3 이였음 지금 너무 피팅되서 숫자가 작게 나옴
                            if (dist_frame <= 4 and min(x1 + limit_coord_dist, img_size[0]) >= b_x1 and max(x1 - limit_coord_dist, 0) <= b_x1 and \
                                    min(y1 + limit_coord_dist, img_size[1]) >= b_y1 and max(y1 - limit_coord_dist, 0) <= b_y1)  or \
                                    pred_coord_dist < limit_coord_dist and not (is_out_line and dist_frame > 5):
                                # boundary 안에 있더라도 너무 안닮았으면 패스 한다.
                                # 원래 180
                                if dist_feat < 170:
                                    more_before_dist.append([
                                        dist_feat * pred_coord_dist,
                                        before_frame_index, object_index
                                    ])
                            elif dist_feat < 6:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                            elif max_dist_feat < 4:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])

                if len(more_before_dist) == 0:
                    # print("??", i)
                    objects_counter[label] += 1

                    # label, feature, bbox, score
                    object_info = ObjectInfo(label, feature, bbox, score,
                                             objects_counter[label])
                    frame_dict[i][label].append(object_info)
                else:
                    more_before_dist = np.array(more_before_dist)
                    min_index = np.argmin(more_before_dist[:, 0])
                    choice_frame = int(more_before_dist[min_index][1])
                    choice_index = int(more_before_dist[min_index][2])

                    more_before_objects = frame_dict[choice_frame][label]

                    similarity_object = more_before_objects[choice_index]
                    similarity_object.update(feature, bbox, score)

                    frame_dict[i][label].append(similarity_object)

                    del more_before_objects[choice_index]

        # 선택 받지 못한 예전 frame 들의 objects 들의 움직을 예측하여 업데이트 한다.
        # 다만 30 프레임을 벗어난 아이들은 갱신하지 않는다.
        for before_frame_index in range(max(i - 30, 0), i):
            more_before_objects = frame_dict[before_frame_index]
            more_before_label_objects = []
            for label_index in range(6):
                more_before_label_objects.extend(
                    more_before_objects[label_index])

            for before_object in more_before_label_objects:
                before_object.predict_bbox_update()

        predictions['info'] = frame_dict[i]
        predictions['bboxes'] = _bboxes
        predictions['scores'] = _scores
        predictions['labels'] = _labels

        print(objects_counter)

        if is_visualize:
            visualize(os.path.join(dir_path, filename),
                      mmcv.imread(file_path),
                      predictions,
                      colors=colors,
                      mask_display=False,
                      class_names=class_names)
Esempio n. 4
0
    def _process_result(i, result):
        # mask가 없을 때만 1번이다. mask 학습이었다면 2번이어야 한다... 지금은 귀찮음
        bb_result, img_feats = result

        class_names = get_classes(dataset)
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bb_result)
        ]
        labels = np.concatenate(labels)
        bboxes = np.vstack(bb_result)

        assert bboxes.ndim == 2
        assert labels.ndim == 1
        assert bboxes.shape[0] == labels.shape[0]
        assert bboxes.shape[1] == 4 or bboxes.shape[1] == 5

        assert bboxes.shape[1] == 5
        scores = bboxes[:, -1]
        bboxes = bboxes[:, :-1]

        # print(i, scores, labels)
        # if i == 23:
        #     exit()

        _scores = list()
        _bboxes = list()
        _labels = list()
        # CLASS = {"사람": 0, "소화기":1, "소화전":2, "차량":3, "자전거":4, "오토바이":5}
        if i == 0:
            for now_label_index, label in enumerate(labels):
                limit_score = checked_score(label)
                score = scores[now_label_index]
                if score < limit_score:
                    continue

                feature = img_feats[now_label_index]
                bbox = bboxes[now_label_index]

                # label, feature, bbox, score, id
                object_info = ObjectInfo(label, feature, bbox, score,
                                         objects_counter[label])
                frame_dict[i][label].append(object_info)
                objects_counter[label] += 1

                _scores.append(score)
                _labels.append(label)
                _bboxes.append(bbox)
        else:
            for now_label_index, label in enumerate(labels):
                # temp_dist = list()
                limit_score = checked_score(label)
                score = scores[now_label_index]
                if score < limit_score:
                    continue

                feature = img_feats[now_label_index]
                bbox = bboxes[now_label_index]

                _scores.append(score)
                _labels.append(label)
                _bboxes.append(bbox)

                # 같은 라벨에 대해서 전부다 하는 것이 아닌 bbox가 가까운 아이들만 검사한다.
                # 좌표 가져오기
                x1, y1, x2, y2 = bbox

                more_before_dist = list()
                # 사람, 소화기, 소화전, 자전거 // 자동차,오토바이 따로 구분해서 처리
                if label in [0, 1, 2, 4]:
                    # 원래는 검사하는 프레임을 i-20으로 20프레임 전만 검사하지만 현재는 테스트 중이므로 50 프레임을 검사한다.
                    # max(i - 20, 0)
                    for before_frame_index in range(max(i - 50, 0), i):
                        more_before_objects = frame_dict[before_frame_index]

                        more_before_label_objects = more_before_objects[label]

                        # debug 용
                        # if len(more_before_label_objects)>0:
                        #     print("index:", before_frame_index, "label:", label)

                        for object_index, before_object in enumerate(
                                more_before_label_objects):
                            # 엇나갈 좌표의 범위는 사람 기준 dist_frame*50.0 px로 둔다.
                            # CLASS = {"사람": 0, "소화기":1, "소화전":2, "차량":3, "자전거":4, "오토바이":5}
                            # 소화기 소화전은 드론 속도만을 고려 하여 프레임당 30px 이동으로 준다.
                            dist_frame = i - before_frame_index
                            if label == 0:
                                dist_px = min(650.0, dist_frame * 120.0)
                            elif label == 1:
                                dist_px = min(650.0, dist_frame * 120.0)
                            elif label == 2:
                                dist_px = min(650.0, dist_frame * 120.0)
                            elif label == 3:
                                dist_px = min(650.0, dist_frame * 200.0)
                            elif label == 4:
                                dist_px = min(650.0, dist_frame * 120.0)
                            else:
                                dist_px = min(650.0, dist_frame * 200.0)

                            b_x1, b_y1, b_x2, b_y2 = before_object.bbox

                            # a = x1 - b_x1  # 선 a의 길이
                            # b = y1 - b_y1  # 선 b의 길이

                            # print((x1, y1), "//", (b_x1, b_y1), "//", before_object.id)
                            # 테스트용 코드 작성
                            pred_coord_dist = 1000.0
                            predict_coordinate = before_object.get_predict_bbox(
                            )
                            if len(predict_coordinate) > 0:
                                _a = x1 - predict_coordinate[0]  # 선 a의 길이
                                _b = y1 - predict_coordinate[1]  # 선 b의 길이
                                pred_coord_dist = math.sqrt((_a * _a) +
                                                            (_b * _b))

                            # bbox가 최대치 일때 비교와 이전 프레임의 feature 비교를 한다.
                            # 넓이는 최대 8*8 = 64 px 까지는 여유로 둔다.
                            max_dist_feat = 1000.0
                            if max((before_object.area - 64),
                                   0) <= (x2 - x1) * (y2 - y1):
                                max_dist_feat = feature - before_object.max_bbox_feature
                                max_dist_feat = np.dot(max_dist_feat,
                                                       max_dist_feat)

                            _dist_feat = feature - before_object.feature
                            _dist_feat = np.dot(_dist_feat, _dist_feat)

                            dist_feat_list = [_dist_feat]
                            for before_freature in before_object.feature_list:
                                _dist_feat = feature - before_freature
                                _dist_feat = np.dot(_dist_feat, _dist_feat)

                                dist_feat_list.append(_dist_feat)

                            dist_feat = min(dist_feat_list)

                            # 일단은 주위 100px 사이로만 검색 한다. (드론 이동이 있음) // 15 프레임까지는 거리 비교를 해본다.
                            # 또한 너무 비슷 하다면 기회를(?) 줘본다.
                            # print(dist_feat, max_dist_feat, dist_frame, "//", before_object.id, "//", pred_coord_dist)
                            dist_px = dist_px if dist_frame < 15 else 100
                            if min(x1+dist_px,1920) >= b_x1 and max(x1-dist_px,0) <= b_x1 and \
                                    min(y1 + dist_px, 1080) >= b_y1 and max(y1 - dist_px, 0) <= b_y1 or \
                                    (pred_coord_dist < 150 and dist_frame < 30):
                                # boundary 안에 있더라도 너무 안닮았으면 패스 한다.
                                if dist_feat < 200:
                                    more_before_dist.append([
                                        dist_feat * pred_coord_dist,
                                        before_frame_index, object_index
                                    ])
                            elif dist_feat < 12:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                            elif max_dist_feat < 8:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                else:
                    # 원본 max(i - 5, 0) 자동차 15프레임이였음 (10프레임이 현재는 가장 스코어가 좋음)
                    for before_frame_index in range(max(i - 30, 0), i):
                        # print("index:", before_frame_index)
                        more_before_objects = frame_dict[before_frame_index]

                        more_before_label_objects = more_before_objects[label]
                        for object_index, before_object in enumerate(
                                more_before_label_objects):
                            b_x1, b_y1 = before_object.bbox[:2]

                            # a = x1 - b_x1  # 선 a의 길이
                            # b = y1 - b_y1  # 선 b의 길이
                            # coord_dist = math.sqrt((a * a) + (b * b))

                            dist_frame = i - before_frame_index
                            if label == 3:
                                dist_px = min(650.0, dist_frame * 210.0)
                            else:
                                dist_px = min(650.0, dist_frame * 210.0)

                            # 테스트용 코드 작성
                            pred_coord_dist = 1000.0
                            predict_coordinate = before_object.get_predict_bbox(
                            )
                            if len(predict_coordinate) > 0:
                                _a = x1 - predict_coordinate[0]  # 선 a의 길이
                                _b = y1 - predict_coordinate[1]  # 선 b의 길이
                                pred_coord_dist = math.sqrt((_a * _a) +
                                                            (_b * _b))

                            # bbox가 최대치 일때 비교와 이전 프레임의 feature 비교를 한다.
                            # 넓이는 최대 25*25 = 625 px 까지는 여유로 둔다.
                            max_dist_feat = 1000.0
                            if max((before_object.area - 625),
                                   0) <= (x2 - x1) * (y2 - y1):
                                max_dist_feat = feature - before_object.max_bbox_feature
                                max_dist_feat = np.dot(max_dist_feat,
                                                       max_dist_feat)

                            # dist_feat = feature - before_object.feature
                            # dist_feat = np.dot(dist_feat, dist_feat)
                            _dist_feat = feature - before_object.feature
                            _dist_feat = np.dot(_dist_feat, _dist_feat)

                            dist_feat_list = [_dist_feat]
                            for before_freature in before_object.feature_list:
                                _dist_feat = feature - before_freature
                                _dist_feat = np.dot(_dist_feat, _dist_feat)

                                dist_feat_list.append(_dist_feat)

                            dist_feat = min(dist_feat_list)

                            # 일단은 주위 100px 사이로만 검색 한다. (드론 이동이 있음) // 15 프레임까지는 거리 비교를 해본다.
                            # 또한 너무 비슷 하다면 기회를(?) 줘본다.
                            # print((x1, y1), "//", (b_x1, b_y1), "//", before_object.id, "//", dist_feat, max_dist_feat)
                            # print(dist_feat, max_dist_feat, dist_frame, "//", before_object.id, "//", pred_coord_dist)
                            dist_px = dist_px if dist_frame < 10 else 300
                            if min(x1 + dist_px, 1920) >= b_x1 and max(x1 - dist_px, 0) <= b_x1 and \
                                    min(y1 + dist_px, 1080) >= b_y1 and max(y1 - dist_px, 0) <= b_y1 or \
                                    (pred_coord_dist < 300 and dist_frame < 30):
                                # boundary 안에 있더라도 너무 안닮았으면 패스 한다.
                                if dist_feat < 180:
                                    more_before_dist.append([
                                        dist_feat * pred_coord_dist,
                                        before_frame_index, object_index
                                    ])
                            elif dist_feat < 6:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])
                            elif max_dist_feat < 3:
                                more_before_dist.append([
                                    dist_feat * pred_coord_dist,
                                    before_frame_index, object_index
                                ])

                if len(more_before_dist) == 0:
                    # print("??", i)
                    objects_counter[label] += 1

                    # label, feature, bbox, score
                    object_info = ObjectInfo(label, feature, bbox, score,
                                             objects_counter[label])
                    frame_dict[i][label].append(object_info)
                else:
                    more_before_dist = np.array(more_before_dist)
                    min_index = np.argmin(more_before_dist[:, 0])
                    choice_frame = int(more_before_dist[min_index][1])
                    choice_index = int(more_before_dist[min_index][2])

                    more_before_objects = frame_dict[choice_frame][label]

                    similarity_object = more_before_objects[choice_index]
                    similarity_object.update(feature, bbox, score)

                    frame_dict[i][label].append(similarity_object)

                    del more_before_objects[choice_index]

        # 선택 받지 못한 예전 frame 들의 objects 들의 움직을 예측하여 업데이트 한다.
        # 다만 30 프레임을 벗어난 아이들은 갱신하지 않는다.
        for before_frame_index in range(max(i - 30, 0), i):
            more_before_objects = frame_dict[before_frame_index]
            more_before_label_objects = []
            for label_index in range(6):
                more_before_label_objects.extend(
                    more_before_objects[label_index])

            for before_object in more_before_label_objects:
                before_object.predict_bbox_update()

        predictions['info'] = frame_dict[i]
        predictions['bboxes'] = _bboxes
        predictions['scores'] = _scores
        predictions['labels'] = _labels

        print(objects_counter)

        if is_visualize:
            visualize(os.path.join(dir_path, filename),
                      mmcv.imread(file_path),
                      predictions,
                      colors=colors,
                      mask_display=False,
                      class_names=class_names)
Esempio n. 5
0
dataset = 'kmu'
score_thr = 0.5
predictions = dict()

for i, result in enumerate(
        inference_detector(model, imgs, cfg, device='cuda:0')):
    print(i, imgs[i])

    # print(result)
    # mask 제외 시킨다 지금은
    result = result[0]

    file_path = imgs[i]
    filename = os.path.split(file_path)[-1]

    class_names = get_classes(dataset)
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(result)
    ]
    labels = np.concatenate(labels)
    bboxes = np.vstack(result)
    img = mmcv.imread(file_path)

    # if filename in img_info:
    #     gt_bboxes = img_info[filename]
    #
    #     for gt_bbox in gt_bboxes:
    #         img = cv2.rectangle(img, tuple(gt_bbox[:2]), tuple(gt_bbox[2:]), (255,0,0), 2)

    assert bboxes.ndim == 2