'hello.py: error: the following arguments are required: -i/--image

I have taken a piece of code from the web which uses OpenCV to find circles in an image.

# import the necessary packages
import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
args = vars(ap.parse_args())

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(args["image"])
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
        
    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

However, I keep getting the same error: hello.py: error: the following arguments are required: -i/--image

I've tried ap.parse_args(args) instead of ap.parse_args() but that doesn't seem to solve it.



Solution 1:[1]

There are two ways to do it.

ap.add_argument("-i",  r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg", required=True,
    help="path to input image")

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

Solution 2:[2]

Hope this will work !

# import the necessary packages
import numpy as np
import argparse
import cv2



# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread(r"C:\Users\user\Desktop\Sem 8T\Mini Project\Data\Imagestraffic.jpg")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)

# ensure at least some circles were found
if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
        
    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 toyota Supra
Solution 2 Parthiban Marimuthu