import cv2
import os
import numpy as np
args={"fname": "/home/ta/Desktop/train_set_v3/txt/train.txt",
"delimiter": " ",
"img_depth": 1
}
def _read_label_file_train_val(file, delimiter):
f = open(file, "r")
filepaths = []
labels = []
collection=[]
for line in f:
collection.append(line)
np.random.shuffle(collection)
for cl in collection:
tokens = cl.split(delimiter)
filepaths.append(tokens[0])
labels.append(int(tokens[1]))
return filepaths, labels
def _read_image(path):
if args["img_depth"]==3:
img=cv2.imread(path, cv2.IMREAD_COLOR)
HT,WD,_=img.shape
else:
img=cv2.imread(path, cv2.IMREAD_GRAYSCALE)
HT,WD=img.shape
img_=img.astype(np.float64)
return img_
def get_data():
filepaths,_=_read_label_file_train_val(args["fname"], args["delimiter"])
num=len(filepaths)
mean=0
for i in range(num):
img=_read_image(filepaths[i])
mean=mean+np.mean(img)/num
print("-----mean=",mean)
if __name__ == '__main__':
get_data()
网友评论