def save_vid_worker(img_files, view_i, save_dir, tag, img_size, fps):
    # port_data soted in view order
    create_dir_if_not_exists(save_dir)
    output_vid_name = "{}_view{}.mov".format(tag, view_i)
    frame_count = len(img_files)
    out_shape_cv = img_size[:2]
    # out_shape_cv = np_shape_to_cv(img_size[:2])
    output_vid_name = os.path.join(save_dir, output_vid_name)
    with vid_writer(output_vid_name, fps, out_shape_cv, frame_count) as vid:
        for im_f in img_files:
            img = cv2.imread(im_f)
            vid.write(img)
def save_img_worker(ports, save_dir, tag, port_data_queue,
                    result_file_name_que):
    # port_data soted in view order
    create_dir_if_not_exists(save_dir)
    while True:
        port_data = port_data_queue.get()
        files_names = {}
        for view_i, p in enumerate(ports):
            f_cnt = port_data[p]["num"]
            frame = port_data[p]["frame"]
            filenamne = "frame{}_{}_view{}.png".format(f_cnt, tag, view_i)
            filenamne = os.path.join(save_dir, filenamne)
            files_names[p] = filenamne
            cv2.imwrite(filenamne, frame)
        result_file_name_que.put(files_names)
Beispiel #3
0
def set_log_file(log_file, mpi_only_log_rank=None):
    """
        mpi_only_rank(int or NOne), mpi rank only to log to the file
    """
    global _file_handler
    # create file handler which logs even debug messages
    # always overte file unless multi mpi log
    mode = "a" if mpi_only_log_rank is None else "w"
    mpi_info = "only mpi rank {}".format(
        mpi_only_log_rank) if mpi_only_log_rank is not None and rank is not None else ""
    if rank is None or mpi_only_log_rank is None or rank == mpi_only_log_rank:
        if _file_handler is not None:
            log.removeHandler(_file_handler)
        create_dir_if_not_exists(log_file)
        _file_handler = logging.FileHandler(log_file, mode=mode)
        formatter = logging.Formatter(_format, datefmt=datefmt)
        _file_handler.setFormatter(formatter)
        log.addHandler(_file_handler)
        start_dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        log.info("Start logging {}, saved to file: {} {}".format(start_dt, log_file, mpi_info))
def plot_embedding(X,
                   labels_str,
                   title,
                   imgs=None,
                   save_dir=None,
                   frame_lable=None,
                   max_frame=None,
                   vid_lable=None):
    # http://scikit-learn.org/stable/auto_examples/manifold/plot_lle_digits.html
    x_min, x_max = np.min(X, 0), np.max(X, 0)
    X = (X - x_min) / (x_max - x_min)
    if imgs is not None:
        fig = plt.figure(figsize=(20, 20))
        ax = plt.subplot(221)
    else:
        fig = plt.figure()
        ax = fig.gca()

    # labels blow plt
    n_classes, y, colors, legend_elements = plt_labeled_data(ax, X, labels_str)

    plt.title(title)
    if imgs is not None:
        # plt again but with image overlay
        ax = plt.subplot(222)
        ax.set_title("image overlay")
        ax.scatter(X[:, 0], X[:, 1], color=colors)
        if hasattr(offsetbox, "AnnotationBbox"):
            # only print thumbnails with matplotlib > 1.0
            shown_images = np.array([[1.0, 1.0]])  # just something big
            for i in range(X.shape[0]):
                dist = np.sum((X[i] - shown_images)**2, 1)
                if np.min(dist) < 5e-3:
                    # don't show points that are too close
                    continue
                shown_images = np.r_[shown_images, [X[i]]]
                imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(
                    imgs[i], cmap=plt.cm.gray_r, zoom=0.75),
                                                    X[i],
                                                    pad=0.0)
                ax.add_artist(imagebox)

        # plt legend same as befor
        plt_labels_blow(ax, list(legend_elements.values()))

    if frame_lable is not None:
        # plt the frames classe
        # show color for ever 50 frame in legend
        ax = plt.subplot(223)
        plt_labeled_data(
            ax,
            X,
            frame_lable,
            label_filter_legend=lambda l: l % 50 == 0,
            plt_cm=plt.cm.Spectral,
            index_color_factor=max_frame,
        )

        ax.set_title("frames as label (color range normalized for every vid)")
    if vid_lable is not None:
        # plt the view pair as classe
        ax = plt.subplot(224)
        plt_labeled_data(ax, X, vid_lable, label_filter_legend=lambda x: False)

        ax.set_title("view pair as label")

    if save_dir is not None:
        create_dir_if_not_exists(save_dir)
        save_dir = os.path.expanduser(save_dir)
        title = os.path.join(save_dir, title)
    fig.savefig(title + ".pdf", bbox_inches="tight")
    log.info("save TSNE plt to: {}".format(title))
    plt.close("all")