-2

I'm following a data-flair tutorial and everything is working well except when it comes to this:

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True, help="Image Path")
args = vars(ap.parse_args())
img_path = args['image']

it gives me this error,

error

I tried writing the code in text file then save it as .py file but it gives the same error.

furas
  • 134,197
  • 12
  • 106
  • 148
MoAh
  • 11
  • 2
  • Does this answer your question? [Passing command line arguments to argv in jupyter/ipython notebook](https://stackoverflow.com/questions/37534440/passing-command-line-arguments-to-argv-in-jupyter-ipython-notebook) – Nir Alfasi Jun 06 '21 at 11:34
  • remove `'-i,'` as it is not necessary – Prakash Dahal Jun 06 '21 at 11:47
  • Prakash Dahal, I tried it before but the same error occurred. – MoAh Jun 06 '21 at 12:43
  • always put code, data and full error message as text (not screenshot, not link) in question (not comment). – furas Jun 06 '21 at 13:22
  • if you follow some tutorial then add link to this tutorial - put it in question, not comment. – furas Jun 06 '21 at 13:24
  • create minimal working code with this problem and show how you run it. Do it in question, not in comment. Did you run it as `python script.py -i some_image` in console/terminal? – furas Jun 06 '21 at 13:25
  • @furas I run it as !python example.py from jupyter notebook – MoAh Jun 07 '21 at 14:12
  • then you have to run it as `!python example.py -i some_image`. OR remove all `argparse` and assign image directly in code `img_path = "some_image"` – furas Jun 07 '21 at 14:17
  • I want the image to be passed to the model then the caption for that image to be generated by the model to be displayed on command line. @furas – MoAh Jun 07 '21 at 14:33
  • then you have to run it as `!python example.py -i some_image` – furas Jun 07 '21 at 14:52
  • @furas I run `!python ex1212.py -i --image` but same error occurred `error: the following arguments are required: -i/--image` – MoAh Jun 07 '21 at 15:19
  • you have to use `-i` OR `--image` with `filename` - like `!python example.py -i lenna.png` or `!python example.py --image lenna.png`. Or even better with full path like `!python example.py -i /home/furas/images/lenna.png` – furas Jun 07 '21 at 15:28
  • @furas so I should also use the path here `ap.add_argument('-i', '--image', required=True, help="Image Path")` ? in the `help="Image Path"` – MoAh Jun 07 '21 at 15:32
  • NO, don't change `args` - it is correct. It will automatically create variable `ap.image` with value `/home/furas/images/lenna.png` (or with any string you use after `--image` in command line) – furas Jun 07 '21 at 15:37
  • BTW: instead of two lines `args = vars(ap.parse_args())` and `img_path = args['image']` you can directy use `img_path = ap.image` – furas Jun 07 '21 at 15:40
  • and `help` is only to display information when you use it with `-h` or `--help` like `!python example.py --help` – furas Jun 07 '21 at 15:41

1 Answers1

0

Code is OK but you run it incorrectly.

You should run it with -i or --image and with image's filename or with /full/path/to/image

!python example.py -i lenna.png

!python example.py --image lenna.png

!python example.py -i /home/furas/images/lenna.png

!python example.py --image /home/furas/images/lenna.png

And help=" " is only to display information when you run it as

!python example.py --help

or shorter

!python example.py -h

Result:

usage: example.py [-h] -i INPUT

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Image Path

In second column it shows Image Path which was in help="Image Path".

You can change it to something more readable like help="put path to image" and you get

usage: example.py [-h] -i INPUT

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        put path to image

Brackets [] in [-h] means that -h is optional.

-i INPUT means that it is required and it needs some value in place of INPUT.

It also shows that you can use:

  • --help instead of -h
  • --image instead of -i

BTW:

Instead of two lines

args = vars(ap.parse_args())
img_path = args['image']

you can run directly

img_path = ap.image
furas
  • 134,197
  • 12
  • 106
  • 148
  • Now, I changed the argparse to `ap = argparse.ArgumentParser() ap.add_argument('--image', required=True, help="Image Path") img_path = 'ap.image'` and I run at as `!python ex1212.py --image 42637986_135a9786a6.jpg` but it gives me this error `NameError: name 'load' is not defined` from this code `max_length = 32 tokenizer = load(open("tokenizer.p","rb")) model = load_model('models/model_9.h5')` – MoAh Jun 07 '21 at 15:57
  • it shows that `argparse` works correctly (so this problem is resolved) but now you have totally different problem in different place and you should have to create new question on new page. You use some `load()` which you should have to define or `import` from other file - so better check if you have `def load()` in some place in tutorial. And when you create new question on new page then better add link to your tutorial. – furas Jun 07 '21 at 16:00