コード例 #1
0
def create(c):
    if not os.path.exists(image_folder) or not os.path.exists(
            detection_file_name):
        raise FileNotFoundError(
            'cannot find the image folder and the detection file')

    if not os.path.exists(args.log_folder):
        os.mkdir(args.log_folder)

    tracker = SSTTracker()
    reader = MOTDataReader(image_folder=image_folder,
                           detection_file_name=detection_file_name,
                           min_confidence=0.0)

    select_squences = [402, 404, 410, 422]

    frame_index = 0
    for i, item in enumerate(reader):
        if i not in select_squences:
            continue

        if i > len(reader):
            break

        if item is None:
            continue

        img = item[0]
        det = item[1]

        if img is None or det is None or len(det) == 0:
            continue

        if len(det) > config['max_object']:
            det = det[:config['max_object'], :]

        h, w, _ = img.shape

        det[:, [2, 4]] /= float(w)
        det[:, [3, 5]] /= float(h)

        image_org = tracker.update(img, det[:, 2:6], args.show_image,
                                   frame_index)
        frame_index += 1

        if args.show_image and not image_org is None:
            # det[:, [2, 4]] *= float(w)
            # det[:, [3, 5]] *= float(h)
            # boxes = det[:, 2:6].astype(int)
            # for bid, b in enumerate(boxes):
            #     image_org = cv2.putText(image_org, '{}'.format(bid), tuple(b[:2]), cv2.FONT_HERSHEY_SIMPLEX, 1,
            #                             (0, 0, 0), 2)
            cv2.imshow('res', image_org)
            cv2.imwrite(os.path.join(args.log_folder, '{0:06}.jpg'.format(i)),
                        image_org)
            # cv2.waitKey(0)
            print('frame: {}'.format(i))
コード例 #2
0
def test(choice=None):
    if args.type == 'train':
        dataset_index = [2, 4, 5, 9, 10, 11, 13]
        dataset_detection_type = {'-DPM', '-FRCNN', '-SDP'}

    if args.type == 'test':
        dataset_index = [1, 3, 6, 7, 8, 12, 14]
        dataset_detection_type = {'-FRCNN', '-SDP', '-DPM'}

    dataset_image_folder_format = os.path.join(
        args.mot_root,
        args.type + '/MOT' + str(args.mot_version) + '-{:02}{}/img1')
    detection_file_name_format = os.path.join(
        args.mot_root,
        args.type + '/MOT' + str(args.mot_version) + '-{:02}{}/det/det.txt')

    if not os.path.exists(args.log_folder):
        os.mkdir(args.log_folder)

    save_folder = ''
    choice_str = ''
    if not choice is None:
        choice_str = TrackerConfig.get_configure_str(choice)
        save_folder = os.path.join(args.log_folder, choice_str)
        if not os.path.exists(save_folder):
            os.mkdir(save_folder)
        # else:
        #     return

    saved_file_name_format = os.path.join(
        save_folder, 'MOT' + str(args.mot_version) + '-{:02}{}.txt')
    save_video_name_format = os.path.join(
        save_folder, 'MOT' + str(args.mot_version) + '-{:02}{}.avi')

    f = lambda format_str: [
        format_str.format(index, type) for type in dataset_detection_type
        for index in dataset_index
    ]

    timer = Timer()
    for image_folder, detection_file_name, saved_file_name, save_video_name in zip(
            f(dataset_image_folder_format), f(detection_file_name_format),
            f(saved_file_name_format), f(save_video_name_format)):
        print('start processing ' + saved_file_name)
        tracker = SSTTracker()
        reader = MOTDataReader(image_folder=image_folder,
                               detection_file_name=detection_file_name,
                               min_confidence=0.0)
        result = list()
        result_str = saved_file_name
        first_run = True
        for i, item in enumerate(reader):
            if i > len(reader):
                break

            if item is None:
                continue

            img = item[0]
            det = item[1]

            if img is None or det is None or len(det) == 0:
                continue

            if len(det) > config['max_object']:
                det = det[:config['max_object'], :]

            h, w, _ = img.shape

            if first_run and args.save_video:
                vw = cv2.VideoWriter(
                    save_video_name,
                    cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))
                first_run = False

            det[:, [2, 4]] /= float(w)
            det[:, [3, 5]] /= float(h)
            timer.tic()
            image_org = tracker.update(img, det[:, 2:6], args.show_image, i)
            timer.toc()
            print('{}:{}, {}, {}\r'.format(os.path.basename(saved_file_name),
                                           i, int(i * 100 / len(reader)),
                                           choice_str))
            #if args.show_image and not image_org is None:
            #plt.imshow('res', image_org)
            #cv2.waitKey(1)

            if args.save_video and not image_org is None:
                vw.write(image_org)

            # save result
            for t in tracker.tracks:
                n = t.nodes[-1]
                if t.age == 1:
                    b = n.get_box(tracker.frame_index - 1, tracker.recorder)
                    result.append([i] + [t.id] +
                                  [b[0] * w, b[1] * h, b[2] * w, b[3] * h] +
                                  [-1, -1, -1, -1])
        # save data
        np.savetxt(saved_file_name, np.array(result).astype(int), fmt='%i')
        print(result_str)

    print(timer.total_time)
    print(timer.average_time)
コード例 #3
0
def test(choice=None):
    video_name_list = config['video_name_list']

    if not os.path.exists(args.log_folder):
        os.mkdir(args.log_folder)

    save_folder = ''
    choice_str = ''
    if not choice is None:
        choice_str = TrackerConfig.get_configure_str(choice)
        save_folder = os.path.join(args.log_folder, choice_str)
        if not os.path.exists(save_folder):
            os.mkdir(save_folder)

    save_file_name_format = os.path.join(save_folder, '{}.txt')
    save_video_name_format = os.path.join(save_folder, '{}.avi')
    timer = Timer()

    for video_name in video_name_list:
        if video_name == 'AVG-TownCentre':
            TrackerConfig.set_configure((4, 0, 4, 4, 5, 4))
        else:
            TrackerConfig.set_configure(choice)

        mot_root = os.path.join(config['mot_root'], config['dataset_type'])
        mot_root = os.path.join(mot_root, video_name)
        image_folder = os.path.join(mot_root, 'img1')
        detection_file_name = os.path.join(mot_root, 'det/det.txt')
        save_video_name = save_video_name_format.format(video_name)
        save_file_name = save_file_name_format.format(video_name)
        reader = MOTDataReader(image_folder=image_folder,
                               detection_file_name=detection_file_name,
                               min_confidence=None)
        tracker = SSTTracker()

        result = list()
        result_str = save_file_name

        force_init = True
        for i, item in enumerate(reader):
            if i > len(reader):
                break

            if item is None:
                continue

            img = item[0]
            det = item[1]

            if img is None or det is None or len(det) == 0:
                continue

            if len(det) > config['max_object']:
                det = det[:config['max_object'], :]

            h, w, _ = img.shape

            if i == 1 and args.save_video:
                vw = cv2.VideoWriter(
                    save_video_name,
                    cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))
            det = det.astype(float)
            det[:, [2, 4]] /= float(w)
            det[:, [3, 5]] /= float(h)
            timer.tic()
            image_org = tracker.update(img, det[:, 2:6], args.show_image, i,
                                       force_init)
            force_init = False
            timer.toc()
            print('{}:{}, {}, {}\r'.format(os.path.basename(save_file_name), i,
                                           int(i * 100 / len(reader)),
                                           choice_str))
            if args.show_image and not image_org is None:
                cv2.imshow('res', image_org)
                cv2.waitKey(1)

            if args.save_video and not image_org is None:
                try:
                    vw.write(image_org)
                except:
                    pass

            # save result
            for t in tracker.tracks:
                n = t.nodes[-1]
                if t.age == 1:
                    b = n.get_box(tracker.frame_index - 1, tracker.recorder)
                    result.append([i + 1] + [t.id + 1] +
                                  [b[0] * w, b[1] * h, b[2] * w, b[3] * h] +
                                  [-1, -1, -1, -1])
        # save data
        np.savetxt(save_file_name, np.array(result).astype(int), fmt='%i')
        print(result_str)

    print(timer.total_time)
    print(timer.average_time)
コード例 #4
0
def test():
    if args.type == 'train':
        dataset_index = [2, 4, 5, 9, 10, 11, 13]
        # dataset_index = [10]
        dataset_detection_type = {'-DPM', '-FRCNN', '-SDP'}
        dataset_detection_type = {'-FRCNN'}

    if args.type == 'test':
        # dataset_index = [1, 3, 6, 7, 8, 12, 14]
        dataset_index = [3]
        dataset_detection_type = {'-DPM', '-FRCNN', '-SDP'}
        dataset_detection_type = {'-FRCNN'}

    dataset_detection_type = {''}
    dataset_image_folder_format = os.path.join(
        args.mot_root,
        args.type + '/MOT' + str(args.mot_version) + '-{:02}{}/img1')
    detection_file_name_format = os.path.join(
        args.mot_root,
        args.type + '/MOT' + str(args.mot_version) + '-{:02}{}/det/det.txt')
    saved_file_name_format = 'MOT' + str(args.mot_version) + '-{:02}{}.txt'
    save_video_name_format = 'MOT' + str(args.mot_version) + '-{:02}{}.avi'

    f = lambda format_str: [
        format_str.format(index, type) for type in dataset_detection_type
        for index in dataset_index
    ]

    timer = Timer()
    for image_folder, detection_file_name, saved_file_name, save_video_name in zip(
            f(dataset_image_folder_format), f(detection_file_name_format),
            f(saved_file_name_format), f(save_video_name_format)):
        print('start processing ' + saved_file_name)
        tracker = SSTTracker()
        reader = MOTDataReader(image_folder=image_folder,
                               detection_file_name=detection_file_name)
        i = 0
        result = list()
        result_str = saved_file_name

        for item in reader:
            i += 1
            if i > len(reader):
                break

            if item is None:
                continue

            img = item[0]
            det = item[1]

            if img is None or det is None or len(det) == 0:
                continue

            if len(det) > config['max_object']:
                det = det[:config['max_object'], :]

            h, w, _ = img.shape

            if i == 1 and args.save_video:
                vw = cv2.VideoWriter(
                    save_video_name,
                    cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (w, h))

            det[:, [2, 4]] /= float(w)
            det[:, [3, 5]] /= float(h)
            timer.tic()
            image_org = tracker.update(img, det[:, 2:6], args.show_image)
            timer.toc()
            print('{}:{}, {}\r'.format(saved_file_name, i,
                                       int(i * 100 / len(reader))))
            if args.show_image and not image_org is None:
                cv2.imshow('res', image_org)
                cv2.waitKey(10)

            if args.save_video and not image_org is None:
                vw.write(image_org)

            # save result
            # for t in tracker.tracks:
            #     n = t.nodes[-1]
            #     if t.age == 1:
            #         b = n.box
            #         result.append(
            #             [i] + [t.id] + [b[0]*w, b[1]*h, b[2]*w, b[3]*h] + [-1, -1, -1, -1]
            #         )
        # save data
        np.savetxt(saved_file_name, np.array(result).astype(int), fmt='%i')
        print(result_str)

    print(timer.total_time)
    print(timer.average_time)