深度学习神经网络训练读入的都是numpy数组,一般直接读取图片在做预处理送入模型训练时很浪费时间的,我们可以提前将数据转成.npy格式的文件,并且数据的预处理也可以在这里完成。那么,将预处理好的数据直接送入网络就可以节省很多的时间了。
关于下面有关raw数据读取看不懂的可以查看我的另外一篇专门敢于读raw数据转png图片的博客,里面有解释。
# -*- coding:utf8 -*-
import numpy as np
import cv2
import os
def oneimg2npy_(root_dir, out_path):
filelist = os.listdir(root_dir)
"读取raw数据时使用"
# 文件排序
filelist.sort(key=lambda x: int(x.split('_')[5]))
for file in filelist:
bayer = np.zeros(shape=2073600, dtype='uint16')
file_path = os.path.join(root_dir, file)
with open(file_path, "rb") as f:
for i in range(0, len(f.read()), 2):
f.seek(i)
raw = f.read(2)
a1 = int((raw[0] / 16) % 16)
a2 = int(raw[0] % 16)
a3 = int((raw[1] / 16) % 16)
a4 = int(raw[1] % 16)
value = a3 * 256 + a4 * 16 + a1 * 1
bayer[int(i / 2)] = value
bayer = bayer.reshape(1080, 1920)
height, width = bayer.shape[0], bayer.shape[1]
# print(height, width)
h = height // 2
w = width // 2
R = np.zeros(shape=(h, w), dtype='uint16', order='C')
Gr = np.zeros(shape=(h, w), dtype='uint16', order='C')
Gb = np.zeros(shape=(h, w), dtype='uint16', order='C')
B = np.zeros(shape=(h, w), dtype='uint16', order='C')
for x in range(height):
for y in range(0, width, 2):
if x % 2 == 0:
R[int(x / 2)][int(y / 2)] = bayer[x][y]
Gr[int(x / 2)][int(y / 2)] = bayer[x][y + 1]
elif x % 2 == 1:
Gb[int(x / 2)][int(y / 2)] = bayer[x][y]
B[int(x / 2)][int(y / 2)] = bayer[x][y + 1]
out = np.stack((R, Gr, Gb, B))
out = out.astype(np.float32)
if not os.path.exists(out_path):
os.makedirs(out_path)
np.save(os.path.join(out_path, './{}.npy'.format(file.split(".")[0])), out)
print("saved: {}".format(file))
f.close()
print("raw finished")
"读取rgb数据时使用"
filelist.sort(key=lambda x: int((x.split('_')[5]))
for file in filelist:
file_path = os.path.join(root_dir, file)
label = cv2.imread(file_path)
# label = cv2.resize(label, (960, 540), interpolation=cv2.INTER_NEAREST).astype(np.float32)
label = np.transpose(label, (2, 0, 1))
if not os.path.exists(out_path):
os.makedirs(out_path)
np.save(os.path.join(out_path, './{}.npy'.format(file.split(".")[0])), label)
print("rgb finished")
if __name__ == '__main__':
_ = oneimg2npy_(root_dir='', out_path='')