Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am getting this error: TypeError: 'Namespace' object is not subscriptable [duplicate]

Tags:

python

I am facing problems with the argparse library.

I have the encodings.pickle file and I am following the below code to recognize the actors but it seems to load the embeddings is not working.

Here is the code:

ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
                help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
                help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
                help="face detection model to use: either `hog` or `cnn`")
ap.add_argument("-fnn", "--fast-nn", action="store_true")
args = parser.parse_args('')
print(args)

# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())

Source code can be found here.

like image 223
Ahmed Abousari Avatar asked Mar 16 '26 20:03

Ahmed Abousari


1 Answers

You are getting this error because the parse_args() method returns a Namespace containing the parsed arguments.

When printing the result of that method, you will see the created namespace: Namespace(encodings='encoding', image='image', detection_method='cnn', fast_nn=False)

In order to access arguments, you should use dot notation (i.e. args.encodings or args.image)

Amended code:

ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
                help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
                help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
                help="face detection model to use: either `hog` or `cnn`")
ap.add_argument("-fnn", "--fast-nn", action="store_true")
args = parser.parse_args()
print(args)

# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args.encodings, "rb").read())
like image 66
H. Ross Avatar answered Mar 18 '26 08:03

H. Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!