伙计们,我正在尝试创建一个应用程序来创建训练数据

数据挖掘 数据集 训练
2022-03-09 01:49:28
import os
import cv2
import matplotlib.pyplot as plt
from colorama import Fore
import random
import numpy as np
import pickle

training_data = []
def create_training_data():
path = os.getcwd()
path_dataset = os.path.join(path,'dataset')
categories = ['a','b']
for category in categories:
    path_categories = os.path.join(path_dataset,category)
    index = categories.index(category)
    for img in os.listdir(path_categories):
        try:
            path = os.path.join(path_categories,img)
            img_array = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
            training_data.append([img_array,index])
        except Exception as e:
            print(Fore.RED+path)
    print(Fore.GREEN+f"imported all data from cateogry {category}")



#For every object in the training dataset
#training_data[element][data type*]
#0 - Stores the values of all images
#1 -  stores the values of all category

create_training_data()

print(training_data[1201][1])
plt.imshow(training_data[1201][0],cmap="gray")
rows,columns = training_data[0][0].shape
print(f"rows-{rows} cols-{columns}")
plt.show()
random.shuffle(training_data)
X = []
Y = []

for feature,label in training_data:
X.append(feature)
Y.append(label)

X = np.array(X).reshape(-1,rows,columns,1)


pickle_out = open(X.pickle,'wb')
pickle.dump(X,pickle_out)
pickle_out.close()

pickle_out = open(Y.pickle,'wb')
pickle.dump(Y,pickle_out)
pickle_out.close()

运行此脚本后,出现错误:

回溯(最近一次调用最后):文件“trainingdata.py”,第 55 行,在 pickle_out = open(X.pickle,'wb') AttributeError: 'numpy.ndarray' object has no attribute 'pickle'

1个回答

该行中的错误pickle_out = open(X.pickle,'wb')是文件名必须用引号引起来。所以正确的代码是:

pickle_out = open('X.pickle','wb')
pickle.dump(X,pickle_out)
pickle_out.close()

对 Y 也做同样的事情。您也可以通过以下方式更简洁地做到这一点:

with open('X.pickle', 'wb') as f:
    pickle.dump(X, f)