示例#1
0
def main():
    params = Params()
    process(params)

    src_path = params.src_path

    if not src_path:
        try:
            from Tkinter import Tk
        except ImportError:
            from tkinter import Tk
        try:
            src_path = Tk().clipboard_get()
        except BaseException as e:
            print('Tk().clipboard_get() failed: {}'.format(e))
            return

    src_path = src_path.replace(os.sep, '/').replace('"', '')
    assert os.path.exists(src_path), "src_path does not exist: {}".format(
        src_path)

    src_dir = os.path.dirname(src_path)
    src_name = os.path.basename(src_path)

    src_name_noext, src_ext = os.path.splitext(src_name)

    out_name = src_name_noext + '_' + src_ext

    out_path = os.path.join(src_dir, out_name)

    assert not os.path.exists(out_path), "out_path already exists"

    img = cv2.imread(src_path)

    img_h, img_w = img.shape[:2]

    out_h = out_w = 0

    if img_h > img_w:
        out_h = params.out_size
    else:
        out_w = params.out_size

    resized_img = resizeAR(img, width=out_w, height=out_h)

    cv2.imwrite(out_path, resized_img)
示例#2
0
    def write(self, frame, frame_id=None, prefix=''):
        if self._height or self._width:
            frame = resizeAR(frame, height=self._height, width=self._width)

        if frame_id is None:
            self.frame_id += 1
        else:
            self.frame_id = frame_id

        if prefix:
            self.filename = '{:s}.{:s}'.format(prefix, self._ext)
        else:
            self.filename = self._fmt % self.frame_id + '.{}'.format(self._ext)

        self.curr_file_path = os.path.join(self._save_dir, self.filename)
        cv2.imwrite(self.curr_file_path, frame,
                    (cv2.IMWRITE_JPEG_QUALITY, 100))
示例#3
0
def main():
    params = {
        'src_path': '.',
        'save_path': '',
        'save_root_dir': '',
        'img_ext': 'jpg',
        'show_img': 1,
        'del_src': 0,
        'start_id': 0,
        'n_frames': 0,
        'width': 0,
        'height': 0,
        'fps': 30,
        # 'codec': 'FFV1',
        # 'ext': 'avi',
        'codec': 'H264',
        'ext': 'mkv',
        'out_postfix': '',
        'reverse': 0,
        'move_src': 0,
        'use_skv': 0,
        'disable_suffix': 0,
        'read_in_batch': 1,
        'placement_type': 1,
        'recursive': 0,
    }

    paramparse.process_dict(params)
    _src_path = params['src_path']
    save_path = params['save_path']
    img_ext = params['img_ext']
    show_img = params['show_img']
    del_src = params['del_src']
    start_id = params['start_id']
    n_frames = params['n_frames']
    __width = params['width']
    __height = params['height']
    fps = params['fps']
    use_skv = params['use_skv']
    codec = params['codec']
    ext = params['ext']
    out_postfix = params['out_postfix']
    reverse = params['reverse']
    save_root_dir = params['save_root_dir']
    move_src = params['move_src']
    disable_suffix = params['disable_suffix']
    read_in_batch = params['read_in_batch']
    placement_type = params['placement_type']
    recursive = params['recursive']

    img_exts = ['.jpg', '.jpeg', '.png', '.bmp', '.tif']

    src_root_dir = ''

    if os.path.isdir(_src_path):
        if recursive:
            src_paths = [_src_path]
        else:
            src_files = [
                k for k in os.listdir(_src_path) for _ext in img_exts
                if k.endswith(_ext)
            ]
            if not src_files:
                # src_paths = [os.path.join(_src_path, k) for k in os.listdir(_src_path) if
                #              os.path.isdir(os.path.join(_src_path, k))]
                src_paths_gen = [[
                    os.path.join(dirpath, d) for d in dirnames if any([
                        os.path.splitext(f.lower())[1] in img_exts
                        for f in os.listdir(os.path.join(dirpath, d))
                    ])
                ] for (dirpath, dirnames,
                       filenames) in os.walk(_src_path, followlinks=True)]
                src_paths = [
                    item for sublist in src_paths_gen for item in sublist
                ]
                src_root_dir = os.path.abspath(_src_path)
            else:
                src_paths = [_src_path]
        print('Found {} image sequence(s):\n{}'.format(len(src_paths),
                                                       pformat(src_paths)))
    elif os.path.isfile(_src_path):
        print('Reading source image sequences from: {}'.format(_src_path))
        src_paths = [
            x.strip() for x in open(_src_path).readlines() if x.strip()
        ]
        n_seq = len(src_paths)
        if n_seq <= 0:
            raise SystemError(
                'No input sequences found in {}'.format(_src_path))
        print('n_seq: {}'.format(n_seq))
    else:
        raise IOError('Invalid src_path: {}'.format(_src_path))

    if recursive:
        print('searching for images recursively')

    if reverse == 1:
        print('Writing the reverse sequence')
    elif reverse == 2:
        print('Appending the reverse sequence')

    print('placement_type: {}'.format(placement_type))

    exit_prog = 0

    n_src_paths = len(src_paths)

    cwd = os.getcwd()

    for src_id, src_path in enumerate(src_paths):
        seq_name = os.path.basename(src_path)

        print('\n{}/{} Reading source images from: {}'.format(
            src_id + 1, n_src_paths, src_path))

        src_path = os.path.abspath(src_path)

        if move_src:
            rel_src_path = os.path.relpath(src_path, os.getcwd())
            dst_path = os.path.join(cwd, 'i2v', rel_src_path)
        else:
            dst_path = ''

        if recursive:
            src_file_gen = [[
                os.path.join(dirpath, f) for f in filenames
                if os.path.splitext(f.lower())[1] in img_exts
            ] for (dirpath, dirnames,
                   filenames) in os.walk(src_path, followlinks=True)]
            src_files = [item for sublist in src_file_gen for item in sublist]
        else:
            src_files = [
                k for k in os.listdir(src_path) for _ext in img_exts
                if k.endswith(_ext)
            ]
        n_src_files = len(src_files)
        if n_src_files <= 0:
            raise SystemError('No input frames found')
        src_files.sort(key=sortKey)
        print('n_src_files: {}'.format(n_src_files))

        if reverse == 1:
            src_files = src_files[::-1]
        elif reverse == 2:
            src_files += src_files[::-1]
            n_src_files *= 2

        _width, _height = __width, __height

        if os.path.exists(save_path):
            dst_mtime = os.path.getmtime(save_path)
            src_mtime = os.path.getmtime(src_path)

            dst_mtime_fmt = datetime.fromtimestamp(dst_mtime).strftime(
                '%Y-%m-%d %H:%M:%S')
            src_mtime_fmt = datetime.fromtimestamp(src_mtime).strftime(
                '%Y-%m-%d %H:%M:%S')

            print('Output video file already exists: {}'.format(save_path))

            if dst_mtime >= src_mtime:
                print(
                    'Last modified time: {} is not older than the source: {} so skipping it'
                    .format(dst_mtime_fmt, src_mtime_fmt))
                save_path = ''
                continue
            else:
                print(
                    'Last modified time: {} is older than the source: {} so overwriting it'
                    .format(dst_mtime_fmt, src_mtime_fmt))

        save_dir = os.path.dirname(save_path)
        if save_dir and not os.path.isdir(save_dir):
            os.makedirs(save_dir)

        src_images = []
        print('orig: {} x {}'.format(_width, _height))

        if read_in_batch:
            print('reading all images in batch')
            src_images = [read_image(src_path, k) for k in tqdm(src_files)]
            if _height <= 0 and _width <= 0:
                heights, widths = zip(*[k.shape[:2] for k in src_images])
                _height, _width = max(heights), max(widths)
            elif _height <= 0:
                _height, _width = sizeAR(src_images[0], width=_width)
            elif _width <= 0:
                _height, _width = sizeAR(src_images[0], height=_height)

        else:
            if _height <= 0 or _width <= 0:
                temp_img = cv2.imread(os.path.join(src_path, src_files[0]))
                _height, _width, _ = temp_img.shape

        print('inferred: {} x {}'.format(_width, _height))

        if not save_path:
            save_fname = os.path.basename(src_path)

            if not disable_suffix:
                save_fname = '{}_{}'.format(save_fname, fps)

                if _height > 0 and _width > 0:
                    save_fname = '{}_{}x{}'.format(save_fname, _width, _height)

                if out_postfix:
                    save_fname = '{}_{}'.format(save_fname, out_postfix)

                if reverse:
                    save_fname = '{}_r{}'.format(save_fname, reverse)

            save_path = os.path.join(os.path.dirname(src_path),
                                     '{}.{}'.format(save_fname, ext))

            if src_root_dir and save_root_dir:
                save_path = save_path.replace(src_root_dir, save_root_dir)
                print('save_path: {}'.format(save_path))
                print('src_root_dir: {}'.format(src_root_dir))
                print('save_root_dir: {}'.format(save_root_dir))
                print('save_path: {}'.format(save_path))
                # sys.exit()

        if use_skv:
            video_out = skvideo.io.FFmpegWriter(
                save_path,
                outputdict={
                    '-vcodec': 'libx264',  # use the h.264 codec
                    '-crf':
                    '0',  # set the constant rate factor to 0, which is lossless
                    '-preset':
                    'veryslow'  # the slower the better compression, in princple, try
                    # other options see https://trac.ffmpeg.org/wiki/Encode/H.264
                })
        elif codec == 'H265':
            video_out = VideoWriterGPU(save_path, fps, (_width, _height))
        else:
            fourcc = cv2.VideoWriter_fourcc(*codec)
            video_out = cv2.VideoWriter(save_path, fourcc, fps,
                                        (_width, _height))

        if video_out is None:
            raise IOError(
                'Output video file could not be opened: {}'.format(save_path))

        print('Saving {}x{} output video to {}'.format(_width, _height,
                                                       save_path))

        frame_id = start_id
        pause_after_frame = 0
        print_diff = max(1, int(n_src_files / 100))
        start_t = time.time()
        while True:
            if read_in_batch:
                image = src_images[frame_id]
            else:
                filename = src_files[frame_id]
                file_path = os.path.join(src_path, filename)
                if not os.path.exists(file_path):
                    raise SystemError(
                        'Image file {} does not exist'.format(file_path))

                image = cv2.imread(file_path)

            image = resizeAR(image,
                             _width,
                             _height,
                             placement_type=placement_type)

            if show_img:
                cv2.imshow(seq_name, image)
                k = cv2.waitKey(1 - pause_after_frame) & 0xFF
                if k == 27:
                    exit_prog = 1
                    break
                elif k == ord('q'):
                    break
                elif k == 32:
                    pause_after_frame = 1 - pause_after_frame

            if use_skv:
                video_out.writeFrame(
                    image[:, :, ::-1])  # write the frame as RGB not BGR
            else:
                video_out.write(image)

            frame_id += 1

            if frame_id % print_diff == 0:
                end_t = time.time()
                try:
                    proc_fps = float(print_diff) / (end_t - start_t)
                except:
                    proc_fps = 0

                sys.stdout.write(
                    '\rDone {:d}/{:d} frames at {:.4f} fps'.format(
                        frame_id - start_id, n_src_files - start_id, proc_fps))
                sys.stdout.flush()
                start_t = end_t

            if (frame_id - start_id) >= n_frames > 0:
                break

            if frame_id >= n_src_files:
                break

        sys.stdout.write('\n')
        sys.stdout.flush()

        if use_skv:
            video_out.close()  # close the writer
        else:
            video_out.release()

        if show_img:
            cv2.destroyWindow(seq_name)

        if move_src or del_src:
            move_or_del_files(src_path, src_files, dst_path)

        save_path = ''

        if exit_prog:
            break
示例#4
0
def main():
    _params = {
        'root_dir': '/data',
        'actor_id': 5,
        'start_id': 1,
        'end_id': -1,
        'ignored_region_only': 0,
        'speed': 0.5,
        'show_img': 0,
        'quality': 3,
        'resize': 0,
        'mode': 0,
        'auto_progress': 0,
    }

    bkg_occl_seq = [24, 28, 47, 48, 49, 54]

    paramparse.process_dict(_params)

    root_dir = _params['root_dir']
    actor_id = _params['actor_id']
    start_id = _params['start_id']
    ignored_region_only = _params['ignored_region_only']
    end_id = _params['end_id']
    show_img = _params['show_img']

    params = ParamDict().__dict__
    actors = params['mot_actors']
    sequences = params['mot_sequences']

    actor = actors[actor_id]
    actor_sequences = sequences[actor]

    if end_id <= start_id:
        end_id = len(actor_sequences) - 1

    print('root_dir: {}'.format(root_dir))
    print('actor_id: {}'.format(actor_id))
    print('start_id: {}'.format(start_id))
    print('end_id: {}'.format(end_id))

    print('actor: {}'.format(actor))
    print('actor_sequences: {}'.format(actor_sequences))

    n_frames_list = []
    _pause = 1
    __pause = 1

    for seq_id in range(start_id, end_id + 1):
        seq_name = actor_sequences[seq_id]
        fname = '{:s}/{:s}/Annotations/xml/{:s}.xml'.format(
            root_dir, actor, seq_name)
        tree = ET.parse(fname)
        root = tree.getroot()

        out_seq_name = 'detrac_{}_{}'.format(seq_id + 1, seq_name)

        out_fname = '{:s}/{:s}/Annotations/{:s}.txt'.format(
            root_dir, actor, out_seq_name)
        out_fid = open(out_fname, 'w')

        ignored_region_obj = tree.find('ignored_region')
        n_ignored_regions = 0

        for bndbox in ignored_region_obj.iter('box'):
            if bndbox is None:
                continue
            xmin = float(bndbox.attrib['left'])
            ymin = float(bndbox.attrib['top'])
            width = float(bndbox.attrib['width'])
            height = float(bndbox.attrib['height'])
            out_fid.write('-1,-1,{:f},{:f},{:f},{:f},-1,-1,-1,-1,-1\n'.format(
                xmin, ymin, width, height))
            n_ignored_regions += 1

        if ignored_region_only:
            out_fid.close()
            continue

        img_dir = '{:s}/{:s}/Images/{:s}'.format(root_dir, actor, out_seq_name)
        frames = glob.glob('{:s}/*.jpg'.format(img_dir))
        n_frames = len(frames)
        n_frames_list.append(n_frames)
        seq_occluded_dict = {}

        skip_seq = 0

        print('Processing sequence {:d} :: {:s} n_ignored_regions: {}'.format(
            seq_id, seq_name, n_ignored_regions))
        for frame_obj in tree.iter('frame'):
            target_list = frame_obj.find('target_list')
            frame_id = int(frame_obj.attrib['num'])

            if show_img:
                frame_path = os.path.join(img_dir,
                                          'image{:06d}.jpg'.format(frame_id))
                frame = cv2.imread(frame_path)
                obj_frame = np.copy(frame)

                if frame is None:
                    raise IOError(
                        'Failed to read frame: {}'.format(frame_path))
                cv2.putText(frame, str(frame_id), (10, 25),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 1,
                            cv2.LINE_AA)

            occluded = []
            occluded_dict = {}
            obj_dict = {}

            for obj in target_list.iter('target'):
                bndbox = obj.find('box')
                obj_id = int(obj.attrib['id'])
                xmin = float(bndbox.attrib['left'])
                ymin = float(bndbox.attrib['top'])
                width = float(bndbox.attrib['width'])
                height = float(bndbox.attrib['height'])

                assert obj_id not in obj_dict, "duplicate object found"
                obj_dict[obj_id] = (xmin, ymin, width, height)

                for occ_idx, occ_obj in enumerate(obj.iter('occlusion')):
                    occlusion = occ_obj.find('region_overlap')
                    occ_status = int(occlusion.attrib['occlusion_status'])
                    occ_id = int(occlusion.attrib['occlusion_id'])
                    occ_xmin = float(occlusion.attrib['left'])
                    occ_ymin = float(occlusion.attrib['top'])
                    occ_width = float(occlusion.attrib['width'])
                    occ_height = float(occlusion.attrib['height'])
                    if occ_status == 0:
                        """occluded by another obj"""
                        _obj_id = obj_id
                        _occ_id = occ_id
                    elif occ_status == 1:
                        """occluding another obj"""
                        _obj_id = occ_id
                        _occ_id = obj_id
                    elif occ_status == -1:
                        """occluded by background"""
                        """"seems extremely unreliable so ignoring"""
                        # _obj_id = obj_id
                        # _occ_id = occ_id
                        continue
                    else:
                        raise AssertionError(
                            'Invalid occ_status: {}'.format(occ_status))

                    # assert _obj_id not in occluded_dict, "duplicate occlusion found"

                    if _obj_id not in occluded_dict:
                        occluded_dict[_obj_id] = []

                    occluded_dict[_obj_id].append(
                        (_occ_id, occ_status, occ_xmin, occ_ymin, occ_width,
                         occ_height))

                    occluded.append((obj_id, occ_status, occ_id, occ_xmin,
                                     occ_ymin, occ_width, occ_height))

                    if occ_idx > 0:
                        raise AssertionError(
                            'Multiple occluding objects found')

            for obj_id in obj_dict:
                xmin, ymin, width, height = obj_dict[obj_id]
                xmax, ymax = xmin + width, ymin + height

                obj_img = np.zeros((int(height), int(width), 1),
                                   dtype=np.uint8)

                obj_img.fill(64)

                if obj_id in occluded_dict:
                    if show_img:
                        _obj_frame = np.copy(obj_frame)
                        drawBox(_obj_frame,
                                xmin,
                                ymin,
                                xmax,
                                ymax,
                                label=str(obj_id),
                                box_color=(255, 255, 255))

                    # __pause = imshow('_obj_frame', _obj_frame, __pause)

                    for _occluded in occluded_dict[obj_id]:
                        occ_id, occ_status, occ_xmin, occ_ymin, occ_width, occ_height = _occluded
                        occ_xmax, occ_ymax = occ_xmin + occ_width, occ_ymin + occ_height

                        start_x, end_x = int(occ_xmin - xmin), int(occ_xmax -
                                                                   xmin)
                        start_y, end_y = int(occ_ymin - ymin), int(occ_ymax -
                                                                   ymin)

                        # assert start_x >= 0 and start_y >= 0, "Invalid occlusion region start: {}".format(_occluded)
                        # assert end_x <= width and end_y <= height, \
                        # "Invalid occlusion region end: {} for obj: {}\n{}, {}".format(
                        #     _occluded, obj_dict[obj_id], (end_x, end_y), (width, height))

                        start_x, start_y = max(start_x, 0), max(start_y, 0)
                        end_x, end_y = min(end_x, width), min(end_y, height)

                        obj_img[int(start_y):int(end_y),
                                int(start_x):int(end_x)] = 192

                    n_occluded_pix = np.count_nonzero(obj_img == 192)
                    occluded_ratio = float(n_occluded_pix) / float(
                        obj_img.size)

                    if show_img:
                        _obj_img = resizeAR(obj_img, 500)
                        _obj_frame = resizeAR(_obj_frame, 1920, 1080)
                        cv2.putText(
                            _obj_img,
                            str('{}-{} : {:.2f}'.format(
                                obj_id, occ_id, occluded_ratio)), (10, 25),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1,
                            cv2.LINE_AA)
                        __pause, k = imshow(('obj_img', '_obj_frame'),
                                            (_obj_img, _obj_frame), __pause)
                        if k == ord('q'):
                            print("skip_seq")
                            skip_seq = 1
                            break
                else:
                    occ_id = occ_xmin = occ_ymin = occ_width = occ_height = -1
                    occluded_ratio = 0

                out_str = '{:d},{:d},{:f},{:f},{:f},{:f},1,-1,-1,-1,{:f}\n'.format(
                    frame_id, obj_id, xmin, ymin, width, height,
                    occluded_ratio)

                out_fid.write(out_str)

                seq_occluded_dict[frame_id] = occluded_dict

                if show_img:
                    drawBox(frame,
                            xmin,
                            ymin,
                            xmax,
                            ymax,
                            label=str(obj_id),
                            box_color=(255, 255, 255),
                            is_dotted=(occluded_ratio != 0))

            if skip_seq:
                break

            if show_img:
                for _occ in occluded:
                    obj_id, occ_status, occ_id, occ_xmin, occ_ymin, occ_width, occ_height = _occ

                    if occ_status == 1:
                        box_color = (0, 0, 255)
                    elif occ_status == 0:
                        box_color = (255, 0, 0)
                    elif occ_status == -1:
                        box_color = (0, 255, 0)

                    occ_xmax, occ_ymax = occ_xmin + occ_width, occ_ymin + occ_height
                    drawBox(frame,
                            occ_xmin,
                            occ_ymin,
                            occ_xmax,
                            occ_ymax,
                            box_color=box_color,
                            thickness=1,
                            label='{}-{}'.format(str(obj_id), str(occ_id)))

                frame = resizeAR(frame, 1920, 1080)
                _pause, k = imshow('frame', frame, _pause)

                if k == ord('q'):
                    print("skip_seq")
                    skip_seq = 1
                    break

            if frame_id % 100 == 0:
                print('\t Done {:d}/{:d} frames'.format(frame_id, n_frames))

        if skip_seq:
            continue

        meta_file_path = out_fname.replace('.txt', '.meta')
        with open(meta_file_path, 'w') as meta_fid:
            pprint(seq_occluded_dict, meta_fid)
        out_fid.close()

    print(n_frames_list)
示例#5
0
        #     wp_start_row = 0
        #     if screensize[0] >= 3840:
        #         wp_start_col = 1920
        #     else:
        #         wp_start_col = 0
        #
        # src_img_desktop = resizeAR(src_img, wp_width, wp_height)
        # wp_end_col = wp_start_col + src_img_desktop.shape[1]
        # wp_end_row = wp_start_row + src_img_desktop.shape[0]
        #
        #
        # src_img_desktop_full = np.zeros((screensize[1], screensize[0], 3), dtype=np.uint8)
        # src_img_desktop_full[wp_start_row:wp_end_row, wp_start_col:wp_end_col, :] = src_img_desktop

        if wallpaper_size != screensize:
            src_img = resizeAR(src_img, screensize[0], screensize[1])

        wp_fname = os.path.join(wallpaper_dir,
                                'wallpaper_{}.jpg'.format(frame_id))
        # wp_fname = os.path.join(wallpaper_dir, 'wallpaper.bmp')
        cv2.imwrite(wp_fname, src_img)

        # print('screensize: {}'.format(screensize))
        # print('wp_fname: {}'.format(wp_fname))

        win_wallpaper_func(SPI_SETDESKWALLPAPER, 0, wp_fname, 0)

        sys.stdout.write('\rDone {:d} frames '.format(frame_id - start_id))
        sys.stdout.flush()

        interrupt_wait.wait(interval)
示例#6
0
    frame_id = start_id
    pause_after_frame = 0
    while True:
        filename = src_file_list[frame_id]
        file_path = os.path.join(src_path, filename)
        if not os.path.exists(file_path):
            raise SystemError('Image file {} does not exist'.format(file_path))

        src_img = cv2.imread(file_path)
        h, w = [int(d * resize_factor) for d in src_img.shape[:2]]

        w_rmd = w % 8
        # if w_rmd != 0:
        #     print('Resized image width {} is not a multiple of 8'.format(w))

        src_img = resizeAR(src_img, w - w_rmd, h)
        w -= w_rmd
        src_img = src_img[:, :, 0].squeeze()

        dst_h, dst_w = h, int(w / 8)

        # print('src_size: {}x{}'.format(w, h))
        # print('dst_size: {}x{}'.format(dst_w, dst_h))

        dst_img = np.zeros((dst_h, dst_w), dtype=np.float64)
        for r in range(dst_h):
            curr_col = 0
            for c in range(dst_w):
                curr_pix_val = 0
                for k in range(8):
                    src_pix = src_img[r, curr_col]
示例#7
0
def main():
    _params = Params()

    paramparse.process(_params)

    root_dir = _params.root_dir
    start_id = _params.start_id
    end_id = _params.end_id

    write_img = _params.write_img
    write_gt = _params.write_gt

    save_img = _params.save_img
    save_vid = _params.save_vid
    codec = _params.codec

    show_img = _params.show_img
    vis_height = _params.vis_height
    vis_width = _params.vis_width

    # default_obj_size = _params.default_obj_size
    ignore_missing_gt = _params.ignore_missing_gt
    ignore_missing_seg = _params.ignore_missing_seg
    raad_gt = _params.raad_gt

    if save_img:
        if not show_img:
            show_img += 2
    else:
        if show_img > 1:
            save_img = 1

    params = ParamDict()

    actor = 'CTC'
    actor_sequences_dict = params.sequences_ctc

    actor_sequences = list(actor_sequences_dict.keys())

    if end_id <= start_id:
        end_id = len(actor_sequences) - 1

    print('root_dir: {}'.format(root_dir))
    print('start_id: {}'.format(start_id))
    print('end_id: {}'.format(end_id))

    print('actor: {}'.format(actor))
    print('actor_sequences: {}'.format(actor_sequences))
    img_exts = ('.tif', )

    n_frames_list = []
    _pause = 1
    __pause = 1

    ann_cols = ('green', 'blue', 'red', 'cyan', 'magenta', 'gold', 'purple',
                'peach_puff', 'azure', 'dark_slate_gray', 'navy', 'turquoise')

    out_img_tif_root_path = linux_path(root_dir, actor, 'Images_TIF')
    os.makedirs(out_img_tif_root_path, exist_ok=True)

    out_img_jpg_root_path = linux_path(root_dir, actor, 'Images')
    os.makedirs(out_img_jpg_root_path, exist_ok=True)

    if save_img:
        out_vis_root_path = linux_path(root_dir, actor, 'Visualizations')
        os.makedirs(out_vis_root_path, exist_ok=True)

    out_gt_root_path = linux_path(root_dir, actor, 'Annotations')
    os.makedirs(out_gt_root_path, exist_ok=True)

    n_frames_out_file = linux_path(root_dir, actor, 'n_frames.txt')
    n_frames_out_fid = open(n_frames_out_file, 'w')

    _exit = 0
    _pause = 1
    time_stamp = datetime.now().strftime("%y%m%d_%H%M%S")

    log_path = linux_path(root_dir, actor, 'log_{}.log'.format(time_stamp))
    tif_root_dir = linux_path(root_dir, actor, 'tif')
    assert os.path.exists(tif_root_dir), "tif_root_dir does not exist"

    seq_ids = _params.seq_ids
    if not seq_ids:
        seq_ids = list(range(start_id, end_id + 1))

    n_seq = len(seq_ids)
    for __id, seq_id in enumerate(seq_ids):

        seq_name = actor_sequences[seq_id]

        default_obj_size = actor_sequences_dict[seq_name]

        seq_img_path = linux_path(tif_root_dir, seq_name)
        assert os.path.exists(seq_img_path), "seq_img_path does not exist"

        seq_img_src_files = [
            k for k in os.listdir(seq_img_path)
            if os.path.splitext(k.lower())[1] in img_exts
        ]
        seq_img_src_files.sort()

        out_gt_fid = None

        n_frames = len(seq_img_src_files)

        print('seq {} / {}\t{}\t{}\t{} frames'.format(__id + 1, n_seq, seq_id,
                                                      seq_name, n_frames))

        n_frames_out_fid.write("{:d}: ('{:s}', {:d}),\n".format(
            seq_id, seq_name, n_frames))

        n_frames_list.append(n_frames)

        gt_available = 0
        if not raad_gt:
            print('skipping GT reading')
        else:
            seq_gt_path = linux_path(tif_root_dir, seq_name + '_GT', 'TRA')
            if not os.path.exists(seq_gt_path):
                msg = "seq_gt_path does not exist"
                if ignore_missing_gt:
                    print(msg)
                else:
                    raise AssertionError(msg)
            else:
                gt_available = 1
                seq_gt_tra_file = linux_path(seq_gt_path, "man_track.txt")
                if os.path.exists(seq_gt_tra_file):
                    out_tra_file = linux_path(out_gt_root_path,
                                              seq_name + '.tra')
                    print('{} --> {}'.format(seq_gt_tra_file, out_tra_file))
                    if not os.path.exists(out_tra_file):
                        shutil.copy(seq_gt_tra_file, out_tra_file)
                    else:
                        print('skipping existing {}'.format(out_tra_file))
                else:
                    msg = "\nseq_gt_tra_file does not exist: {}".format(
                        seq_gt_tra_file)
                    if ignore_missing_gt:
                        print(msg)
                    else:
                        raise AssertionError(msg)

        if _params.tra_only:
            continue

        seg_available = 0
        if not raad_gt:
            print('skipping segmentation reading')
        else:
            seq_seg_path = linux_path(tif_root_dir, seq_name + '_ST', 'SEG')
            if not os.path.exists(seq_seg_path):
                print("ST seq_seg_path does not exist")
                seq_seg_path = linux_path(tif_root_dir, seq_name + '_GT',
                                          'SEG')
                if not os.path.exists(seq_seg_path):
                    msg = "GT seq_seg_path does not exist"
                    if ignore_missing_seg:
                        print(msg)
                    else:
                        raise AssertionError(msg)
                else:
                    seg_available = 1
            else:
                seg_available = 1

        if write_img:
            out_img_tif_dir_path = linux_path(out_img_tif_root_path, seq_name)
            os.makedirs(out_img_tif_dir_path, exist_ok=True)
            print('copying TIF images to {}'.format(out_img_tif_dir_path))

            out_img_jpg_dir_path = linux_path(out_img_jpg_root_path, seq_name)
            os.makedirs(out_img_jpg_dir_path, exist_ok=True)
            print('Saving jPG images to {}'.format(out_img_jpg_dir_path))

        vid_out = None
        if save_img:
            if save_vid:
                out_vis_path = linux_path(out_vis_root_path, seq_name + '.mkv')
                vid_out = cv2.VideoWriter(out_vis_path,
                                          cv2.VideoWriter_fourcc(*codec), 30,
                                          (vis_width, vis_height))
            else:
                out_vis_path = linux_path(out_vis_root_path, seq_name)
                os.makedirs(out_vis_path, exist_ok=True)
            print('Saving visualizations to {}'.format(out_vis_path))

        from collections import OrderedDict

        file_id_to_gt = OrderedDict()
        obj_id_to_gt_file_ids = OrderedDict()

        if gt_available:
            print('reading GT from {}...'.format(seq_gt_path))

            seq_gt_src_files = [
                k for k in os.listdir(seq_gt_path)
                if os.path.splitext(k.lower())[1] in img_exts
            ]
            seq_gt_src_files.sort()

            assert len(seq_img_src_files) == len(
                seq_gt_src_files
            ), "mismatch between the lengths of seq_img_src_files and seq_gt_src_files"

            for seq_gt_src_file in tqdm(seq_gt_src_files,
                                        disable=_params.disable_tqdm):
                seq_gt_src_file_id = ''.join(k for k in seq_gt_src_file
                                             if k.isdigit())

                file_id_to_gt[seq_gt_src_file_id] = OrderedDict()

                seq_gt_src_path = os.path.join(seq_gt_path, seq_gt_src_file)
                seq_gt_pil = Image.open(seq_gt_src_path)
                seq_gt_np = np.array(seq_gt_pil)

                gt_obj_ids = list(np.unique(seq_gt_np, return_counts=False))
                gt_obj_ids.remove(0)

                for obj_id in gt_obj_ids:
                    obj_locations = np.nonzero(seq_gt_np == obj_id)
                    centroid_y, centroid_x = [
                        np.mean(k) for k in obj_locations
                    ]

                    file_id_to_gt[seq_gt_src_file_id][obj_id] = [
                        obj_locations, centroid_y, centroid_x
                    ]

                    if obj_id not in obj_id_to_gt_file_ids:
                        obj_id_to_gt_file_ids[obj_id] = []

                    obj_id_to_gt_file_ids[obj_id].append(seq_gt_src_file_id)

            if write_gt:
                out_gt_path = linux_path(out_gt_root_path, seq_name + '.txt')
                out_gt_fid = open(out_gt_path, 'w')

        file_id_to_seg = OrderedDict()
        file_id_to_nearest_seg = OrderedDict()

        obj_id_to_seg_file_ids = OrderedDict()
        obj_id_to_seg_sizes = OrderedDict()
        obj_id_to_seg_bboxes = OrderedDict()
        obj_id_to_mean_seg_sizes = OrderedDict()
        obj_id_to_max_seg_sizes = OrderedDict()
        all_seg_sizes = []
        mean_seg_sizes = None
        max_seg_sizes = None

        if seg_available:
            print('reading segmentations from {}...'.format(seq_seg_path))
            seq_seq_src_files = [
                k for k in os.listdir(seq_seg_path)
                if os.path.splitext(k.lower())[1] in img_exts
            ]
            for seq_seq_src_file in tqdm(seq_seq_src_files,
                                         disable=_params.disable_tqdm):

                seq_seq_src_file_id = ''.join(k for k in seq_seq_src_file
                                              if k.isdigit())
                file_gt = file_id_to_gt[seq_seq_src_file_id]

                file_id_to_seg[seq_seq_src_file_id] = OrderedDict()

                seq_seq_src_path = os.path.join(seq_seg_path, seq_seq_src_file)
                seq_seg_pil = Image.open(seq_seq_src_path)
                seq_seg_np = np.array(seq_seg_pil)

                seg_obj_ids = list(np.unique(seq_seg_np, return_counts=False))
                seg_obj_ids.remove(0)

                _gt_obj_ids = list(file_gt.keys())

                if len(_gt_obj_ids) != len(seg_obj_ids):
                    print(
                        "\nmismatch between the number of objects in segmentation: {} and GT: {} in {}"
                        .format(len(seg_obj_ids), len(_gt_obj_ids),
                                seq_seq_src_file))

                from scipy.spatial import distance_matrix

                seg_centroids = []
                seg_id_to_locations = {}

                for seg_obj_id in seg_obj_ids:
                    # obj_id = gt_obj_ids[seg_obj_id - 1]

                    seg_obj_locations = np.nonzero(seq_seg_np == seg_obj_id)
                    seg_centroid_y, seg_centroid_x = [
                        np.mean(k) for k in seg_obj_locations
                    ]

                    seg_centroids.append([seg_centroid_y, seg_centroid_x])

                    seg_id_to_locations[seg_obj_id] = seg_obj_locations

                gt_centroids = [[k[1], k[2]] for k in file_gt.values()]
                gt_centroids = np.asarray(gt_centroids)
                seg_centroids = np.asarray(seg_centroids)

                gt_to_seg_dists = distance_matrix(seg_centroids, gt_centroids)

                # seg_min_dist_ids = np.argmin(gt_to_seg_dists, axis=1)
                # gt_min_dist_ids = np.argmin(gt_to_seg_dists, axis=0)
                # unique_min_dist_ids = np.unique(seg_min_dist_ids)                #
                #
                # assert len(unique_min_dist_ids) == len(seg_min_dist_ids), \
                #     "duplicate matches found between segmentation and GT objects"

                # seg_to_gt_obj_ids = {
                #     seg_obj_id:  _gt_obj_ids[seg_min_dist_ids[_id]] for _id, seg_obj_id in enumerate(seg_obj_ids)
                # }

                from scipy.optimize import linear_sum_assignment

                seg_inds, gt_inds = linear_sum_assignment(gt_to_seg_dists)

                if len(seg_inds) != len(seg_obj_ids):
                    print(
                        "only {} / {} segmentation objects assigned to GT objects"
                        .format(len(seg_inds), len(seg_obj_ids)))

                seg_to_gt_obj_ids = {
                    seg_obj_ids[seg_inds[i]]: _gt_obj_ids[gt_inds[i]]
                    for i in range(len(seg_inds))
                }

                # print()

                for seg_obj_id in seg_obj_ids:
                    seg_obj_locations = seg_id_to_locations[seg_obj_id]
                    _gt_obj_id = seg_to_gt_obj_ids[seg_obj_id]

                    min_y, min_x = [np.amin(k) for k in seg_obj_locations]
                    max_y, max_x = [np.amax(k) for k in seg_obj_locations]

                    size_x, size_y = max_x - min_x, max_y - min_y

                    if _gt_obj_id not in obj_id_to_seg_sizes:
                        obj_id_to_seg_bboxes[_gt_obj_id] = []
                        obj_id_to_seg_sizes[_gt_obj_id] = []
                        obj_id_to_seg_file_ids[_gt_obj_id] = []

                    obj_id_to_seg_file_ids[_gt_obj_id].append(
                        seq_seq_src_file_id)

                    file_id_to_seg[seq_seq_src_file_id][_gt_obj_id] = (
                        seg_obj_locations, [min_x, min_y, max_x, max_y])

                    obj_id_to_seg_bboxes[_gt_obj_id].append(
                        [seq_seq_src_file, min_x, min_y, max_x, max_y])
                    obj_id_to_seg_sizes[_gt_obj_id].append([size_x, size_y])

                    all_seg_sizes.append([size_x, size_y])

            obj_id_to_mean_seg_sizes = OrderedDict({
                k: np.mean(v, axis=0)
                for k, v in obj_id_to_seg_sizes.items()
            })
            obj_id_to_max_seg_sizes = OrderedDict({
                k: np.amax(v, axis=0)
                for k, v in obj_id_to_seg_sizes.items()
            })

            print('segmentations found for {} files'.format(
                len(file_id_to_seg),
                # '\n'.join(file_id_to_seg.keys())
            ))
            print('segmentations include {} objects:\n{}'.format(
                len(obj_id_to_seg_bboxes),
                ', '.join(str(k) for k in obj_id_to_seg_bboxes.keys())))

            mean_seg_sizes = np.mean(all_seg_sizes, axis=0)
            max_seg_sizes = np.amax(all_seg_sizes, axis=0)

            for obj_id in obj_id_to_seg_file_ids:
                seg_file_ids = obj_id_to_seg_file_ids[obj_id]
                seg_file_ids_num = np.asarray(
                    list(int(k) for k in seg_file_ids))
                gt_file_ids = obj_id_to_gt_file_ids[obj_id]
                # gt_file_ids_num = np.asarray(list(int(k) for k in gt_file_ids))
                gt_seg_file_ids_dist = {
                    gt_file_id: np.abs(int(gt_file_id) - seg_file_ids_num)
                    for gt_file_id in gt_file_ids
                }
                file_id_to_nearest_seg[obj_id] = {
                    gt_file_id: seg_file_ids[np.argmin(_dist).item()]
                    for gt_file_id, _dist in gt_seg_file_ids_dist.items()
                }

        nearest_seg_size = OrderedDict()

        for frame_id in tqdm(range(n_frames), disable=_params.disable_tqdm):
            seq_img_src_file = seq_img_src_files[frame_id]

            # assert seq_img_src_file in seq_gt_src_file, \
            #     "mismatch between seq_img_src_file and seq_gt_src_file"

            seq_img_src_file_id = ''.join(k for k in seq_img_src_file
                                          if k.isdigit())

            seq_img_src_path = os.path.join(seq_img_path, seq_img_src_file)

            # seq_img_pil = Image.open(seq_img_src_path)
            # seq_img = np.array(seq_img_pil)

            seq_img = cv2.imread(seq_img_src_path, cv2.IMREAD_UNCHANGED)
            # assert (seq_img == seq_img_cv).all(), "mismatch between PIL and cv2 arrays"
            # seq_img_cv_unique = np.unique(seq_img_cv)
            # n_seq_img_unique_cv = len(seq_img_cv_unique)

            # seq_img_float = seq_img.astype(np.float32) / 65535.

            max_pix, min_pix = np.amax(seq_img), np.amin(seq_img)

            seq_img_float_norm = (seq_img.astype(np.float32) -
                                  min_pix) / (max_pix - min_pix)
            seq_img_uint8 = (seq_img_float_norm * 255.).astype(np.uint8)
            # seq_img_uint8 = (seq_img / 256.).astype(np.uint8)
            max_pix_uint8, min_pix_uint8 = np.amax(seq_img_uint8), np.amin(
                seq_img_uint8)

            seq_img_unique = np.unique(seq_img)
            seq_img_unique_uint8 = np.unique(seq_img_uint8)

            n_seq_img_unique = len(seq_img_unique)
            n_seq_img_unique_uint8 = len(seq_img_unique_uint8)

            if n_seq_img_unique > n_seq_img_unique_uint8:
                # print('{} :: drop in number of unique values from {} ({}, {}) to {} ({}, {})'.format(
                #     seq_img_src_file, n_seq_img_unique, max_pix, min_pix,
                #     n_seq_img_unique_uint8, max_pix_uint8, min_pix_uint8))
                with open(log_path, 'a') as log_fid:
                    log_fid.write('{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n'.format(
                        seq_name, seq_img_src_file, n_seq_img_unique,
                        n_seq_img_unique_uint8, max_pix, min_pix,
                        max_pix_uint8, min_pix_uint8))
                # print()

            if write_img:
                # out_img_file = os.path.splitext(seq_img_src_file)[0] + '.png'
                # out_img_file_path = linux_path(out_img_tif_dir_path, out_img_file)

                out_img_file_tif = os.path.splitext(
                    seq_img_src_file)[0] + '.tif'
                out_img_file_path_tif = linux_path(out_img_tif_dir_path,
                                                   out_img_file_tif)

                out_img_file_uint8 = os.path.splitext(
                    seq_img_src_file)[0] + '.jpg'
                out_img_file_path_uint8 = linux_path(out_img_jpg_dir_path,
                                                     out_img_file_uint8)

                if not os.path.exists(out_img_file_path_uint8):
                    # cv2.imwrite(out_img_file_path, seq_img)
                    cv2.imwrite(out_img_file_path_uint8, seq_img_uint8)

                if not os.path.exists(out_img_file_path_tif):
                    # print('{} --> {}'.format(seq_img_src_path, out_img_file_path_tif))
                    shutil.copyfile(seq_img_src_path, out_img_file_path_tif)

            if show_img:
                seq_img_col = seq_img_uint8.copy()
                if len(seq_img_col.shape) == 2:
                    seq_img_col = cv2.cvtColor(seq_img_col, cv2.COLOR_GRAY2BGR)

                # seq_img_col2 = seq_img_col.copy()

                seq_img_col3 = seq_img_col.copy()

            if not gt_available:
                if save_img:
                    if vid_out is not None:
                        vid_out.write(seq_img_col)
                continue

            file_gt = file_id_to_gt[seq_img_src_file_id]

            # seq_gt_src_file = seq_gt_src_files[frame_id]
            # assert seq_gt_src_file_id == seq_img_src_file_id, \
            #     "Mismatch between seq_gt_src_file_id and seq_img_src_file_id"

            gt_obj_ids = list(file_gt.keys())
            for obj_id in gt_obj_ids:
                assert obj_id != 0, "invalid object ID"

                try:
                    nearest_seg_file_ids = file_id_to_nearest_seg[obj_id]
                except KeyError:
                    file_seg = {}
                else:
                    nearest_seg_file_id = nearest_seg_file_ids[
                        seq_img_src_file_id]
                    file_seg = file_id_to_seg[nearest_seg_file_id]

                obj_locations, centroid_y, centroid_x = file_gt[obj_id]

                if file_seg:
                    xmin, ymin, xmax, ymax = file_seg[obj_id][1]
                    size_x, size_y = xmax - xmin, ymax - ymin
                    nearest_seg_size[obj_id] = (size_x, size_y)
                else:
                    try:
                        size_x, size_y = nearest_seg_size[obj_id]
                    except KeyError:
                        try:
                            size_x, size_y = obj_id_to_mean_seg_sizes[obj_id]
                        except KeyError:
                            if mean_seg_sizes is not None:
                                size_x, size_y = mean_seg_sizes
                            else:
                                size_x, size_y = default_obj_size, default_obj_size

                    ymin, xmin = centroid_y - size_y / 2.0, centroid_x - size_x / 2.0
                    ymax, xmax = centroid_y + size_y / 2.0, centroid_x + size_x / 2.0

                width = int(xmax - xmin)
                height = int(ymax - ymin)

                if show_img:
                    col_id = (obj_id - 1) % len(ann_cols)

                    col = col_rgb[ann_cols[col_id]]
                    drawBox(seq_img_col,
                            xmin,
                            ymin,
                            xmax,
                            ymax,
                            label=str(obj_id),
                            box_color=col)
                    # seq_img_col2[obj_locations] = col

                    if file_seg:
                        try:
                            locations, bbox = file_seg[obj_id][:2]
                        except KeyError:
                            print('weird stuff going on here')
                        else:
                            seq_img_col3[locations] = col
                            # min_x, min_y, max_x, max_y = bbox
                            # drawBox(seq_img_col3, min_x, min_y, max_x, max_y, label=str(obj_id), box_color=col)

                if write_gt:
                    out_gt_fid.write(
                        '{:d},{:d},{:.3f},{:.3f},{:d},{:d},1,-1,-1,-1\n'.
                        format(frame_id + 1, obj_id, xmin, ymin, width,
                               height))
                # print()
            skip_seq = 0

            if show_img:
                # images_to_stack = [seq_img_col, seq_img_col2]
                images_to_stack = [
                    seq_img_col,
                ]
                if file_seg:
                    images_to_stack.append(seq_img_col3)
                    __pause = _pause
                else:
                    __pause = _pause

                seq_img_vis = stackImages(images_to_stack, sep_size=5)

                seq_img_vis = resizeAR(seq_img_vis,
                                       height=vis_height,
                                       width=vis_width)

                cv2.putText(seq_img_vis,
                            '{}: {}'.format(seq_name,
                                            seq_img_src_file_id), (20, 20),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

                if show_img != 2:
                    cv2.imshow('seq_img_vis', seq_img_vis)
                    k = cv2.waitKey(1 - __pause)
                    if k == 32:
                        _pause = 1 - _pause
                    elif k == 27:
                        skip_seq = 1
                        break
                    elif k == ord('q'):
                        break

                if save_img:
                    if vid_out is not None:
                        vid_out.write(seq_img_vis)
                    else:
                        out_vis_file = os.path.splitext(
                            seq_img_src_file)[0] + '.jpg'
                        out_vis_file_path = linux_path(out_vis_path,
                                                       out_vis_file)
                        cv2.imwrite(out_vis_file_path, seq_img_vis)

            if skip_seq or _exit:
                break
        if vid_out is not None:
            vid_out.release()

        if out_gt_fid is not None:
            out_gt_fid.close()

        if _exit:
            break

    n_frames_out_fid.close()
示例#8
0
                                        (dst_width, dst_height))
        elif ext in image_exts:
            video_out = ImageSequenceWriter(dst_path)
        else:
            raise IOError('Invalid ext: {}'.format(ext))

        if video_out is None:
            raise IOError(
                'Output video file could not be opened: {}'.format(dst_path))

        print('Saving {}x{} output video to {}'.format(dst_width, dst_height,
                                                       dst_path))

    video_out.write(out_img)
    if show_img:
        out_img_disp = resizeAR(out_img, 1920, 1080)
        cv2.imshow(win_name, out_img_disp)
        k = cv2.waitKey(1 - pause_after_frame) & 0xFF
        if k == ord('q') or k == 27:
            break
        elif k == 32:
            pause_after_frame = 1 - pause_after_frame

    sys.stdout.write('\rDone {:d}/{:d} frames '.format(frame_id - start_id,
                                                       n_frames))
    sys.stdout.flush()

    if frame_id - start_id >= n_frames:
        break

sys.stdout.write('\n')
示例#9
0
    print('Saving {}x{} output video to {}'.format(width, height, save_path))

    frame_id = start_id
    pause_after_frame = 0
    print_diff = max(1, int(n_src_files / 100))
    start_t = time.time()
    while True:
        filename = src_files[frame_id]
        file_path = os.path.join(src_path, filename)
        if not os.path.exists(file_path):
            raise SystemError('Image file {} does not exist'.format(file_path))

        image = cv2.imread(file_path)

        image = resizeAR(image, width, height)

        if show_img:
            cv2.imshow(seq_name, image)
            k = cv2.waitKey(1 - pause_after_frame) & 0xFF
            if k == 27:
                exit_prog = 1
                break
            elif k == ord('q'):
                break
            elif k == 32:
                pause_after_frame = 1 - pause_after_frame

        video_out.write(image)

        frame_id += 1
示例#10
0
        if video_out is None:
            raise IOError('Output video file could not be opened: {}'.format(dst_path))

        print('Saving {}x{} output video to {}'.format(dst_width, dst_height, dst_path))

    if start_id > 0:
        print('Starting from frame_id {}'.format(start_id))

    frame_id = 0
    pause_after_frame = 0
    frames = []

    if add_headers > 0:
        header_path = header_files[src_id]
        header_img = cv2.imread(header_path)
        header_img = resizeAR(header_img, dst_width, dst_height)
        for i in range(n_header_frames):
            video_out.write(header_img)

    while True:

        ret, image = cap.read()
        if not ret:
            print('\nFrame {:d} could not be read'.format(frame_id + 1))
            break

        frame_id += 1

        if frame_id <= start_id:
            continue
示例#11
0
    if video_out is None:
        raise IOError('Output video file could not be opened: {}'.format(dst_path))

    print('Saving {}x{} output video to {}'.format(dst_width, dst_height, dst_path))

    frame_id = start_id
    pause_after_frame = 0
    while True:

        ret, image = cap.read()
        if not ret:
            print('\nFrame {:d} could not be read'.format(frame_id + 1))
            break

        image = resizeAR(image, dst_width, dst_height)

        if show_img:
            cv2.imshow(seq_name, image)
            k = cv2.waitKey(1 - pause_after_frame) & 0xFF
            if k == ord('q') or k == 27:
                break
            elif k == 32:
                pause_after_frame = 1 - pause_after_frame

        video_out.write(image)

        frame_id += 1
        sys.stdout.write('\rDone {:d} frames '.format(frame_id - start_id))
        sys.stdout.flush()
示例#12
0
                raise IOError(
                    'Output video file could not be opened: {}'.format(
                        dst_path))

    if start_id > 0:
        print('Starting from frame_id {}'.format(start_id))

    frame_id = 0
    pause_after_frame = 0
    frames = []

    if add_headers > 0:
        header_path = header_files[src_id]
        header_img = cv2.imread(header_path)
        header_img = resizeAR(header_img,
                              dst_width,
                              dst_height,
                              placement_type=1)

        if rotate == 1:
            header_img = imutils.rotate_bound(header_img, 90)
        elif rotate == -1:
            header_img = imutils.rotate_bound(header_img, -90)
        elif rotate == 2:
            header_img = imutils.rotate_bound(header_img, 180)
        elif rotate == -2:
            header_img = imutils.rotate_bound(header_img, -180)

        for i in range(n_header_frames):
            if use_skv:
                video_out.writeFrame(
                    header_img[:, :, ::-1])  # write the frame as RGB not BGR
示例#13
0
def main():
    params = Params()

    paramparse.process(params)

    root_dirs = params.root_dirs
    annotations = params.annotations
    save_path = params.save_path
    img_ext = params.img_ext
    show_img = params.show_img
    del_src = params.del_src
    start_id = params.start_id
    n_frames = params.n_frames
    width = params.width
    height = params.height
    fps = params.fps
    codec = params.codec
    ext = params.ext
    out_width = params.out_width
    out_height = params.out_height
    grid_size = params.grid_size
    sep_size = params.sep_size
    only_height = params.only_height
    borderless = params.borderless
    preserve_order = params.preserve_order
    ann_fmt = params.ann_fmt
    resize_factor = params.resize_factor
    recursive = params.recursive
    img_seq = params.img_seq
    match_images = params.match_images

    if match_images:
        assert img_seq, "image matching is only supported in image sequence mode"

    vid_exts = ['.mkv', '.mp4', '.avi', '.mjpg', '.wmv']
    image_exts = ['.jpg', '.bmp', '.png', '.tif']

    min_n_sources = None
    min_sources = None
    min_sources_id = None

    sources_list = []
    for root_dir_id, root_dir in enumerate(root_dirs):
        root_dir = os.path.abspath(root_dir)
        if img_seq:
            sources = [k for k in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, k))]
        else:
            sources = [k for k in os.listdir(root_dir) if os.path.splitext(k)[1] in vid_exts]
        sources.sort()

        n_sources = len(sources)
        if min_n_sources is None or n_sources < min_n_sources:
            min_n_sources = n_sources
            min_sources = sources
            min_sources_id = root_dir_id

        sources = [os.path.join(root_dir, k) for k in sources]

        sources_list.append(sources)

    if match_images:
        for sources_id, sources in enumerate(sources_list):
            if sources_id == min_sources_id:
                continue

            sources = [k for k in sources if os.path.basename(k) in min_sources]

            assert len(sources) == min_n_sources, "invalid sources after filtering {}".format(sources)

            sources_list[sources_id] = sources

    src_paths = list(zip(*sources_list))

    print('sources_list:\n{}'.format(pformat(sources_list)))
    print('src_paths:\n{}'.format(pformat(src_paths)))

    timestamp = datetime.now().strftime("%y%m%d_%H%M%S")

    _exit = 0

    for _src_path in src_paths:
        _annotations = annotations
        _save_path = save_path
        _grid_size = grid_size
        n_frames = 0

        src_files = _src_path

        n_videos = len(src_files)
        assert n_videos > 0, 'no input videos found'

        if not _save_path:
            seq_dir = os.path.dirname(src_files[0])
            seq_name = os.path.splitext(os.path.basename(src_files[0]))[0]
            dst_path = os.path.join(seq_dir, 'stacked_{}'.format(timestamp), '{}.{}'.format(seq_name, ext))
        else:
            out_seq_name, out_ext = os.path.splitext(os.path.basename(_save_path))
            dst_path = os.path.join(os.path.dirname(_save_path), '{}_{}{}'.format(
                out_seq_name, datetime.now().strftime("%y%m%d_%H%M%S"), out_ext))

        save_dir = os.path.dirname(dst_path)
        if save_dir and not os.path.isdir(save_dir):
            os.makedirs(save_dir)

        print('Stacking: {} videos:'.format(n_videos))
        print('src_files:\n{}'.format(pformat(src_files)))

        if _annotations:
            if len(_annotations) == 1 and _annotations[0] == 1:
                _annotations = []
                for i in range(n_videos):
                    _annotations.append(seq_names[i])
            else:
                assert len(_annotations) == n_videos, 'Invalid annotations: {}'.format(_annotations)

                for i in range(n_videos):
                    if _annotations[i] == '__n__':
                        _annotations[i] = ''

            print('Adding annotations:\n{}'.format(pformat(_annotations)))
        else:
            _annotations = None

        if not _grid_size:
            _grid_size = None
        else:
            _grid_size = [int(x) for x in _grid_size.split('x')]
            if len(_grid_size) != 2 or _grid_size[0] * _grid_size[1] != n_videos:
                raise AssertionError('Invalid grid_size: {}'.format(_grid_size))

        n_frames_list = []
        cap_list = []
        size_list = []
        seq_names = []

        min_n_frames = None
        min_n_frames_id = 0

        for src_id, src_file in enumerate(src_files):
            src_file = os.path.abspath(src_file)
            seq_name = os.path.splitext(os.path.basename(src_file))[0]

            seq_names.append(seq_name)

            if os.path.isfile(src_file):
                cap = cv2.VideoCapture()
            elif os.path.isdir(src_file):
                cap = ImageSequenceCapture(src_file, recursive=recursive)
            else:
                raise IOError('Invalid src_file: {}'.format(src_file))

            if not cap.open(src_file):
                raise IOError('The video file ' + src_file + ' could not be opened')

            cv_prop = cv2.CAP_PROP_FRAME_COUNT
            h_prop = cv2.CAP_PROP_FRAME_HEIGHT
            w_prop = cv2.CAP_PROP_FRAME_WIDTH

            total_frames = int(cap.get(cv_prop))
            _height = int(cap.get(h_prop))
            _width = int(cap.get(w_prop))

            cap_list.append(cap)
            n_frames_list.append(total_frames)
            if min_n_frames is None or total_frames < min_n_frames:
                min_n_frames = total_frames
                min_n_frames_id = src_id

            size_list.append((_width, _height))

        if match_images:
            assert all(seq_name == seq_names[0] for seq_name in seq_names), "mismatch in seq_names: {}".format(seq_names)

            frames_list = [os.path.basename(k) for k in cap_list[min_n_frames_id].src_files]
            for src_id, cap in enumerate(cap_list):
                if src_id == min_n_frames_id:
                    continue
                cap_list[src_id].filter_files(frames_list)
                n_frames_list[src_id] = min_n_frames

        frame_id = start_id
        pause_after_frame = 0
        video_out = None

        win_name = 'stacked_{}'.format(datetime.now().strftime("%y%m%d_%H%M%S"))

        min_n_frames = min(n_frames_list)
        max_n_frames = max(n_frames_list)

        if n_frames <= 0:
            n_frames = max_n_frames
        else:
            if max_n_frames < n_frames:
                raise IOError(
                    'Invalid n_frames: {} for sequence list with max_n_frames: {}'.format(n_frames, max_n_frames))

        if show_img == 2:
            vis_only = True
            print('Running in visualization only mode')
        else:
            vis_only = False

        while True:

            images = []
            valid_caps = []
            valid_annotations = []
            for cap_id, cap in enumerate(cap_list):
                ret, image = cap.read()
                if not ret:
                    print('\nFrame {:d} could not be read'.format(frame_id + 1))
                    continue
                images.append(image)
                valid_caps.append(cap)
                if _annotations:
                    valid_annotations.append(_annotations[cap_id])

            cap_list = valid_caps
            if _annotations:
                _annotations = valid_annotations

            # if len(images) != n_videos:
            #     break

            frame_id += 1

            if frame_id <= start_id:
                break

            out_img = stackImages(images, _grid_size, borderless=borderless, preserve_order=preserve_order,
                                  annotations=_annotations, ann_fmt=ann_fmt, only_height=only_height, sep_size=sep_size)
            if resize_factor != 1:
                out_img = cv2.resize(out_img, (0, 0), fx=resize_factor, fy=resize_factor)

            if not vis_only:
                if video_out is None:
                    dst_height, dst_width = sizeAR(out_img, width=out_width, height=out_height)

                    if '.' + ext in vid_exts:
                        fourcc = cv2.VideoWriter_fourcc(*codec)
                        video_out = cv2.VideoWriter(dst_path, fourcc, fps, (dst_width, dst_height))
                    elif '.' + ext in image_exts:
                        video_out = ImageSequenceWriter(dst_path, height=dst_height, width=dst_width)
                    else:
                        raise IOError('Invalid ext: {}'.format(ext))

                    if video_out is None:
                        raise IOError('Output video file could not be opened: {}'.format(dst_path))

                    print('Saving {}x{} output video to {}'.format(dst_width, dst_height, dst_path))

                out_img = resizeAR(out_img, width=dst_width, height=dst_height)
                video_out.write(out_img)

            if show_img:
                # out_img_disp = out_img
                out_img_disp = resizeAR(out_img, 1280)
                cv2.imshow(win_name, out_img_disp)
                k = cv2.waitKey(1 - pause_after_frame) & 0xFF
                if k == ord('q'):
                    _exit = 1
                    break
                elif k == 27:
                    break
                elif k == 32:
                    pause_after_frame = 1 - pause_after_frame

            sys.stdout.write('\rDone {:d}/{:d} frames '.format(frame_id - start_id, n_frames))
            sys.stdout.flush()

            if frame_id - start_id >= n_frames:
                break

        if _exit:
            break

        sys.stdout.write('\n')
        sys.stdout.flush()

        video_out.release()

        if show_img:
            cv2.destroyWindow(win_name)
示例#14
0
        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)

        fourcc = cv2.VideoWriter_fourcc(*codec)
        video_out = cv2.VideoWriter(out_video, fourcc, fps, (w, h))
        if video_out is None:
            raise IOError(
                'Output video file could not be opened: {}'.format(out_video))
        print('Writing video {}'.format(out_video))

    while True:
        ret, image = cap.read()
        if not ret:
            # print('frame {} could not be read from {}'.format(i, video))
            break

        _h, _w = image.shape[:2]
        if (_h, _w) != (h, w):
            print('resizing from {} to {}'.format((_h, _w), (h, w)))
            image = resizeAR(image, w, h)

        video_out.write(image)

    cap.release()

    video_dst = os.path.join(out_dir, video_fname + video_ext)
    print('moving {} to {}'.format(video, video_dst))
    shutil.move(video, video_dst)

video_out.release()