• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用npy转image图像并保存的实例

python 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论

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_nam<div>本文来源gaodai.ma#com搞##代!^码7网</div>e, 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()

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用npy转image图像并保存的实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址