def main():
    params = {
        'interface_name': 'ethernet adapter ethernet',
        'utorrent_mode': 1,
        'sleep_time': 10,
        'wait_time': 10800,
        'post_wait_time': 10,
        'check_vpn_gap': 30,
        'max_vpn_wait_time': 600,
        'max_disconnects': 5,
        'vpn_path': 'C:/Users/Tommy/Desktop/purevpn.lnk',
        'tor_path': 'C:/Users/Tommy/Desktop/uTorrent.lnk',
        'settings_path': 'C:/Users/Tommy/AppData/Roaming/uTorrent/settings.dat',
        'vpn_proc': 'PureVPN.exe',
        'tor_proc': 'uTorrent.exe',
        'syatem_name': 'GT1K',
        'email_auth': [],
    }

    processArguments(sys.argv[1:], params)

    interface_name = params['interface_name']
    sleep_time = params['sleep_time']
    max_disconnects = params['max_disconnects']

    # hibernate_now = 0

    interface_name = interface_name.lower()

    n_disconnects = 0
    time_stamp = datetime.now().strftime("%y%m%d_%H%M%S")
    print("{} :: started".format(time_stamp))

    while True:
        if not is_connected(interface_name):
            time_stamp = datetime.now().strftime("%y%m%d_%H%M%S")
            n_disconnects += 1
            print("{} :: disconnect {}".format(time_stamp, n_disconnects))
        else:
            n_disconnects = 0

        if n_disconnects > max_disconnects:
            # hibernate_now = 1
            time_stamp = datetime.now().strftime("%y%m%d_%H%M%S")
            print("{} :: hibernating...".format(time_stamp))
            os.system("shutdown /h")
        try:
            time.sleep(sleep_time)
        except KeyboardInterrupt:
            exit()
Example #2
0
def main():
    params = {
        'files': '',
        'root_dir': [
            '.',
        ],
        'delete_file': 0,
        'db_file': '',
        'thresh': 0.1,
    }

    processArguments(sys.argv[1:], params)
    files = params['files']
    root_dir = params['root_dir']
    delete_file = params['delete_file']
    db_file = params['db_file']
    thresh = params['thresh']

    check_for_similar_images(files, root_dir, db_file, thresh=thresh)
Example #3
0
params = {
    'src_path': '.',
    'thresh': 0,
    'dst_path': '',
    'show_img': 0,
    'quality': 3,
    'recursive': 1,
    'border_type': 2,
    'resize': 0,
    'out_size': '',
    'out_ext': 'jpg',
}

if __name__ == '__main__':
    processArguments(sys.argv[1:], params)
    src_path = params['src_path']
    thresh = params['thresh']
    dst_path = params['dst_path']
    show_img = params['show_img']
    quality = params['quality']
    border_type = params['border_type']
    # 0: LHS, 1:RHS, 2: both
    resize = params['resize']
    out_size = params['out_size']
    out_ext = params['out_ext']
    recursive = params['recursive']

    if out_size:
        resize = 1
Example #4
0
def main():
    params = {
        'src_path': '.',
        'save_path': '',
        'img_ext': 'jpg',
        'show_img': 1,
        'del_src': 0,
        'start_id': 0,
        'width': 0,
        'height': 0,
        'fps': 30,
        # 'codec': 'FFV1',
        # 'ext': 'avi',
        'codec': 'H264',
        'ext': 'mkv',
        'out_postfix': '',
        'labels_col': 'red',
        'reverse': 0,
        'sub_seq_start_id': 0,
        'metric': 4,
        'thresh': -1,
        'order': 5,
        'frames_per_seq': 0,
    }

    processArguments(sys.argv[1:], params)
    _src_path = params['src_path']
    show_img = params['show_img']
    _start_id = params['start_id']
    _width = params['width']
    _height = params['height']
    reverse = params['reverse']
    labels_col = params['labels_col']
    metric = params['metric']
    _thresh = params['thresh']
    order = params['order']
    sub_seq_start_id = params['sub_seq_start_id']
    frames_per_seq = params['frames_per_seq']

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

    min_thresh = 0
    if metric == 0:
        sim_func = measure.compare_mse
        metric_type = 'MSE'
        cmp_func = np.greater
        max_thresh = 10000
    elif metric == 1:
        sim_func = measure.compare_ssim
        metric_type = 'SSIM'
        cmp_func = np.less
        max_thresh = 1
    elif metric == 2:
        sim_func = measure.compare_nrmse
        metric_type = 'NRMSE'
        cmp_func = np.greater
    elif metric == 3:
        sim_func = measure.compare_psnr
        metric_type = 'PSNR'
        cmp_func = np.less
    elif metric == 4:
        sim_func = functools.partial(cv2.matchTemplate,
                                     method=cv2.TM_CCORR_NORMED)
        metric_type = 'NCC'
        cmp_func = np.less
        max_thresh = 1
    elif metric == 5:
        sim_func = optical_flow_lk_fb
        metric_type = 'LK'
        cmp_func = np.greater
    elif metric == 6:
        sim_func = optical_flow_farneback_fb
        metric_type = 'Farneback'
        cmp_func = np.greater

    metric_type_ratio = f'{metric_type} Ratio'

    _src_path = os.path.abspath(_src_path)

    if any(_src_path.endswith(_ext) for _ext in vid_exts):
        print('Converting video to image sequences: {}'.format(_src_path))
        os.system('v2i {}'.format(_src_path))

        _src_path = os.path.join(
            os.path.dirname(_src_path),
            os.path.splitext(os.path.basename(_src_path))[0])

    if os.path.isdir(_src_path):
        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))
            ]
        else:
            src_paths = [_src_path]
    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')
        print('n_seq: {}'.format(n_seq))
    else:
        raise IOError('Invalid src_path: {}'.format(_src_path))

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

    labels_col_rgb = col_rgb[labels_col]
    plot_cols = [
        labels_col_rgb,
    ]

    for src_path in src_paths:

        src_path = os.path.abspath(src_path)

        start_id = _start_id
        thresh = _thresh
        seq_name = os.path.basename(src_path)

        print('Reading source images from: {}'.format(src_path))

        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')

        # print('src_files: {}'.format(src_files))

        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

        filename = src_files[start_id]
        file_path = os.path.join(src_path, filename)
        assert os.path.exists(
            file_path), f'Image file {file_path} does not exist'
        prev_image = cv2.imread(file_path)
        prev_image = cv2.cvtColor(prev_image, cv2.COLOR_BGR2GRAY)

        frame_id = start_id + 1
        pause_after_frame = 0

        if frames_per_seq > 0:
            split_indices = list(
                range(frames_per_seq, n_src_files, frames_per_seq))
        else:
            sim_list = []
            sim_ratio_list = []
            prev_sim = None
            # sub_seq_id = sub_seq_start_id
            # if thresh >= 0:
            #     dst_path = os.path.join(src_path, f'{sub_seq_id}')
            #     if not os.path.isdir(dst_path):
            #         os.makedirs(dst_path)
            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)
                assert os.path.exists(
                    file_path), f'Image file {file_path} does not exist'

                image = cv2.imread(file_path)
                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                if image.shape != prev_image.shape:
                    sim = min_thresh
                else:
                    sim = sim_func(image, prev_image)

                if prev_sim is not None:
                    s_ratio = (sim + 1) / (prev_sim + 1)
                else:
                    s_ratio = 1

                sim_list.append(sim)
                sim_ratio_list.append(s_ratio)

                prev_image = image
                prev_sim = sim

                # image = resizeAR(image, width, height)

                if show_img:
                    sim_plot = getPlotImage([
                        list(range(start_id, frame_id)),
                    ], [
                        sim_list,
                    ], plot_cols, metric_type, [
                        metric_type,
                    ], 'frame', metric_type)

                    sim_ratio_plot = getPlotImage([
                        list(range(start_id, frame_id)),
                    ], [
                        sim_ratio_list,
                    ], plot_cols, metric_type_ratio, [
                        metric_type_ratio,
                    ], 'frame', metric_type_ratio)

                    cv2.imshow('sim_plot', sim_plot)
                    cv2.imshow('sim_ratio_plot', sim_ratio_plot)
                    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

                # if thresh >= 0:
                #     if cmp_func(sim, thresh):
                #         sub_seq_id += 1
                #         print(f'sub_seq_id: {sub_seq_id} with sim: {sim}')
                #         dst_path = os.path.join(src_path, f'{sub_seq_id}')
                #         if not os.path.isdir(dst_path):
                #             os.makedirs(dst_path)
                #     dst_file_path = os.path.join(dst_path, filename)
                #     shutil.move(file_path, dst_file_path)

                frame_id += 1

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

                if frame_id >= n_src_files:
                    break

            sys.stdout.write('\n\n')
            sys.stdout.flush()
            """
            compensate for the 1-frame differential
            """
            sim_list.insert(0, sim_list[0])
            sim_ratio_list.insert(0, sim_ratio_list[0])

            sim_list = np.asarray(sim_list).squeeze()
            sim_ratio_list = np.asarray(sim_ratio_list).squeeze()

            split_indices = []

            if thresh < 0:
                if thresh == -1:
                    _data = sim_list
                    # c_max_index = argrelextrema(sim_list, cmp_func, order=order)
                    # plt.plot(sim_list)
                    # plt.scatter(c_max_index[0], sim_list[c_max_index[0]], linewidth=0.3, s=50, c='r')
                    # plt.show()
                elif thresh == -2:
                    _data = sim_ratio_list
                    # plt.plot(sim_ratio_list)
                    # plt.scatter(c_max_index[0], sim_ratio_list[c_max_index[0]], linewidth=0.3, s=50, c='r')
                    # plt.show()

                def update_order(_order):
                    nonlocal order, split_indices
                    order = _order
                    split_indices = argrelextrema(_data, cmp_func,
                                                  order=order)[0]
                    split_indices = [
                        k for k in split_indices if cmp_func(_data[k], thresh)
                    ]
                    scatter_plot = getPlotImage([
                        list(range(len(_data))),
                    ], [
                        _data,
                    ],
                                                plot_cols,
                                                metric_type, [
                                                    metric_type,
                                                ],
                                                'frame',
                                                metric_type,
                                                scatter=split_indices)
                    print(f'order: {order}')
                    cv2.imshow('scatter_plot', scatter_plot)

                def update_thresh(x):
                    nonlocal thresh, split_indices
                    thresh = min_thresh + float(max_thresh -
                                                min_thresh) / float(1000) * x
                    split_indices = argrelextrema(_data, cmp_func,
                                                  order=order)[0]
                    split_indices = [
                        k for k in split_indices if cmp_func(_data[k], thresh)
                    ]
                    scatter_plot = getPlotImage([
                        list(range(len(_data))),
                    ], [
                        _data,
                    ],
                                                plot_cols,
                                                metric_type, [
                                                    metric_type,
                                                ],
                                                'frame',
                                                metric_type,
                                                scatter=split_indices)
                    print(f'thresh: {thresh}')
                    cv2.imshow('scatter_plot', scatter_plot)

                update_order(order)
                cv2.createTrackbar('order', 'scatter_plot', order, 100,
                                   update_order)
                cv2.createTrackbar('threshold', 'scatter_plot', 0, 1000,
                                   update_thresh)

            else:
                if thresh == 0:
                    _data = sim_list
                else:
                    _data = sim_ratio_list
                max_thresh = np.max(_data)

                def update_thresh(x):
                    nonlocal thresh, split_indices
                    if x == 0:
                        return
                    thresh = min_thresh + float(max_thresh) / float(x)
                    split_indices = np.nonzero(cmp_func(_data, thresh))
                    scatter_plot = getPlotImage([
                        list(range(len(_data))),
                    ], [
                        _data,
                    ],
                                                plot_cols,
                                                metric_type, [
                                                    metric_type,
                                                ],
                                                'frame',
                                                metric_type,
                                                scatter=split_indices)
                    cv2.imshow('scatter_plot', scatter_plot)

                update_order(order)
                cv2.createTrackbar('threshold', 'scatter_plot', 0, 1000,
                                   update_thresh)

            while True:
                k = cv2.waitKey(0)
                print('k: {}'.format(k))
                if k == 13 or k == 27:
                    break

            cv2.destroyWindow('scatter_plot')

            if show_img:
                cv2.destroyAllWindows()

            if k == 27:
                break

            print(f'order: {order}')
            print(f'thresh: {thresh}')
            print(f'n_src_files: {n_src_files}')

        if n_src_files not in split_indices:
            split_indices.append(n_src_files)

        split_indices = list(split_indices)
        n_splits = len(split_indices)
        print(f'Splitting into {n_splits} sub sequences:\n{split_indices}')
        start_id = 0
        sub_seq_id = sub_seq_start_id
        for end_id in split_indices:
            print(
                f'sub_seq_id: {sub_seq_id} with start_id: {start_id}, end_id: {end_id}'
            )
            dst_path = os.path.join(src_path, f'{seq_name}_{sub_seq_id}')
            if not os.path.isdir(dst_path):
                os.makedirs(dst_path)

            for i in range(start_id, end_id):
                filename = src_files[i]
                src_file_path = os.path.join(src_path, filename)
                dst_file_path = os.path.join(dst_path, filename)
                shutil.move(src_file_path, dst_file_path)

            start_id = end_id
            sub_seq_id += 1
def main():
    params = {
        'files': '',
        'root_dir': '.',
        'delete_file': 0,
        'db_file': '',
        'file_type': '',
        'show_img': 0,
    }
    processArguments(sys.argv[1:], params)
    files = params['files']
    root_dir = params['root_dir']
    delete_file = params['delete_file']
    db_file = params['db_file']
    file_type = params['file_type']
    show_img = params['show_img']

    img_exts = ['.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif']
    vid_exts = [
        '.mp4', '.mkv', '.avi', '.wmv', '.3gp', '.webm', '.mpeg', '.mjpg'
    ]

    if file_type == 'img':
        print('Only searching for images')
        valid_exts = img_exts
    else:
        show_img = 0
        if file_type == 'vid':
            print('Only searching for videos')
            valid_exts = vid_exts
        elif file_type:
            valid_exts = [
                '.' + file_type,
            ]
        else:
            valid_exts = None

    src_file_gen = [[os.path.abspath(os.path.join(dirpath, f)) for f in files]
                    for (dirpath, dirnames,
                         files) in os.walk(root_dir, followlinks=True)]
    src_file_list = [item for sublist in src_file_gen for item in sublist]

    if valid_exts:
        print('Looking only for files with ext: {}'.format(valid_exts))
        src_file_list = [
            k for k in src_file_list
            if os.path.splitext(os.path.basename(k).lower())[1] in valid_exts
        ]

    n_files = len(src_file_list)
    print('n_files: {}'.format(n_files))

    all_stats = {k: os.stat(k) for k in src_file_list}
    # pprint(all_stats)

    all_stats = {
        '{}_{}'.format(st.st_ino, st.st_dev): k
        for k, st in all_stats.items()
    }

    # pprint(all_stats)

    n_files = len(all_stats)

    script_filename = inspect.getframeinfo(inspect.currentframe()).filename
    script_path = os.path.dirname(os.path.abspath(script_filename))

    db = {}
    if db_file:
        db_file = os.path.join(script_path, 'log', db_file)

        if os.path.isfile(db_file):
            print('Loading file hashes from {}'.format(db_file))
            db = pickle.load(open(db_file, "rb"))
        else:
            db_file_dir = os.path.dirname(db_file)
            if not os.path.isdir(db_file_dir):
                os.makedirs(db_file_dir)

    new_stats = [
        k for k in all_stats
        if k not in db or db[k][0] != os.path.getmtime(all_stats[k])
    ]

    n_new_files = len(new_stats)

    if new_stats:
        print('Computing hashes for {}/{} files ...'.format(
            n_new_files, n_files))
        db.update({
            k: (os.path.getmtime(all_stats[k]), getHash(all_stats[k]))
            for k in new_stats
        })
    else:
        print('No new files to compute hashes for')
    # src_file_hash_list = list(src_file_hash_dict.keys())
    # src_file_hash_set = set(src_file_hash_list)
    _new_stats = []

    if files:
        if os.path.isfile(files):
            print('Looking for duplicates of {}'.format(files))
            file_hash = getHash(files)
            duplicates = [(files, all_stats[k]) for k in all_stats
                          if db[k][1] == file_hash]
        elif os.path.isdir(files):
            _src_file_gen = [[
                os.path.abspath(os.path.join(_dirpath, f)) for f in _filenames
            ] for (_dirpath, _dirnames,
                   _filenames) in os.walk(files, followlinks=True)]
            _src_file_list = [
                item for sublist in _src_file_gen for item in sublist
            ]
            if valid_exts:
                print('Looking only for orig files with ext: {}'.format(
                    valid_exts))
                _src_file_list = [
                    k for k in _src_file_list if os.path.splitext(
                        os.path.basename(k).lower())[1] in valid_exts
                ]

            _all_stats = {k: os.stat(k) for k in _src_file_list}
            # pprint(all_stats)

            _all_stats = {
                '{}_{}'.format(st.st_ino, st.st_dev): k
                for k, st in _all_stats.items()
            }
            _n_files = len(_all_stats)

            _new_stats = [
                k for k in _all_stats
                if k not in db or db[k][0] != os.path.getmtime(_all_stats[k])
            ]
            _n_new_files = len(_new_stats)

            if _new_stats:
                print('Computing hashes for {}/{} orig files ...'.format(
                    _n_new_files, _n_files))
                db.update({
                    k:
                    (os.path.getmtime(_all_stats[k]), getHash(_all_stats[k]))
                    for k in _new_stats
                })

            print(
                'Looking for duplicates of {} files in {} among {} files in {}'
                .format(_n_files, files, n_files, root_dir))

            duplicates = [(_all_stats[k1], all_stats[k2]) for k1 in _all_stats
                          for k2 in all_stats
                          if db[k1][1] == db[k2][1] and k2 not in _all_stats]
    else:
        print('Looking for duplicates...')
        duplicates = [(all_stats[k[0]], all_stats[k[1]])
                      for k in itertools.combinations(all_stats, r=2)
                      if db[k[0]][1] == db[k[1]][1]]
    # duplicates = []
    # for pair in itertools.combinations(src_file_hash_list, r=2):
    #     if pair[0][0] == pair[1][0]:
    #         duplicates.append((pair[0][1], pair[1][1]))
    if duplicates:
        n_duplicates = len(duplicates)
        print('Found {} duplicates:'.format(n_duplicates))
        pprint(duplicates)
        if delete_file or show_img:
            _pause = 1
            for pair in duplicates:
                if show_img:
                    vis_img = np.concatenate(
                        (cv2.imread(pair[0]), cv2.imread(pair[1])), axis=1)
                    cv2.imshow('duplicates', vis_img)
                    k = cv2.waitKey(1 - _pause)
                    if k == 32:
                        _pause = 1 - _pause
                    elif k == 27:
                        break

                if delete_file:
                    del_path = pair[delete_file - 1]
                    if os.path.isfile(del_path):
                        print('Deleting {}'.format(del_path))
                        os.remove(del_path)

            if delete_file:
                print('Deleted {} duplicates'.format(n_duplicates))

    else:
        print('No duplicates found')

    if db_file and (new_stats or _new_stats):
        print('Saving hash db to: {}'.format(db_file))
        pickle.dump(db, open(db_file, "wb"))
Example #6
0
def main():
    params = {
        'seq_prefix': [
            'Seq',
        ],
        'seq_prefix_filter': '',
        'seq_prefix_ext': '',
        'seq_root_dir': '.',
        'seq_start_id': -1,
        'shuffle_files': 0,
        'filename_fmt': 0,
        'write_log': 1,
        'target_ext': '',
        'recursive': 0,
    }
    processArguments(sys.argv[1:], params)
    seq_prefix = params['seq_prefix']
    seq_prefix_filter = params['seq_prefix_filter']
    seq_prefix_ext = params['seq_prefix_ext']
    _seq_root_dir = params['seq_root_dir']
    seq_start_id = params['seq_start_id']
    shuffle_files = params['shuffle_files']
    filename_fmt = params['filename_fmt']
    write_log = params['write_log']
    target_ext = params['target_ext']
    recursive = params['recursive']

    # seq_prefix = 'Seq'
    # seq_root_dir = '.'
    # seq_start_id = 1
    # shuffle_files = 1
    # filename_fmt = 0
    # write_log = 1
    # target_ext = ''
    #
    # arg_id = 1
    # if len(sys.argv) > arg_id:
    #     seq_prefix = sys.argv[arg_id]
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     seq_start_id = int(sys.argv[arg_id])
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     shuffle_files = int(sys.argv[arg_id])
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     filename_fmt = int(sys.argv[arg_id])
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     _seq_root_dir = sys.argv[arg_id]
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     write_log = int(sys.argv[arg_id])
    #     arg_id += 1
    # if len(sys.argv) > arg_id:
    #     target_ext = sys.argv[arg_id]
    #     arg_id += 1

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

    if len(seq_prefix) == 1:
        seq_prefix = seq_prefix[0]
    elif len(seq_prefix) == 2:
        seq_prefix, seq_prefix_filter = seq_prefix
    elif len(seq_prefix) == 3:
        seq_prefix, seq_prefix_filter, seq_prefix_ext = seq_prefix

    print('seq_prefix: {}'.format(seq_prefix))
    print('seq_prefix_filter: {}'.format(seq_prefix_filter))
    print('seq_prefix_ext: {}'.format(seq_prefix_ext))

    script_filename = inspect.getframeinfo(inspect.currentframe()).filename
    script_path = os.path.dirname(os.path.abspath(script_filename))

    if seq_start_id < 0:
        # extract seq_start_id from seq_prefix
        if os.path.isdir(seq_prefix):
            src_dir = os.path.abspath(seq_prefix)
            print(('Looking for sequence prefix in {}'.format(
                os.path.abspath(src_dir))))
            if recursive:
                print('Searching recursively')
                src_file_gen = [[
                    f for f in filenames if f != 'Thumbs.db'
                ] for (dirpath, dirnames,
                       filenames) in os.walk(src_dir, followlinks=True)
                                if dirpath != os.getcwd()]
                src_file_names = [
                    item for sublist in src_file_gen for item in sublist
                ]
            else:
                src_file_names = [
                    f for f in os.listdir(src_dir)
                    if os.path.isfile(os.path.join(src_dir, f))
                    and f != 'Thumbs.db'
                ]

            if seq_prefix_filter:
                print(('Restricting search to files containing:{}'.format(
                    seq_prefix_filter)))
                src_file_names = [
                    k for k in src_file_names if seq_prefix_filter in k
                ]

            if seq_prefix_ext:
                print(('Restricting search to files with extension:{}'.format(
                    seq_prefix_ext)))
                src_file_names = [
                    k for k in src_file_names if k.endswith(seq_prefix_ext)
                ]

            src_file_names.sort(key=sortKey)
            # print 'src_file_names: {}'.format(src_file_names)

            seq_prefix = os.path.splitext(src_file_names[-1])[0]
            print(('Found sequence prefix {}'.format(seq_prefix)))

        split_str = seq_prefix.split('_')
        try:
            seq_start_id = int(split_str[-1]) + 1
            seq_prefix = split_str[0]
        except ValueError:
            seq_start_id = 1
        for _str in split_str[1:-1]:
            seq_prefix = '{}_{}'.format(seq_prefix, _str)

    print('seq_prefix: {:s}'.format(seq_prefix))
    print('seq_start_id: {:d}'.format(seq_start_id))
    print('shuffle_files: {:d}'.format(shuffle_files))
    print('file_fmt: {:d}'.format(filename_fmt))

    if target_ext:
        print('target_ext: {:s}'.format(target_ext))

    _seq_root_dir = os.path.abspath(_seq_root_dir)

    if os.path.isdir(_seq_root_dir):
        seq_root_dirs = [_seq_root_dir]
    elif os.path.isfile(_seq_root_dir):
        seq_root_dirs = [
            x.strip() for x in open(_seq_root_dir).readlines() if x.strip()
        ]
    else:
        raise IOError('Invalid seq_root_dir: {}'.format(_seq_root_dir))

    # if write_log:
    #     log_dir = 'rseq_log'
    #     if not os.path.isdir(log_dir):
    #         os.makedirs(log_dir)
    #     print('Saving log to {}'.format(log_dir))
    if write_log:
        log_dir = os.path.join(script_path, 'log')
        if not os.path.isdir(log_dir):
            os.makedirs(log_dir)
        log_file = os.path.join(log_dir, 'rseq_log.txt')
        print(('Saving log to {}'.format(log_file)))
        log_fid = open(log_file, 'w')

    for seq_root_dir in seq_root_dirs:
        print('Processing: {}'.format(seq_root_dir))
        src_file_names = [
            f for f in os.listdir(seq_root_dir)
            if os.path.isfile(os.path.join(seq_root_dir, f))
            and f != 'rseq_log.txt' and f != 'Thumbs.db'
        ]
        if shuffle_files:
            print('Shuffling files...')
            random.shuffle(src_file_names)
        else:
            src_file_names.sort(key=sortKey)

        seq_id = seq_start_id
        file_count = 1
        n_files = len(src_file_names)

        for src_fname in src_file_names:
            filename, file_extension = os.path.splitext(src_fname)

            if target_ext and file_extension[1:] != target_ext:
                print(('Ignoring file {} with invalid extension {}'.format(
                    src_fname, file_extension)))
                continue

            src_path = os.path.join(seq_root_dir, src_fname)

            if is_hidden(src_path):
                print('Skipping hidden file: {}'.format(src_path))
                continue

            if filename_fmt == 0:
                dst_fname = '{:s}_{:d}{:s}'.format(seq_prefix, seq_id,
                                                   file_extension)
            else:
                dst_fname = '{:s}{:06d}{:s}'.format(seq_prefix, seq_id,
                                                    file_extension)
            dst_path = os.path.join(seq_root_dir, dst_fname)

            if src_path != dst_path:
                while os.path.exists(dst_path):
                    seq_id += 1
                    if filename_fmt == 0:
                        dst_fname = '{:s}_{:d}{:s}'.format(
                            seq_prefix, seq_id, file_extension)
                    else:
                        dst_fname = '{:s}{:06d}{:s}'.format(
                            seq_prefix, seq_id, file_extension)
                    dst_path = os.path.join(seq_root_dir, dst_fname)
                try:
                    os.rename(src_path, dst_path)
                except WindowsError as e:
                    print('Renaming of {} failed: {}'.format(src_path, e))
            if write_log:
                log_fid.write('{}\t{}\n'.format(src_fname, dst_fname))
            seq_id += 1
            if file_count % 10 == 0 or file_count == n_files:
                print('Done {:d}/{:d}'.format(file_count, n_files))
            file_count += 1
        if write_log:
            log_fid.close()
Example #7
0
def main():
    params = {
        'max_t': 120,
        'relative': 0,
    }
    processArguments(sys.argv[1:], params)

    max_t = params['max_t']

    win_titles = ['Google Chrome']
    try:
        orig_x, orig_y = win32api.GetCursorPos()
        print('GetCursorPos x: {}'.format(orig_x))
        print('GetCursorPos y: {}'.format(orig_y))

        win32gui.EnumWindows(foreach_window, None)

        # for i in range(len(titles)):
        #     print(titles[i])

        target_title = [k[1] for k in titles if all(title in k[1] for title in win_titles)]
        # print('target_title: {}'.format(target_title))

        if not target_title:
            raise IOError('Window with win_titles: {} not found'.format(win_titles))

        target_title = target_title[0]

        target_handle = win32gui.FindWindow(None, target_title)
        rect = win32gui.GetWindowRect(target_handle)

        x = int((rect[0] + rect[2]) / 2)
        y = int((rect[1] + rect[3]) / 2)

        # active_handle = win32gui.GetForegroundWindow()
        # target_title = win32gui.GetWindowText(active_handle)

        print('target_title: {}'.format(target_title))
        print('rect: {}'.format(rect))
        print('x: {}'.format(x))
        print('y: {}'.format(y))

        try:
            app = application.Application().connect(title=target_title, found_index=0)
        except BaseException as e:
            print('Failed to connect to app for window {}: {}'.format(target_title, e))
            exit(0)
        try:
            app_win = app.window(title=target_title)
        except BaseException as e:
            print('Failed to access app window for {}: {}'.format(target_title, e))
            exit(0)

    except BaseException as e:
        print('BaseException: {}'.format(e))
        return

    start_t = time.time()
    while True:
        try:
            app_win.type_keys("{PGDN}")
        except BaseException as e:
            print('BaseException: {}'.format(e))
            break
        end_t = time.time()
        time_elapsed = end_t - start_t
        if time_elapsed > max_t:
            break
        print(time_elapsed)
Example #8
0
def main():
    params = {
        'seq_prefix': ['Seq', ],
        'seq_prefix_filter': '',
        'seq_prefix_ext': '',
        'seq_root_dir': '.',
        'seq_start_id': -1,
        'shuffle_files': 0,
        'filename_fmt': 0,
        'write_log': 1,
        'target_ext': '.jpg',
        'prefix_len': 11,
        'recursive': 0,
        'recursive_src': 1,
        'suffix_mode': 0,
    }
    processArguments(sys.argv[1:], params)
    seq_prefix = params['seq_prefix']
    seq_prefix_filter = params['seq_prefix_filter']
    seq_prefix_ext = params['seq_prefix_ext']
    _seq_root_dir = params['seq_root_dir']
    seq_start_id = params['seq_start_id']
    shuffle_files = params['shuffle_files']
    filename_fmt = params['filename_fmt']
    write_log = params['write_log']
    target_ext = params['target_ext']
    prefix_len = params['prefix_len']
    recursive = params['recursive']
    recursive_src = params['recursive_src']
    suffix_mode = params['suffix_mode']

    excluded_files = ['rseq_log.txt', 'Thumbs.db']

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

    if len(seq_prefix) == 1:
        seq_prefix = seq_prefix[0]
    elif len(seq_prefix) == 2:
        seq_prefix, seq_prefix_filter = seq_prefix
    elif len(seq_prefix) == 3:
        seq_prefix, seq_prefix_filter, seq_prefix_ext = seq_prefix

    print('seq_prefix: {}'.format(seq_prefix))
    print('seq_prefix_filter: {}'.format(seq_prefix_filter))
    print('seq_prefix_ext: {}'.format(seq_prefix_ext))

    script_filename = inspect.getframeinfo(inspect.currentframe()).filename
    script_path = os.path.dirname(os.path.abspath(script_filename))

    prefix_file_paths = None

    if target_ext:
        print('target_ext: {:s}'.format(target_ext))

    _seq_root_dir = os.path.abspath(_seq_root_dir)

    if os.path.isdir(_seq_root_dir):
        seq_root_dirs = [_seq_root_dir]
    elif os.path.isfile(_seq_root_dir):
        seq_root_dirs = [x.strip() for x in open(_seq_root_dir).readlines() if x.strip()]
    else:
        raise IOError('Invalid seq_root_dir: {}'.format(_seq_root_dir))

    if write_log:
        log_dir = os.path.join(script_path, 'log')
        if not os.path.isdir(log_dir):
            os.makedirs(log_dir)
        log_file = os.path.join(log_dir, 'rmpf_log.txt')
        print(('Saving log to {}'.format(log_file)))
        log_fid = open(log_file, 'w')

    all_src_file_names = []
    all_src_file_names_flat = []
    for seq_root_dir in seq_root_dirs:
        seq_root_dir = os.path.abspath(seq_root_dir)

        print('Processing: {}'.format(seq_root_dir))
        if recursive_src:
            src_file_gen = [[os.path.join(dirpath, f) for f in filenames if f not in excluded_files]
                            for (dirpath, dirnames, filenames) in os.walk(seq_root_dir, followlinks=False)]
            src_file_names = [item for sublist in src_file_gen for item in sublist]

        else:
            src_file_names = [os.path.join(seq_root_dir, f) for f in os.listdir(seq_root_dir)
                              if os.path.isfile(os.path.join(seq_root_dir, f))
                              and f not in excluded_files]
        if shuffle_files:
            print('Shuffling files...')
            random.shuffle(src_file_names)
        else:
            src_file_names.sort(key=sortKey)

        all_src_file_names.append(src_file_names)
        all_src_file_names_flat += src_file_names

    for src_file_names in all_src_file_names:
        file_count = 1
        n_files = len(src_file_names)

        for src_file_path in src_file_names:
            src_dir = os.path.dirname(src_file_path)
            src_fname = os.path.basename(src_file_path)

            src_fname_no_ext, src_ext = os.path.splitext(src_fname)

            if target_ext and src_ext != target_ext:
                print(('Ignoring file {} with invalid extension {}'.format(src_fname, src_ext)))
                continue

            # src_path = os.path.join(seq_root_dir, src_fname)
            src_path = src_file_path

            if is_hidden(src_path):
                print('Skipping hidden file: {}'.format(src_path))
                continue
            if suffix_mode:
                dst_fname_noext = src_fname_no_ext[:-prefix_len]
            else:
                dst_fname_noext = src_fname_no_ext[prefix_len:]

            # dst_fname_noext, dst_fname_ext = os.path.splitext(dst_fname)

            dst_fname = dst_fname_noext + src_ext

            dst_path = os.path.join(src_dir, dst_fname)
            if src_path != dst_path:

                seq_id = 0
                while os.path.exists(dst_path):
                    seq_id += 1
                    dst_fname = '{:s}_{:06d}{:s}'.format(dst_fname_noext, seq_id, src_ext)
                    dst_path = os.path.join(src_dir, dst_fname)

                try:
                    os.rename(src_path, dst_path)
                except WindowsError as e:
                    print('Renaming of {} failed: {}'.format(src_path, e))

            print('{} --> {}\n'.format(src_fname, dst_fname))
            if write_log:
                log_fid.write('{}\t{}\n'.format(src_fname, dst_fname))

            if file_count % 10 == 0 or file_count == n_files:
                print('Done {:d}/{:d}'.format(file_count, n_files))

            file_count += 1
        if write_log:
            log_fid.close()
Example #9
0
def main():
    params = {
        'seq_prefix': [
            'Seq',
        ],
        'seq_prefix_filter': '',
        'seq_prefix_ext': '',
        'seq_root_dir': '.',
        'seq_start_id': -1,
        'shuffle_files': 0,
        'filename_fmt': 0,
        'write_log': 1,
        'target_ext': '',
        'recursive': 0,
        'recursive_src': 1,
    }
    processArguments(sys.argv[1:], params)
    seq_prefix = params['seq_prefix']
    seq_prefix_filter = params['seq_prefix_filter']
    seq_prefix_ext = params['seq_prefix_ext']
    _seq_root_dir = params['seq_root_dir']
    seq_start_id = params['seq_start_id']
    shuffle_files = params['shuffle_files']
    filename_fmt = params['filename_fmt']
    write_log = params['write_log']
    target_ext = params['target_ext']
    recursive = params['recursive']
    recursive_src = params['recursive_src']

    excluded_files = ['rseq_log.txt', 'Thumbs.db']

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

    if len(seq_prefix) == 1:
        seq_prefix = seq_prefix[0]
    elif len(seq_prefix) == 2:
        seq_prefix, seq_prefix_filter = seq_prefix
    elif len(seq_prefix) == 3:
        seq_prefix, seq_prefix_filter, seq_prefix_ext = seq_prefix

    print('seq_prefix: {}'.format(seq_prefix))
    print('seq_prefix_filter: {}'.format(seq_prefix_filter))
    print('seq_prefix_ext: {}'.format(seq_prefix_ext))

    script_filename = inspect.getframeinfo(inspect.currentframe()).filename
    script_path = os.path.dirname(os.path.abspath(script_filename))

    prefix_file_paths = None

    if seq_start_id < 0:
        # extract seq_start_id from seq_prefix
        if os.path.isdir(seq_prefix):
            src_dir = os.path.abspath(seq_prefix)
            print(('Looking for sequence prefix in {}'.format(
                os.path.abspath(src_dir))))
            if recursive:
                print('Searching recursively')
                src_file_gen = [[
                    os.path.join(dirpath, f) for f in filenames
                    if f != 'Thumbs.db'
                ] for (dirpath, dirnames,
                       filenames) in os.walk(src_dir, followlinks=True)
                                if dirpath != os.getcwd()]
                prefix_file_paths = [
                    item for sublist in src_file_gen for item in sublist
                ]
            else:
                prefix_file_paths = [
                    os.path.join(src_dir, f) for f in os.listdir(src_dir)
                    if os.path.isfile(os.path.join(src_dir, f))
                    and f != 'Thumbs.db'
                ]

            if seq_prefix_filter:
                print(('Restricting search to files containing: {}'.format(
                    seq_prefix_filter)))
                prefix_file_paths = [
                    k for k in prefix_file_paths
                    if seq_prefix_filter in os.path.basename(k)
                ]

            if seq_prefix_ext:
                print(('Restricting search to files with extension: {}'.format(
                    seq_prefix_ext)))
                prefix_file_paths = [
                    k for k in prefix_file_paths
                    if os.path.basename(k).endswith(seq_prefix_ext)
                ]

        #     seq_prefix = os.path.splitext(prefix_file_paths[-1])[0]
        #     print(('Found sequence prefix {}'.format(seq_prefix)))
        #
        # split_str = seq_prefix.split('_')
        # try:
        #     seq_start_id = int(split_str[-1]) + 1
        # except ValueError:
        #     seq_start_id = 1
        # else:
        #     seq_prefix = '_'.join(split_str[:-1])
        # # for _str in split_str[1:-1]:
        # #     seq_prefix = '{}_{}'.format(seq_prefix, _str)
        else:
            seq_start_id = 0

    print('seq_prefix: {:s}'.format(seq_prefix))
    print('seq_start_id: {:d}'.format(seq_start_id))
    print('shuffle_files: {:d}'.format(shuffle_files))
    print('file_fmt: {:d}'.format(filename_fmt))

    if target_ext:
        print('target_ext: {:s}'.format(target_ext))

    _seq_root_dir = os.path.abspath(_seq_root_dir)

    if os.path.isdir(_seq_root_dir):
        seq_root_dirs = [_seq_root_dir]
    elif os.path.isfile(_seq_root_dir):
        seq_root_dirs = [
            x.strip() for x in open(_seq_root_dir).readlines() if x.strip()
        ]
    else:
        raise IOError('Invalid seq_root_dir: {}'.format(_seq_root_dir))

    # if write_log:
    #     log_dir = 'rseq_log'
    #     if not os.path.isdir(log_dir):
    #         os.makedirs(log_dir)
    #     print('Saving log to {}'.format(log_dir))
    if write_log:
        log_dir = os.path.join(script_path, 'log')
        if not os.path.isdir(log_dir):
            os.makedirs(log_dir)
        log_file = os.path.join(log_dir, 'rseq_log.txt')
        print(('Saving log to {}'.format(log_file)))
        log_fid = open(log_file, 'w')

    all_src_file_names = []
    all_src_file_names_flat = []
    for seq_root_dir in seq_root_dirs:
        seq_root_dir = os.path.abspath(seq_root_dir)

        print('Processing: {}'.format(seq_root_dir))
        if recursive_src:
            src_file_gen = [[
                os.path.join(dirpath, f) for f in filenames
                if f not in excluded_files
            ] for (dirpath, dirnames,
                   filenames) in os.walk(seq_root_dir, followlinks=False)]
            src_file_names = [
                item for sublist in src_file_gen for item in sublist
            ]

        else:
            src_file_names = [
                os.path.join(seq_root_dir, f) for f in os.listdir(seq_root_dir)
                if os.path.isfile(os.path.join(seq_root_dir, f))
                and f not in excluded_files
            ]
        if shuffle_files:
            print('Shuffling files...')
            random.shuffle(src_file_names)
        else:
            src_file_names.sort(key=sortKey)

        all_src_file_names.append(src_file_names)
        all_src_file_names_flat += src_file_names

    if prefix_file_paths is not None:
        # exclude files to be renamed from files used to generate prefix
        prefix_file_paths = list(
            set(prefix_file_paths).difference(set(all_src_file_names_flat)))

        prefix_file_names = [os.path.basename(k) for k in prefix_file_paths]

        def argsort(seq):
            # http://stackoverflow.com/questions/3071415/efficient-method-to-calculate-the-rank-vector-of-a-list-in
            # -python
            return sorted(range(len(seq)), key=lambda x: sortKey(seq[x]))

        sort_idx = argsort(prefix_file_names)

        prefix_file_names = [prefix_file_names[k] for k in sort_idx]
        prefix_file_paths = [prefix_file_paths[k] for k in sort_idx]

        # prefix_file_names.sort(key=sortKey)
        # print 'prefix_file_paths: {}'.format(prefix_file_paths)

        seq_prefix = os.path.splitext(prefix_file_names[-1])[0]
        print(('Found sequence prefix {}'.format(seq_prefix)))

        split_str = seq_prefix.split('_')
        try:
            seq_start_id = int(split_str[-1]) + 1
        except ValueError:
            seq_start_id = 1
        else:
            seq_prefix = '_'.join(split_str[:-1])
        # for _str in split_str[1:-1]:
        #     seq_prefix = '{}_{}'.format(seq_prefix, _str)

    for src_file_names in all_src_file_names:
        seq_id = seq_start_id
        file_count = 1
        n_files = len(src_file_names)

        for src_file_path in src_file_names:
            src_dir = os.path.dirname(src_file_path)
            src_fname = os.path.basename(src_file_path)

            if recursive_src and os.path.normpath(src_dir) != os.path.normpath(
                    seq_root_dir):
                rel_path = os.path.relpath(src_dir, seq_root_dir)
                _seq_prefix = '{}_{}'.format(seq_prefix,
                                             rel_path.replace(os.sep, '_'))
                # print('src_dir: {}'.format(src_dir))
                # print('rel_path: {}'.format(rel_path))
            else:
                _seq_prefix = seq_prefix

            filename, file_extension = os.path.splitext(src_fname)

            if target_ext and file_extension[1:] != target_ext:
                print(('Ignoring file {} with invalid extension {}'.format(
                    src_fname, file_extension)))
                continue

            # src_path = os.path.join(seq_root_dir, src_fname)
            src_path = src_file_path

            if is_hidden(src_path):
                print('Skipping hidden file: {}'.format(src_path))
                continue

            if filename_fmt == 0:
                dst_fname = '{:s}_{:06d}{:s}'.format(_seq_prefix, seq_id,
                                                     file_extension)
            else:
                dst_fname = '{:s}{:06d}{:s}'.format(_seq_prefix, seq_id,
                                                    file_extension)
            dst_path = os.path.join(src_dir, dst_fname)

            if src_path != dst_path:
                while os.path.exists(dst_path):
                    seq_id += 1
                    if filename_fmt == 0:
                        dst_fname = '{:s}_{:d}{:s}'.format(
                            _seq_prefix, seq_id, file_extension)
                    else:
                        dst_fname = '{:s}{:06d}{:s}'.format(
                            _seq_prefix, seq_id, file_extension)
                    dst_path = os.path.join(src_dir, dst_fname)
                try:
                    os.rename(src_path, dst_path)
                except WindowsError as e:
                    print('Renaming of {} failed: {}'.format(src_path, e))
            if write_log:
                log_fid.write('{}\t{}\n'.format(src_fname, dst_fname))
            seq_id += 1
            if file_count % 10 == 0 or file_count == n_files:
                print('Done {:d}/{:d}'.format(file_count, n_files))
            file_count += 1
        if write_log:
            log_fid.close()
Example #10
0
def main():

    params = {
        'win_title': 'PDF-XChange Editor',
        'mode': 0,
        'wait_t': 10,
        'scp_dst': '',
        'auth_path': '',
        'dst_path': '.',
        'scp_path': '.',
        'scp_name': 'grs',
    }
    processArguments(sys.argv[1:], params)
    win_title = params['win_title']
    # mode = params['mode']
    # wait_t = params['wait_t']
    # scp_dst = params['scp_dst']
    # auth_path = params['auth_path']
    # dst_path = params['dst_path']
    # scp_path = params['scp_path']
    # scp_name = params['scp_name']

    EnumWindows = ctypes.windll.user32.EnumWindows
    EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool,
                                         ctypes.POINTER(ctypes.c_int),
                                         ctypes.POINTER(ctypes.c_int))
    GetWindowText = ctypes.windll.user32.GetWindowTextW
    GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
    IsWindowVisible = ctypes.windll.user32.IsWindowVisible

    # Form1.SetFocus()

    # if mode == 0:
    #     data_type = 'filename (from)'
    # elif mode == 1:
    #     data_type = 'filename (to)'
    # elif mode == 2:
    #     data_type = 'log'
    # else:
    #     raise AssertionError('Invalid mode: {}'.format(mode))

    # while True:
    #     k = input('Enter {}\n'.format(data_type))

    # x, y = win32api.GetCursorPos()

    # EnumWindows(EnumWindowsProc(foreach_window), 0)

    titles = []

    def foreach_window(hwnd, lParam):
        if IsWindowVisible(hwnd):
            length = GetWindowTextLength(hwnd)
            buff = ctypes.create_unicode_buffer(length + 1)
            GetWindowText(hwnd, buff, length + 1)
            titles.append((hwnd, buff.value))
        return True

    win32gui.EnumWindows(foreach_window, None)

    # for i in range(len(titles)):
    #     print(titles[i])

    target_title = [k[1] for k in titles if win_title in k[1]]
    print('target_title: {}'.format(target_title))

    if not target_title:
        print('Window with win_title: {} not found'.format(win_title))
        return

    target_title = target_title[0]
    # print('target_title: {}'.format(target_title))

    try:
        app = application.Application().connect(title=target_title,
                                                found_index=0)
    except BaseException as e:
        print('Failed to connect to app for window {}: {}'.format(
            target_title, e))
        return
    try:
        app_win = app.window(title=target_title)
    except BaseException as e:
        print('Failed to access app window for {}: {}'.format(target_title, e))
        return

    app_win.type_keys("^Q")

    if app_win.exists():
        dialogs = app.windows()
        print(dialogs)

        diag_win = app.window(handle=dialogs[0])
        # print(diag_win)

        diag_win.type_keys("~")
Example #11
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': 'XVID',
        'ext': 'avi',
        # 'codec': 'H264',
        # 'ext': 'mkv',
        'out_postfix': '',
        'reverse': 0,
        'save_video': 0,
        'min_free_space': 100,
    }

    processArguments(sys.argv[1:], params)
    src_path = params['src_path']
    min_free_space = params['min_free_space']
    save_video = params['save_video']
    codec = params['codec']
    ext = params['ext']
    fps = params['fps']

    img_exts = ('.jpg', '.bmp', '.jpeg', '.png', '.tif', '.tiff', '.webp')

    # existing_images = {}
    image_pause = {}
    video_writers = {}
    _pause = 1

    src_path = os.path.abspath(src_path)
    script_dir = os.path.dirname(os.path.realpath(__file__))
    log_path = os.path.join(script_dir, 'siif_log.txt')
    with open(log_path, 'w') as fid:
        fid.write(src_path)

    os.environ["SIIF_DUMP_IMAGE_PATH"] = src_path

    read_img_path = os.path.join(src_path, "read")

    if os.path.exists(read_img_path):
        clear_dir(read_img_path)
        # shutil.rmtree(read_img_path)
    else:
        os.makedirs(read_img_path)

    print('SIIF started in {}'.format(src_path))

    img_id = 0
    reset_program = exit_program = 0
    while True:
        _src_files = [
            k for k in os.listdir(src_path)
            if os.path.splitext(k.lower())[1] in img_exts and '~' not in k
        ]
        for _src_file in _src_files:
            _src_path = os.path.join(src_path, _src_file)
            # mod_time = os.path.getmtime(_src_path)
            _src_file_no_ext = os.path.splitext(_src_file)[0]
            try:
                _src_file_id, _src_file_timestamp = _src_file_no_ext.split(
                    '___')
            except ValueError as e:
                print('Invalid _src_file: {} with _src_file_no_ext: {}'.format(
                    _src_file, _src_file_no_ext))

            # if _src_file_id not in existing_images or existing_images[_src_file_id] != _src_file_timestamp:
            #     existing_images[_src_file] = _src_file_timestamp

            # print('reading {} with time: {}'.format(_src_file_id, _src_file_timestamp))

            read_success = 1

            try:
                img = cv2.imread(_src_path)
            except cv2.error as e:
                print('Error reading image {} with ID: {}: {}'.format(
                    _src_file, _src_file_id, e))
                read_success = 0

            if img is None:
                print('Failed to read image {} with ID: {}'.format(
                    _src_file, _src_file_id))
                read_success = 0

            # out_path = _src_path+'.done'
            # print('writing to {}'.format(out_path))
            # with open(out_path, 'w') as fid:
            #     fid.write('siif')
            #     fid.close()

            _dst_path = os.path.join(read_img_path,
                                     os.path.basename(_src_path))

            try:
                # os.remove(_src_path)
                shutil.move(_src_path, _dst_path)
            except PermissionError as e:
                print('Failed to move image {} :: {}'.format(_src_path, e))

            if not read_success:
                continue

            try:
                cv2.imshow(_src_file_id, img)
            except cv2.error as e:
                print('Error showing image {} with ID: {}: {}'.format(
                    _src_file, _src_file_id, e))
                continue

            if _src_file_id not in image_pause:
                image_pause[_src_file_id] = _pause

            if save_video:
                if _src_file_id not in video_writers:
                    time_stamp = datetime.now().strftime("%y%m%d_%H%M%S_%f")
                    out_fname = '{}_{}.{}'.format(_src_file_id, time_stamp,
                                                  ext)

                    frame_size = img.shape[:2][::-1]

                    writer = cv2.VideoWriter()
                    writer.open(filename=out_fname,
                                fourcc=cv2.VideoWriter_fourcc(*codec),
                                fps=fps,
                                frameSize=frame_size)

                    assert writer.isOpened(
                    ), 'Video file {:s} could not be opened'.format(out_fname)

                    video_writers[_src_file_id] = writer

                    print('Saving output video for {} to {}  of size {} x {}'.
                          format(_src_file_id, out_fname, *frame_size))

                video_writers[_src_file_id].write(img)

            k = cv2.waitKey(1 - image_pause[_src_file_id])
            if k == 27:
                print('resetting')
                reset_program = 1
                break
            elif k == ord('q'):
                exit_program = 1
                print('exiting')
                break
            elif k == 32:
                _pause = 1 - _pause
                for _src_file_id in image_pause:
                    image_pause[_src_file_id] = _pause
                print('image_pause: {}'.format(image_pause))
            elif k == ord('k'):
                cv2.destroyWindow(_src_file_id)
                del image_pause[_src_file_id]
            elif k == ord('p'):
                image_pause[_src_file_id] = 1 - image_pause[_src_file_id]
                print('image_pause: {}'.format(image_pause))
            elif k == ord('P'):
                image_pause = {
                    k:
                    1 - image_pause[k] if k != _src_file_id else image_pause[k]
                    for k in image_pause
                }
                print('image_pause: {}'.format(image_pause))

            img_id += 1

            # print('image_pause: {}'.format(image_pause))

            if img_id % 10 == 0:
                free_space = get_free_space_mb(src_path)
                # print('free_space {}'.format(free_space))
                if free_space < min_free_space:
                    # print('Free space running low. Press any key to clear the backup directory')
                    # cv2.waitKey(0)
                    clear_dir(read_img_path)

            # del_images = []
            # for existing_image in existing_images.keys():
            #     if existing_image not in _src_files:
            #         cv2.destroyWindow(existing_image)
            #         del_images.append(existing_image)
            #
            # for del_image in del_images:
            #     del existing_images[del_image]

        if exit_program or reset_program:
            break

        k = cv2.waitKey(1)
        if k == 27:
            print('resetting global')
            break
        elif k == ord('q'):
            print('exiting global')
            exit_program = 1
            break

    for _src_file_id in image_pause.keys():
        cv2.destroyWindow(_src_file_id)

    if save_video:
        for _src_file_id in video_writers.keys():
            video_writers[_src_file_id].release()

    if exit_program:
        return False

    return True