Example #1
0
def plot_frame_bboxes(cam_name, frame_id):
    gt_detection = reid_hash_map[cam_name]
    yolo_detection = time_to_baseline_bbox(cam_name)

    gt_rects = gt_detection[frame_id]
    ab_rects, single_rects = [], []
    for yolo_rect in yolo_detection[frame_id]:
        max_iou = 0
        for gt_rect in gt_rects:
            max_iou = max(max_iou, general.IoU(gt_rect, yolo_rect))
        if max_iou == 0:
            single_rects.append(yolo_rect)
        elif max_iou < 0.2:
            ab_rects.append(yolo_rect)

    if not (len(ab_rects) >= 1 and len(single_rects) >= 3): return

    base_frame = general.get_frame(cam_name, frame_id - 1)
    for rect in gt_rects:
        left, top, width, height = rect
        cv2.rectangle(base_frame, (left, top), (left + width, top + height),
                      (255, 0, 0), 3)
    for rect in ab_rects:
        left, top, width, height = [int(each) for each in rect]
        cv2.rectangle(base_frame, (left, top), (left + width, top + height),
                      (0, 255, 0), 3)
    for rect in single_rects:
        left, top, width, height = [int(each) for each in rect]
        cv2.rectangle(base_frame, (left, top), (left + width, top + height),
                      (0, 0, 255), 3)

    print(frame_id)
    cv2.imshow('image', base_frame)
    cv2.waitKey()
    cv2.destroyAllWindows()
Example #2
0
def time_to_track_bbox_obj(cam_name):
    result = {}

    track_id_time_to_bbox = {}
    for line in open(WORKSAPCE + DATA_PATH + cam_name + '/' + TRACK_PATH,
                     'r').readlines():
        frame_id, obj_id, left, top, width, height, _, _, _, _ = [
            round(float(each)) for each in line.split(',')
        ]
        if obj_id not in track_id_time_to_bbox:
            track_id_time_to_bbox[obj_id] = {
                frame_id: (left, top, width, height)
            }
        else:
            track_id_time_to_bbox[obj_id][frame_id] = (left, top, width,
                                                       height)

    total_obj = len(track_id_time_to_bbox)
    re_id_obj_count = 0

    gt_time_to_bbox_obj = time_to_gt_bbox_obj(cam_name)
    for obj_id in track_id_time_to_bbox:
        reid_candidates = {}
        for frame_id in track_id_time_to_bbox[obj_id]:
            if frame_id not in gt_time_to_bbox_obj:
                continue
            for gt_bbox_obj in gt_time_to_bbox_obj[frame_id]:
                track_rect = track_id_time_to_bbox[obj_id][frame_id]
                gt_bbox, gt_obj = gt_bbox_obj[:4], gt_bbox_obj[-1]
                IoU_score = general.IoU(track_rect, gt_bbox)
                if IoU_score > 0.3:
                    if gt_obj not in reid_candidates:
                        reid_candidates[gt_obj] = IoU_score
                    else:
                        reid_candidates[gt_obj] += IoU_score

        if len(reid_candidates) == 0:
            new_obj_id = -1
        else:
            new_obj_id, obj_score = max(reid_candidates,
                                        key=reid_candidates.get), max(
                                            reid_candidates.keys())
            if obj_score < 0.8:
                new_obj_id = -1
            else:
                re_id_obj_count += 1

        for frame_id in track_id_time_to_bbox[obj_id]:
            bbox = track_id_time_to_bbox[obj_id][frame_id]
            if frame_id not in result:
                result[frame_id] = [bbox + (new_obj_id, )]
            else:
                result[frame_id].append(bbox + (new_obj_id, ))

    print(cam_name, total_obj, re_id_obj_count)
    return result
Example #3
0
def assign_id(id_dict, target_dict):
    time = target_dict.keys()
    for t in time:
        if t not in id_dict:
            bboxes = target_dict[t]
            id_bboxes = [each + (-1, ) for each in bboxes]
            target_dict[t] = id_bboxes
            continue
        for idx, target_rect in enumerate(target_dict[t]):
            best_fit_id, IoU_score = -1, 0
            for gt_rect_id in id_dict[t]:
                gt_rect, id = gt_rect_id[:4], gt_rect_id[-1]
                IoU_tmp = general.IoU(target_rect, gt_rect)
                if IoU_tmp < 0.1: continue
                if IoU_tmp > IoU_score:
                    IoU_score = IoU_tmp
                    best_fit_id = id
            target_dict[t][idx] = target_dict[t][idx] + (best_fit_id, )
    return target_dict
Example #4
0
def get_SVM_HashMap(gamma=2e-3, res_thres=2.0):

    reid_hash_map = RegressionFilter.get_Regression_Hashmap(
        res_thres=res_thres)
    exist_max_oid = -1

    for cam_name in reid_hash_map:
        for _, obj in reid_hash_map[cam_name]:
            exist_max_oid = max(exist_max_oid, obj)

    # Logic to add some extra bbox from yolo detection

    for cam_name in cameras:
        time_to_bbox = {}
        for frame_id, oid in reid_hash_map[cam_name]:
            if frame_id not in time_to_bbox:
                time_to_bbox[frame_id] = [
                    reid_hash_map[cam_name][(frame_id, oid)]
                ]
            else:
                time_to_bbox[frame_id].append(
                    reid_hash_map[cam_name][(frame_id, oid)])

        for line in open(WORKSAPCE + DATA_PATH + cam_name + '/' +
                         DET_PATH).readlines():
            frame_id, _, left, top, width, height, confidence, _, _, _ = [
                float(each) for each in line.split(',')
            ]
            if confidence < 0.05: continue
            if left < 50 or top < 50 or left + width + 50 > cameras_shape[cam_name][1] or \
                top + height + 50 > cameras_shape[cam_name][0]:
                continue
            frame_id, left, top, width, height = round(frame_id), round(
                left), round(top), round(width), round(height)

            add_this = True
            if frame_id in time_to_bbox:
                for exist_bbox in time_to_bbox[frame_id]:
                    if general.IoU(exist_bbox,
                                   (left, top, width, height)) > 0.1:
                        add_this = False
                        break
            if add_this:
                exist_max_oid += 1
                reid_hash_map[cam_name][(frame_id,
                                         exist_max_oid)] = (left, top, width,
                                                            height)

    outlier_dict = {cam: [] for cam in cameras}
    unique_dict = {cam: [] for cam in cameras}

    print("SVM function gamma input ", gamma)

    for source_cam in cameras:
        source_unique = set(reid_hash_map[source_cam].keys())
        source_outlier = set()
        for destination_cam in cameras:
            if source_cam == destination_cam:
                continue
            svm_data, svm_label, data_id = prepare_svm_data(
                reid_hash_map, source_cam, destination_cam, [0, 900])
            false_pos = list(np.where(svm_label == 0)[0])
            source_unique = source_unique.intersection(
                set([data_id[i] for i in false_pos]))

            clf = svm.SVC(kernel='rbf', class_weight='balanced',
                          gamma=gamma)  # change to 2e-5 in overall setup
            clf.fit(svm_data, svm_label)
            y_pred = clf.predict(svm_data)

            false_negative_pos = list(np.where((svm_label - y_pred) == -1)[0])
            if len(false_negative_pos) == 0: continue
            tmp_outlier = set([data_id[i] for i in false_negative_pos])
            source_outlier = source_outlier.union(tmp_outlier)
        outlier_dict[source_cam] = source_unique.intersection(source_outlier)
        unique_dict[source_cam] = source_unique

    outlier_num = 0

    Multi_Hashmap = {}
    for cam in cameras:
        for key in outlier_dict[cam]:
            del reid_hash_map[cam][key]
            outlier_num += 1
        Multi_Hashmap[cam] = reid_hash_map[cam]

    print('SVM Outliers Number', outlier_num)

    return Multi_Hashmap, reid_hash_map, outlier_dict, unique_dict