gpt4 book ai didi

python - OpenCV Python中的内存不足错误-(-215)u!= 0在函数cv::Mat::create中

转载 作者:行者123 更新时间:2023-12-02 17:09:20 24 4
gpt4 key购买 nike

我正在使用OpenCV + Python进行与人类情绪检测相关的研究项目。我遵循使用CK +数据集进行训练的教程。但是,当我尝试运行代码来训练数据集时,它给出了OutOfMemory错误。我该如何解决这个问题。请帮我。我是OpenCV和Python的初学者。我把错误代码和源代码放在下面。

OpenCV Error: Insufficient memory (Failed to allocate 495880000 bytes) in cv::OutOfMemoryError, file C:\projects\opencv-python\opencv\modules\core\src\alloc.cpp, line 55
OpenCV Error: Assertion failed (u != 0) in cv::Mat::create, file C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp, line 436
Traceback (most recent call last):
File "D:/Documents/Private/Pycharm/EmotionDetection/training.py", line 71, in <module>
correct = run_recognizer()
File "D:/Documents/Private/Pycharm/EmotionDetection/training.py", line 49, in run_recognizer
fishface.train(training_data, np.asarray(training_labels))
cv2.error: C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:436: error: (-215) u != 0 in function cv::Mat::create

这是源代码。
   import cv2
import glob
import random
import numpy as np

emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] # Emotion list
fishface = cv2.face.EigenFaceRecognizer_create() # Initialize fisher face classifier

data = {}


def get_files(emotion): # Define function to get file list, randomly shuffle it and split 80/20
files = glob.glob("dataset\\%s\\*" % emotion)
random.shuffle(files)
training = files[:int(len(files) * 0.8)] # get first 80% of file list
prediction = files[-int(len(files) * 0.2):] # get last 20% of file list
return training, prediction


def make_sets():
training_data = []
training_labels = []
prediction_data = []
prediction_labels = []
for emotion in emotions:
training, prediction = get_files(emotion)
# Append data to training and prediction list, and generate labels 0-7
for item in training:
image = cv2.imread(item) # open image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to grayscale
training_data.append(gray) # append image array to training data list
training_labels.append(emotions.index(emotion))

for item in prediction: # repeat above process for prediction set
image = cv2.imread(item)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
prediction_data.append(gray)
prediction_labels.append(emotions.index(emotion))

return training_data, training_labels, prediction_data, prediction_labels


def run_recognizer():
training_data, training_labels, prediction_data, prediction_labels = make_sets()

print("training fisher face classifier")
print("size of training set is:", len(training_labels), "images")

fishface.train(training_data, np.asarray(training_labels))

print("predicting classification set")

cnt = 0
correct = 0
incorrect = 0
for image in prediction_data:
pred, conf = fishface.predict(image)
if pred == prediction_labels[cnt]:
correct += 1
cnt += 1
else:
cv2.imwrite("difficult\\%s_%s_%s.jpg" % (emotions[prediction_labels[cnt]], emotions[pred], cnt), image) # <-- this one is new
incorrect += 1
cnt += 1
return (100 * correct) / (correct + incorrect)


# Now run it
meta_score = []
for i in range(0, 10):
correct = run_recognizer()
print("got", correct, "percent correct!")
meta_score.append(correct)

print("\n\nend score:", np.mean(meta_score), "percent correct!")

最佳答案

如果您使用的是32位系统,则这是不可能的,因为没有足够的内存可以寻址。您的镜像对于构建系统而言太大。如果您具有32位操作系统,请升级到64位,否则您可能正在使用32位构建环境,应该切换到64位构建工具。

关于python - OpenCV Python中的内存不足错误-(-215)u!= 0在函数cv::Mat::create中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46689344/

24 4 0