Using Command Line Argument with Python on Jupyter Notebook - python

I need to grasp the concept behind these lines of code. It is really confusing to me. I used Jupyter Notebook to run the code but received the error flag:
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image file")
ap.add_argument("-c", "--coords",
help = "comma separated list of source points")
args = vars(ap.parse_args())
The error message:
usage: ipykernel_launcher.py [-h] [-i IMAGE] [-c COORDS]
ipykernel_launcher.py: error: unrecognized arguments: -f C:\Users\bamidele\AppData\Roaming\jupyter\runtime\kernel-720c660a-a69a-48d8-9b23-40bf4a604168.json

Related

I am getting 'SystemExit: 2' error , how to fix it

I am new to the machine learning coding . I am trying to run the code to find the number people in the video , image or by the live camera, but I am getting error
I am using Collab notebook to run it
'''
if __name__ == "__main__":
HOGCV = cv2.HOGDescriptor()
HOGCV.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
args = argsParser()
humanDetector(args) '''
I am getting this error
'''
usage: ipykernel_launcher.py [-h] [-v VIDEO] [-i IMAGE] [-c CAMERA]
[-o OUTPUT]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-53a4a604-58e1-45bc-94ff-0a7c8720f3dc.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2 '''
I excepted it should run using image , video and camera in collab notebook`
You need to add in the end of " arg_parse.add_argument " line ( This is the reason you are getting 'SystemExit: 2' error ):
parser.add_argument("-f", required=False)
Edit your argsParser like this:
def argsParser():
arg_parse = argparse.ArgumentParser()
arg_parse.add_argument("-v", "--video", default='your_video_path', help="path to Video File ")
arg_parse.add_argument("-i", "--image", default='your_image_path', help="path to Image File ")
arg_parse.add_argument("-c", "--camera", default='True/False', help="Set True if you want to use the camera.")
arg_parse.add_argument("-o", "--output", type=str, default='your_output_path', help="path to optional output video file")
arg_parse.add_argument("-f", required=False)
args = vars(arg_parse.parse_args())
return args
Now you can call:
args = argsParser()
humanDetector(args)

Running Argpars but got this error SystemExit 2

I am trying to set up training arguments and parse but I got this error could anyone help please!
parser = argparse.ArgumentParser(description='Explore pre-trained AlexNet')
parser.add_argument(
'--image_path', type=str,
help='Full path to the input image to load.')
parser.add_argument(
'--use_pre_trained', type=bool, default=True,
help='Load pre-trained weights?')
args = parser.parse_args()
got this error
usage: ipykernel_launcher.py [-h] [--image_path IMAGE_PATH]
[--use_pre_trained USE_PRE_TRAINED]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-ff8e2476-e39b-4e40-b8f9-6b8113fe8f1f.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
In a Jupyter notebook cell:
import sys
sys.argv
I get get
['/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py',
'-f',
'/home/paul/.local/share/jupyter/runtime/kernel-7923bfd2-9f96-45cf-8b44-1859a2185715.json']
The Jupyter server is using the sys.argv to set up the communication channel with your kernel. argparse parses this list too.
So commandline and argparse cannot be used to provide arguments to your notebook when run this way.
How did you start this script? Did you even try to provide the commandline values that the script expected?
'--image_path'
'--use_pre_trained'
If you did, you probably would have gotten a different parser's error, about 'unexpected arguments'. That's coming from the server.
If you use a Colab may this solution help you, this solution suggest to you to write argparse in the other python file introduction-argparse-colab
%%writefile parsing.py
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
In a Jupyter notebook code works like this:
parser = argparse.ArgumentParser()
parser.add_argument('--image_folder', type=str, default='/content/image', help='path to image folder')
parser.add_argument("-f", "--file", required=False)
You need to add in the end of " parser.add_argument " line ( This is the reason you are getting 'SystemExit: 2' error ):
parser.add_argument("-f", required=False)
in your case this should work:
parser = argparse.ArgumentParser(description='Explore pre-trained AlexNet')
parser.add_argument('--image_path', type=str, default='your_path', help='Full path to the input image.')
parser.add_argument('--use_pre_trained', type=bool, default=True, help='Load pre-trained weights?')
parser.add_argument("-f", "--file", required=False)
args = parser.parse_args()
Now you can call:
image = args.image_path
Or
from PIL import Image
image = Image.open(args.image_path)
Tested in Google colab

Parse command line argument in python

have to do command line parsing and I am getting this error. I'm very new to Python and I don't know what the error means.
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-m", "--mask-rcnn", required=True,
help="base path to mask-rcnn directory")
ap.add_argument("-v", "--visualize", type=int, default=0,
help="whether or not we are going to visualize each instance")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
ap.add_argument("-t", "--threshold", type=float, default=0.3,
help="minimum threshold for pixel-wise mask segmentation")
args = vars(ap.parse_args())
I am getting this error:
usage: main.py [-h] -i IMAGE -m MASK_RCNN [-v VISUALIZE] [-c CONFIDENCE]
[-t THRESHOLD]
main.py: error: the following arguments are required: -i/--image, -m/--mask-rcnn
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
​
argparse processes the strings in the sys.argv list. Normally this comes from the command line values provided when calling the script containing the argparse code:
$ python main.py -i foo -m bar
But it looks like you are using ipython. It would be good to see how you call this script. But you might need to use:
$ ipython -i main.py -- -i foo -m bar
The '--' separates the input that ipython uses from the input that it should make available to main.py.

How to parse arguments in python (spyder)?

I am following this tutorial and trying to run the below part of the script. I am using python 3.7 and spyder 3.3.4.
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
help="path to input dataset (i.e., directory of images)")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-l", "--labelbin", required=True,
help="path to output label binarizer")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
I have tried going to Run > Configuration per file and entering the arguments as advised by this post and and this post.
command line options: path1, path2, path3, path4
I filled out the appropriate paths for the arguments above and then ran the script, but go the error below.
usage: train.py [-h] -d DATASET -m MODEL -l LABELBIN [-p PLOT]
train.py: error: the following arguments are required: -d/--dataset,
-m/--model, -l/--labelbin An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
How can I fix this error to run my script appropriately and pass the arguments in spyder?
You can parse arguments by doing a special run from the settings and putting in the order that the arguments are expected.

Python ArgumentParser - Error - Missing arguments?

I'm trying to run this code :
ap = argparse.ArgumentParser()
ap.add_argument("-q", "--query", required=True, help="search query to search Bing Image API for")
ap.add_argument("-o", "--output", required=True, help="path to output directory of images")
args = vars(ap.parse_args())
And I get this error :
usage: ipykernel_launcher.py [-h] -q QUERY -o OUTPUT
ipykernel_launcher.py: error: the following arguments are required:
-q/--query, -o/--output
I've tried to look into the ArgumentParser documentation (here), but couldn't find my answer. Could someone help me ?
You must launch the application providing the arguments indicated in the error message. In your code, both arguments are required. Hence, you must supply them to run the application.
Here is what it may be expecting
python ipykernel_launcher.py -q "<query>", -o "<dir>"
Note, the <query> and <dir> above are fillers. You must provide a query inline with applications definition of a query. The -o flag indicates a requirement to pass a directory. Hence, it could be as simple as ./output or something between these lines.
You set the required option to true:
ap.add_argument("-q", "--query", **required=True,** help="search query to search Bing Image API for")
ap.add_argument("-o", "--output", **required=True**, help="path to output directory of images")
(** added for visibility)
writing
ap.add_argument("-q", "--query", **required=False,** help="search query to search Bing Image API for")
ap.add_argument("-o", "--output", **required=False**, help="path to output directory of images")
instead, should fix the problem. Unless of course you want them to be required, then you have to call the script with the arguments.

Categories