python - 类型错误 : 'int' object is not iterable error

标签 python opencv3.0

我正在尝试运行 python 代码,但出现错误,我不熟悉 python,所以我不知道如何调试代码。 请帮忙,谢谢。 我刚刚在这个网站上找到了这段代码: http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/ 这是代码:

import cv2
import glob
import random
import numpy as np

emotions = ["neutral", "anger", "contempt", "disgust", "fear", "happy", "sadness", "surprise"] #Emotion list
fishface = cv2.face.createFisherFaceRecognizer() #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:
            incorrect += 1
            cnt += 1
    return ((100*correct)/(correct + incorrect))
#Now run it
metascore = []
for i in range(0,10):
    correct = run_recognizer()
    print ("got", correct, "percent correct!")
    metascore.append(correct)

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

这是这段代码的输出:

training fisher face classifier
size of training set is: 351 images
predicting classification set
Traceback (most recent call last):
  File "splitData.py", line 62, in <module>
    correct = run_recognizer()
  File "splitData.py", line 51, in run_recognizer
    pred, conf = fishface.predict(image)
TypeError: 'int' object is not iterable

最佳答案

int 不可迭代

a, b = c 表示它将尝试将 c 解包为可迭代对象并分配给 a, b。因此,a, b = [1, 2] 会将 a 设置为 1 并将 b 设置为 2a, b = [1] 会出错,因为没有足够的值来解包。 a, b = [1, 2, 3] 会出错,因为要解包的值太多。

在您的情况下,a, b = 1 会出错,因为 1 无法解包,因为您无法遍历它。遍历一个对象意味着遍历它的所有元素(如列表、元组、集合、字典等)。整数只是一个值;它不可迭代。

这意味着 fishface.predict 返回一个数字。我不确定 pred, conf 是什么意思(我猜是预测和置信度),但请查看 FisherFaceRecognizer#predict 的文档以查看它返回的内容.

关于python - 类型错误 : 'int' object is not iterable error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46450159/

相关文章:

python - 使用 Opencv 比较签名

python - 文件夹逻辑中的重复图像

Python Pandas 根据条件更改值

python - 带有 Selenium 的 Firefox( headless )

python - 无法使用 django-python3-ldap 连接到 ldap

python - 有没有办法避免这种重复的代码?

python - OpenCV 在 Mac 上全屏显示图像,没有白色边框

opencv - 人脸特征点标注工具

python - 当我尝试合并零 channel 以创建具有单个 channel 的图像时出错

python - MPI 信号处理