概述
最近在做和视觉深度学习有关的项目,在写相关代码的时候读取数据集的图片来进行预处理成了大问题。在查询了网上现有方法的基础上,总结了一种较为简单通用的读取方法。
遍历文件夹
首先是对于数据集文件夹的遍历,这里可以输出得到文件夹内所有图片的名称
def load_img(train_imgPath
,test_imgPath
):
test_filenames
= os
.listdir
(test_imgPath
)
train_filenames
= os
.listdir
(train_imgPath
)
for tfname
in train_filenames
:
if os
.path
.isdir
(tfname
) == False:
filenames
= os
.listdir
(train_imgPath
+tfname
)
for filename
in filenames
:
if filename
.endswith
('.jpg'):
print(filename
)
路径选择
path
= train_imgPath
+ "\\" + tfname
+ "\\" + filename
train_imgPath是训练集文件夹的位置,tfname是数字文件夹的名称,filename是图片名称。
项目实例
import cv2
from skimage
import data_dir
, io
, color
import numpy
as np
import random
import numpy
as np
import os
, shutil
Path_traindata
= "D:/data/train/"
Path_testdata
= "D:/data/test/"
def load_img(train_imgPath
,test_imgPath
):
test_filenames
= os
.listdir
(test_imgPath
)
train_filenames
= os
.listdir
(train_imgPath
)
for tfname
in test_filenames
:
if os
.path
.isdir
(tfname
) == False:
filenames
= os
.listdir
(test_imgPath
+tfname
)
for filename
in filenames
:
if filename
.endswith
('.jpg'):
path
= test_imgPath
+ "\\" + tfname
+ "\\" + filename
src
= cv2
.imread
(path
)
(b
, g
, r
) = cv2
.split
(src
)
bH
= cv2
.equalizeHist
(b
)
gH
= cv2
.equalizeHist
(g
)
rH
= cv2
.equalizeHist
(r
)
result
= cv2
.merge
((bH
, gH
, rH
))
cv2
.imwrite
(path
, result
)
load_img
(Path_traindata
,Path_testdata
)
这段是读取数据库之后再使用cv2做的图片预处理。