Error during constructing the argument parse and parse the arguments - python

Why my argument parse has an error although I have already defined the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-w", "--width", type=float, required=True,
help="width of the left-most object in the image (in inches)")
args = vars(ap.parse_args())
and the error says that
usage: main.py [-h] -i IMAGE -w WIDTH
main.py: error: the following arguments are required: -i/--image, -w/--width

Related

How to implement parse_args() in pytorch?

I am trying to code a module for my deep learning model. I wish to store these arguments using argeparse. There is some problem occuring in args = parser.parse_args()! Also What are the benefits of using the argparse library?
import numpy as np
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str,
help='Dataset')
parser.add_argument('--epoch', type=int, default=40,
help='Training Epochs')
parser.add_argument('--node_dim', type=int, default=64,
help='Node dimension')
parser.add_argument('--num_channels', type=int, default=2,
help='number of channels')
parser.add_argument('--lr', type=float, default=0.005,
help='learning rate')
parser.add_argument('--weight_decay', type=float, default=0.001,
help='l2 reg')
parser.add_argument('--num_layers', type=int, default=2,
help='number of layer')
parser.add_argument('--norm', type=str, default='true',
help='normalization')
parser.add_argument('--adaptive_lr', type=str, default='false',
help='adaptive learning rate')
args = parser.parse_args()
print(args)
The above code is a part of full code, as given in the link below
Error:
usage: ipykernel_launcher.py [-h] [--dataset DATASET] [--epoch EPOCH]
[--node_dim NODE_DIM]
[--num_channels NUM_CHANNELS] [--lr LR]
[--weight_decay WEIGHT_DECAY]
[--num_layers NUM_LAYERS] [--norm NORM]
[--adaptive_lr ADAPTIVE_LR]
ipykernel_launcher.py: error: unrecognized arguments: -f /Users/anshumansinha/Library/Jupyter/runtime/kernel-1e4c0f41-a4d7-4388-be24-640dd3411f56.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
The above code is a part of a code, the full code can be seen on the following github link : link
You called ipykernel_launcher.py with the argument -f. But -f is not in your list of arguments.
Is -f the argument, which you want to use for the input file? Then you should add something like this to your code:
parser.add_argument('--file', '-f', type=str)
The benefit of argparse is that you don't have to write by hand the code for parsing the arguments. You can try this. It is more code than one normally thinks. Especially if you have positional arguments.

Command line arguments are not working in python

--A_Z_Handwritten Data.csv: The path to the Kaggle A-Z dataset (Lines 3 and 4)
--HandWritingRecognition: The path to output the trained handwriting recognition model (Lines 5 and 6)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-a", "--A_Z_Handwritten Data.csv", required=True,
help="path to A-Z dataset")
ap.add_argument("-m", "--HandWritingRecognition", type=str, required=True,
help="path to output trained handwriting recognition mode")
# ap.add_argument("-p", "--plot", type=str, default="plot.png",
# help="/home/gaurav/Desktop/HandWritingRecognition/")
args = vars(ap.parse_args())
This is the error...!
usage: ipykernel_launcher.py [-h] -a A_Z_HANDWRITTEN DATA.CSV -m
HANDWRITINGRECOGNITION
ipykernel_launcher.py: error: the following arguments are required: -a/--A_Z_Handwritten Data.csv, -m/--HandWritingRecognition
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2

How to run my Python code using Spyder, without the error "the following arguments are required: -p/--shape-predictor"?

How can I run this?
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True,
help="path to facial landmark predictor")
ap.add_argument("-a", "--alarm", type=str, default="",
help="path alarm .WAV file")
ap.add_argument("-w", "--webcam", type=int, default=0,
help="index of webcam on system")
args = vars(ap.parse_args())
While I run this on Spyder it gives
usage: [-h] -p SHAPE_PREDICTOR [-a ALARM] [-w WEBCAM]
: error: the following arguments are required: -p/--shape-predictor
An exception has occurred, use %tb to see the full traceback.
How can I solve this issue?
From #hiroprotagonist's comment posted above:
In spyder ctrl+F6 should allow you to pass command-line args when you execute your script. see here https://stackoverflow.com/a/26766414/4954037.

Usage: waterhed.py [-h] -i IMAGE waterhed.py: error: unrecognized arguments

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
args = vars(ap.parse_args())
when I use
python image.py -- image C/:.... path of image .....
I get the error message:
waterhed.py [-h] -i IMAGE waterhed.py: error: unrecognized arguments
Why did I get this error and how would I fix it?
Remove the space between the double dash (-- image -> --image), and also wrap paths with spaces in quotes:
python waterhed.py --image "C:/path of image.jpg"

Why is ArgumentParser add_argument not picking up this argument?

I have the following code at the top of my class:
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-m", "--method", required=False, default="CAFFE",
help="Method")
args = vars(ap.parse_args())
input_image_path = args["image"]
detection_method = args["method"]
When I try to run with these parameters, I get the error shown here:
python FaceRecognition.py -i images//test_image.jpg -m "CAFFE"
usage: FaceRecognition.py [-h] [-m METHOD]
FaceRecognition.py: error: unrecognized arguments: -i images//test_image.jpg
Why does it not recognise the -i argument?
I've already tried adding quotes, changing the order of the arguments, removing the other argument.
I tried running this:
python FaceRecognition.py -m "CAFFE"
usage: FaceRecognition.py [-h] -i IMAGE [-m METHOD]
FaceRecognition.py: error: the following arguments are required: -i/--image
How come it only recognises it when I dont include it?

Categories

Resources