0

I dont deeply understand about the meaning of argparse. And when i tested with this code, it always this error:

usage: test1.py [-h] -i IMAGE
test1.py: error: the following arguments are required: -i/--image

This is my code:

# (1) import the necessary packages
import argparse
import imutils
import cv2

# (2) construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to the input image")
args = vars(ap.parse_args())

# (3) load the image, convert it to grayscale, blur it slightly,
# and threshold it
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', gray)

blurred = cv2.GaussianBlur(gray, (5,5), 0)
cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', blurred)

thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('C:\\Users\\Hoang Cao Chuyen\\Downloads\\shapes.png', thresh)

# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Chuyên Hoàng
  • 108
  • 1
  • 3
  • 8
  • What is your input in command line? As you set `required=True`, you need to input like `test1.py -i image` and `test1.py image` will raise error. – adamkwm Apr 17 '21 at 10:30
  • how exactly did you test this? What's the `image` value, and how did you provide it? – hpaulj Apr 17 '21 at 10:59
  • With `pycharm` you have to specify command line arguments in some extra window. You can't just push the `run` button. I don't know why many new users miss this. Doesn't the IDE docs explain how to run scripts with arguments? – hpaulj Apr 17 '21 at 14:46
  • thank you guys. You're so kind to answer me. As I said first, i didnt understand the extreme meaning of argparse.I read some documents but i still cannot. – Chuyên Hoàng Apr 17 '21 at 16:25
  • The input of path is a image from my C:/ path, but the '-image','-i', i dont know where it is from? – Chuyên Hoàng Apr 17 '21 at 16:26
  • It's the job of someone using this program to provide that command-line argument. So it comes from the user, if they're using your program correctly. – Charles Duffy Apr 18 '21 at 03:09
  • Voting to reopen, the "duplicate" question is about how arguments are parsed when passing a list of strings to an `ArgumentParser`. The error message happens to be the same but the root cause is completely different. – augurar Apr 18 '21 at 03:46

1 Answers1

1

This line of code adds a required flag -i or --image:

ap.add_argument("-i", "--image", required=True, help="path to the input image")

This means the script expects you to specify the -i or --image flag with an argument, like this:

python test1.py --image C:\path\to\image.png

If you want the image path to be a positional argument instead, you can change that line of code to this:

ap.add_argument("image", help="path to the input image")

Then you can call your script like this:

python test1.py C:\path\to\image.png

Relevant documentation: https://docs.python.org/3/library/argparse.html#name-or-flags

augurar
  • 12,081
  • 6
  • 50
  • 65
  • As your suggestion and with my understanding, I tried to code : python test1.py --image C:\pythonProject\venv\bill_gates.jfif in my Terminal ,but it failed. – Chuyên Hoàng Apr 18 '21 at 09:12
  • @ChuyênHoàng What error message did you get? Make sure that `python` is on your path or refers to the correct Python executable, and `test1.py` should be the path to your script file. – augurar Apr 18 '21 at 19:57
  • After finding out something on the Youtube, I fixed the error. The answer is the same as yours. Thank you – Chuyên Hoàng Apr 19 '21 at 01:27
  • Another error is in my code, not in argparse anymore – Chuyên Hoàng Apr 19 '21 at 01:28