LabelEncoder对象不是subscriptab

2024-10-03 02:43:48 发布

您现在位置:Python中文网/ 问答频道 /正文

from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
import argparse
import pickle

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--embeddings",default=r"C:\Users\osama\Desktop\opencv-face-recognition\face_detection_model\output",
    help="path to serialized db of facial embeddings")
ap.add_argument("-r", "--recognizer", default=r"C:\Users\osama\Desktop\opencv-face-recognition\face_detection_model\output",
    help="path to output model trained to recognize faces")
ap.add_argument("-l", "--le", default=r"C:\Users\osama\Desktop\opencv-face-recognition\face_detection_model\output",
    help="path to output label encoder")
args = vars(ap.parse_args())
# load the face embeddings
print("[INFO] loading face embeddings...")
data = pickle.loads(open(args["embeddings"], "rb").read())

# encode the labels



   print("[INFO] encoding labels...")
    le = LabelEncoder()
    labels = le.fit_transform(data["names"])

# train the model used to accept the 128-d embeddings of the face and
# then produce the actual face recognition


 print("[INFO] training model...")
    recognizer = SVC(C=1.0, kernel="linear", probability=True)
    recognizer.fit(data["embeddings"], labels)

# write the actual face recognition model to disk
f = open(args["recognizer"], "wb")
f.write(pickle.dumps(recognizer))
f.close()

#write the label encoder to disk
f = open(args["le"], "wb")
f.write(pickle.dumps(le))
f.close()

我得到的错误是:

Traceback (most recent call last):

  File "<ipython-input-6-f2ad45651de8>", line 1, in <module>
    runfile('C:/Users/osama/Desktop/untitled1.py', wdir='C:/Users/osama/Desktop')

  File "C:\Users\osama\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\osama\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/osama/Desktop/untitled1.py", line 28, in <module>
    labels = le.fit_transform(data["names"])

TypeError: 'LabelEncoder' object is not subscriptable

我试着给这些名字加上标签然后得到这个错误。有什么办法?你知道吗


Tags: thetoleoutputlabelsmodelargsusers
1条回答
网友
1楼 · 发布于 2024-10-03 02:43:48

您正在此处取消拾取对象:

data = pickle.loads(open(args["embeddings"], "rb").read())

它的类型为LabelEncoder。然后您将尝试使用以下下标(索引,如列表)访问它:

    labels = le.fit_transform(data["names"])

这是不允许的。所以你需要解决这个问题。也许你想要这样的东西:

    labels = le.fit_transform(data.get_params())

在任何情况下,data是一个LabelEncoder对象,不能使用下标访问它(data["names"]将不起作用)。您希望data是什么类型的对象?你知道吗

相关问题 更多 >