preprocess_images.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:benchmark-keras 作者: beeva-ricardoguerrero 项目源码 文件源码
def preprocess_images_worker(line, prefix_orig, prefix_dest, img_rows, img_cols, img_crop_rows, img_crop_cols):
    """
    Meant to be called by preprocess_images_multiprocess
    Nested functions (to avoid parameters copy) can not be parallelizable, hence, this function is defined here
    """

    path, label = line.strip().split()

    if os.path.exists(prefix_orig + path):
        try:
            image = cv2.imread(prefix_orig + path)
            image = cv2.resize(image, (img_rows, img_rows),
                               interpolation=cv2.INTER_AREA)  # Resize in create_caffenet.sh

        except cv2.error:
            print("Exception catched. The image in path %s can't be read. Could be corrupted\n" % path)
            return ""

        if img_crop_rows != 0 and img_crop_rows != img_rows:  # We need to crop rows
            crop_rows = img_rows - img_crop_rows
            crop_rows_pre, crop_rows_post = int(mt.ceil(crop_rows / 2.0)), int(mt.floor(crop_rows / 2.0))
            image = image[crop_rows_pre:-crop_rows_post, :]

        if img_crop_cols != 0 and img_crop_cols != img_cols:  # We need to crop cols
            crop_cols = img_cols - img_crop_cols
            crop_cols_pre, crop_cols_post = int(mt.ceil(crop_cols / 2.0)), int(mt.floor(crop_cols / 2.0))
            image = image[:, crop_cols_pre:-crop_cols_post]  # Crop in train_val.prototxt

        # Store the image in h5 format
        npy_path = prefix_dest + path.split(".")[0] + ".npy"
        with open(npy_path, "wb") as fout:
            np.save(fout, image)

        return line.replace("JPEG", "npy")

    else:
        print("There is no image in %s" % path)
        return ""
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号