npy转image图像并保存

38 篇文章 21 订阅

1. 用于分类模型:

import numpy as np
import scipy.misc
import cv2
import os

# DF1
path = "/home/pi/工作/predict1/"
npy_list = os.listdir(path)
save_path = "/home/pi/predict1_img/"
if not os.path.exists(save_path):
    os.mkdir(save_path)

for i in range(0, len(npy_list)):
    print(i)
    print(npy_list[i])
    npy_full_path = os.path.join(path, npy_list[i])
    img = np.load(npy_full_path) # load进来

    save_full_path = os.path.join(save_path, npy_list[i][:-4])
    scipy.misc.imsave(save_full_path, img) # 保存

2. 用于分割模型

"""
将数据集随机分成训练集、测试集
传入参数:
ratio = 0.7 # 训练样本比例
path = "/home/pi/20190701_0705"  # 数据路径
new_path = "/home/pi/20190701_0705_new2"  # 保存路径
使用方法:
temp = Generate_Train_and_Test(path, new_path, ratio)
temp.splict_data()
"""
import random
import os
import cv2


def makeDir(path):
    try:
        if not os.path.exists(path):
            if not os.path.isfile(path):
                # os.mkdir(path)
                os.makedirs(path)
                return 0
        else:
            return 1
    except Exception as e:
        print(str(e))
        return -2


class Generate_Train_and_Test:

    def __init__(self, path, new_path, ratio):
        if not os.path.exists(new_path):
            makeDir(new_path)
        self.path = path
        self.new_path = new_path
        self.ratio = ratio
        self.train_sample_path = os.path.join(new_path, "train")
        self.test_sample_path = os.path.join(new_path, "test")

        makeDir(self.train_sample_path)
        makeDir(self.test_sample_path)

    def splict_data(self):
        class_names = os.listdir(self.path)  # 类别:bg and ng10
        for name in class_names:
            print("process class name=%s" % name)
            tmp_class_name = os.path.join(self.path, name)
            save_train_class_name = os.path.join(self.train_sample_path, name)
            save_test_class_name = os.path.join(self.test_sample_path, name)
            makeDir(save_train_class_name)
            makeDir(save_test_class_name)
            if os.path.isdir(tmp_class_name):
                image_names = os.listdir(tmp_class_name)  # 其中一个类别的所有图像
                image_names = [f for f in image_names if not f.endswith('_mask.png')]
                total = len(image_names)

                # 1, 打乱当前类中所有图像
                random.shuffle(image_names)

                # 2, 从当前类(ng)中,取前面的图像作为train data
                train_temp = int(self.ratio * total)  # 打乱后,取前面作为train_data
                for i in range(0, train_temp):
                    print(i, image_names[i])
                    temp_img_name = os.path.join(tmp_class_name, image_names[i])
                    train_image = cv2.imread(temp_img_name)
                    temp_label_name = os.path.join(tmp_class_name, image_names[i][:-4] + '_mask.png')
                    train_label = cv2.imread(temp_label_name)

                    save_train_img_name = os.path.join(save_train_class_name, image_names[i])
                    cv2.imwrite(save_train_img_name, train_image)

                    save_train_label_name = os.path.join(save_train_class_name, image_names[i][:-4] + '_mask.png')
                    cv2.imwrite(save_train_label_name, train_label)

                # 3, 从当前类(bg)中,取后面的图像作为test data
                for i in range(train_temp, total):
                    print(i, image_names[i])
                    test_img_name = os.path.join(tmp_class_name, image_names[i])
                    test_image = cv2.imread(test_img_name)
                    test_label_name = os.path.join(tmp_class_name, image_names[i][:-4] + '_mask.png')
                    test_label = cv2.imread(test_label_name)

                    save_test_img_name = os.path.join(save_test_class_name, image_names[i])
                    cv2.imwrite(save_test_img_name, test_image)

                    save_test_label_name = os.path.join(save_test_class_name, image_names[i][:-4] + '_mask.png')
                    cv2.imwrite(save_test_label_name, test_label)


ratio = 0.7  # 训练样本比例
path = "/home/pi/工作/20190712_splict"  # 数据路径
new_path = "/home/pi/工作/20190712_splict_new3"  # 保存路径

temp = Generate_Train_and_Test(path, new_path, ratio)
temp.splict_data()

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值