コード例 #1
0
    def _process_dir(self, dir_path, json_path, relabel):
        if osp.exists(json_path):
            print("=> {} generated before, awesome!".format(json_path))
            split = read_json(json_path)
            return split['tracklets']

        print(
            "=> Automatically generating split (might take a while for the first time, have a coffe)"
        )
        pdirs = sorted(glob.glob(osp.join(dir_path, '*')))  # avoid .DS_Store
        print("Processing '{}' with {} person identities".format(
            dir_path, len(pdirs)))

        pid_container = set()
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            pid_container.add(pid)
        pid2label = {pid: label for label, pid in enumerate(pid_container)}

        cam_list = []
        tracklets = []
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            if relabel: pid = pid2label[pid]
            tdirs = sorted(glob.glob(osp.join(pdir, '*')))

            for tdir in tdirs:
                tid = int(osp.basename(tdir)) - 1
                raw_img_paths = sorted(glob.glob(osp.join(tdir, '*.jpg')))
                num_imgs = len(raw_img_paths)

                if num_imgs < self.min_seq_len:
                    continue

                img_paths = []
                for img_idx in range(num_imgs):
                    # some tracklet starts from 0002 instead of 0001
                    img_idx_name = 'F' + str(img_idx + 1).zfill(4)
                    res = sorted(
                        glob.glob(osp.join(tdir,
                                           '*' + img_idx_name + '*.jpg')))
                    if len(res) == 0:
                        print(
                            "Warn: index name {} in {} is missing, jump to next"
                            .format(img_idx_name, tdir))
                        continue
                    img_paths.append(res[0])
                img_name = osp.basename(img_paths[0])
                if img_name.find('_') == -1:
                    # old naming format: 0001C6F0099X30823.jpg
                    camid = int(img_name[5]) - 1
                else:
                    # new naming format: 0001_C6_F0099_X30823.jpg
                    camid = int(img_name[6]) - 1
                img_paths = tuple(img_paths)
                tid_sub = -1
                pid_sub = -1
                tracklets.append(
                    (img_paths, tid, pid, tid_sub, pid_sub, camid))
                cam_list.append(camid)

        num_cams = len(list(set(cam_list)))
        start_tid_uic = 0
        for cam_index in range(num_cams):
            # count tid per camera
            tkl_index_list = [
                index for index, (_, _, _, _, _, camid) in enumerate(tracklets)
                if camid == cam_index
            ]
            # count pid per camera
            pid_list_sub = [tracklets[j][2] for j in tkl_index_list]
            unique_pid_list_percam = list(set(pid_list_sub))
            start_tid_uic += len(unique_pid_list_percam)

            pid_percam2label = {
                pid: label
                for label, pid in enumerate(unique_pid_list_percam)
            }

            for index, tkl_index in enumerate(tkl_index_list):
                img_paths = tracklets[tkl_index][0]
                tid = tracklets[tkl_index][1]
                pid = tracklets[tkl_index][2]
                tid_sub = index
                pid_sub = pid_percam2label[pid]
                camid = tracklets[tkl_index][5]
                tracklets[tkl_index] = (img_paths, tid, pid, tid_sub, pid_sub,
                                        camid)

        print("Saving split to {}".format(json_path))
        split_dict = {
            'tracklets': tracklets,
        }
        write_json(split_dict, json_path)
        return tracklets
    def _process_test_dir(self, dir_path, json_path, relabel):
        if osp.exists(json_path):
            print("=> {} generated before, awesome!".format(json_path))
            split = read_json(json_path)
            return split['tracklets']

        print(
            "=> Automatically generating split (might take a while for the first time, have a coffe)"
        )
        pdirs = sorted(glob.glob(osp.join(dir_path, '*')))  # avoid .DS_Store
        print("Processing '{}' with {} person identities".format(
            dir_path, len(pdirs)))

        pid_container = set()
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            pid_container.add(pid)
        pid2label = {pid: label for label, pid in enumerate(pid_container)}

        cam_list = []
        tracklets = []
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            if relabel: pid = pid2label[pid]
            tdirs = sorted(glob.glob(osp.join(pdir, '*')))

            for tdir in tdirs:
                tid = int(osp.basename(tdir)[3:])
                raw_img_paths = sorted(glob.glob(osp.join(tdir, '*.jpg')))
                num_imgs = len(raw_img_paths)
                if num_imgs < self.min_seq_len:
                    continue

                img_paths = sorted(glob.glob(osp.join(tdir, '*.jpg')))
                img_name = osp.basename(img_paths[0])
                # naming format: c6_030823.jpg
                camid = int(img_name[1]) - 1
                img_paths = tuple(img_paths)
                tid_sub = -1
                pid_sub = -1
                tracklets.append(
                    (img_paths, tid, pid, tid_sub, pid_sub, camid))
                cam_list.append(camid)

        num_cams = len(list(set(cam_list)))
        start_tid_uic = 0
        for cam_index in range(num_cams):
            # count tid per camera
            tkl_index_list = [
                index for index, (_, _, _, _, _, camid) in enumerate(tracklets)
                if camid == cam_index
            ]
            # count pid per camera
            pid_list_sub = [tracklets[j][2] for j in tkl_index_list]
            unique_pid_list_percam = list(set(pid_list_sub))
            start_tid_uic += len(unique_pid_list_percam)

            pid_percam2label = {
                pid: label
                for label, pid in enumerate(unique_pid_list_percam)
            }

            for index, tkl_index in enumerate(tkl_index_list):
                img_paths = tracklets[tkl_index][0]
                tid = tracklets[tkl_index][1]
                pid = tracklets[tkl_index][2]
                tid_sub = index
                pid_sub = pid_percam2label[pid]
                camid = tracklets[tkl_index][5]
                tracklets[tkl_index] = (img_paths, tid, pid, tid_sub, pid_sub,
                                        camid)

        print("Saving split to {}".format(json_path))
        split_dict = {
            'tracklets': tracklets,
        }
        write_json(split_dict, json_path)

        return tracklets
コード例 #3
0
    def _process_dir(self, dir_path, json_path, relabel):
        if osp.exists(json_path):
            print("=> {} generated before, awesome!".format(json_path))
            split = read_json(json_path)
            return split['tracklets'], split['num_tracklets'], split[
                'num_pids'], split['num_imgs_per_tracklet']

        print(
            "=> Automatically generating split (might take a while for the first time)"
        )
        pdirs = glob.glob(osp.join(dir_path, '*'))  # avoid .DS_Store
        print("Processing {} with {} person identities".format(
            dir_path, len(pdirs)))

        pid_container = set()
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            pid_container.add(pid)
        pid2label = {pid: label for label, pid in enumerate(pid_container)}

        tracklets = []
        num_imgs_per_tracklet = []
        for pdir in pdirs:
            pid = int(osp.basename(pdir))
            if relabel: pid = pid2label[pid]
            tdirs = glob.glob(osp.join(pdir, '*'))
            for tdir in tdirs:
                raw_img_paths = sorted(glob.glob(osp.join(tdir, '*.jpg')))
                num_imgs = len(raw_img_paths)

                if num_imgs < self.min_seq_len:
                    continue

                num_imgs_per_tracklet.append(num_imgs)
                img_paths = raw_img_paths
                # img_paths = []
                # for img_idx in range(num_imgs):
                #     # some tracklet starts from 0002 instead of 0001
                #     img_idx_name = 'F' + str(img_idx+1).zfill(4)
                #     res = glob.glob(osp.join(tdir, '*' + img_idx_name + '*.jpg'))
                #     if len(res) == 0:
                #         print("Warn: index name {} in {} is missing, jump to next".format(img_idx_name, tdir))
                #         continue
                #     img_paths.append(res[0])

                img_name = osp.basename(img_paths[0])
                tmax = int(osp.basename(img_paths[-1])[9:14]) / 30.
                tmin = int(osp.basename(img_paths[0])[9:14]) / 30.
                # naming format: 0001_C1_F00423_3047-1034-0270-0721.jpg
                camid = int(img_name[6]) - 1
                img_paths = tuple(img_paths)

                # tracklets.append((img_paths, pid, camid, tmin, tmax))
                tracklets.append((img_paths, pid, camid))

        num_pids = len(pid_container)
        num_tracklets = len(tracklets)

        print("Saving split to {}".format(json_path))
        split_dict = {
            'tracklets': tracklets,
            'num_tracklets': num_tracklets,
            'num_pids': num_pids,
            'num_imgs_per_tracklet': num_imgs_per_tracklet,
        }
        write_json(split_dict, json_path)

        return tracklets, num_tracklets, num_pids, num_imgs_per_tracklet