Error in Python code

Asked

Viewed 125 times

0

I’m trying to run the following code but always appears this same type of error:

"usage: bw2color_image.py [-h] --i I --p P --m M --c C
bw2color_image.py: error: the following arguments are required: --i/--image images/robin_williams.jpg, --p/--prototxt model/colorization_deploy_v2.prototxt, --m/--model model/colorization_release_v2.caffemodel, --c/--points model/pts_in_hull.npy

It’s like he can’t find the address of the files .jpg, .npy, .prototxt and .caffemodel. I can’t find the error in this code.

Below follows the code I’m working on.

import numpy as np
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("--i",'--image images/robin_williams.jpg', type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--p","--prototxt model/colorization_deploy_v2.prototxt", type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--m","--model model/colorization_release_v2.caffemodel", type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--c","--points model/pts_in_hull.npy", type=str, required=True,
    help="D:\Colorir_Imagens")
args = vars(ap.parse_args())

# load our serialized black and white colorizer model and cluster
# center points from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["colorization_deploy_v2.prototxt"], args["colorization_release_v2.caffemodel"])
pts = np.load(args["pts_in_hull.npy"])

# add the cluster centers as 1x1 convolutions to the model
class8 = net.getLayerId("class8_ab")
conv8 = net.getLayerId("conv8_313_rh")
pts = pts.transpose().reshape(2, 313, 1, 1)
net.getLayer(class8).blobs = [pts.astype("float32")]
net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]

# load the input image from disk, scale the pixel intensities to the
# range [0, 1], and then convert the image from the BGR to Lab color
# space
image = cv2.imread(args["albert_einstein.jpg"])
scaled = image.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)

# resize the Lab image to 224x224 (the dimensions the colorization
# network accepts), split channels, extract the 'L' channel, and then
# perform mean centering
resized = cv2.resize(lab, (224, 224))
L = cv2.split(resized)[0]
L -= 50

# pass the L channel through the network which will *predict* the 'a'
# and 'b' channel values
'print("[INFO] colorizing image...")'
net.setInput(cv2.dnn.blobFromImage(L))
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))

# resize the predicted 'ab' volume to the same dimensions as our
# input image
ab = cv2.resize(ab, (image.shape[1], image.shape[0]))

# grab the 'L' channel from the *original* input image (not the
# resized one) and concatenate the original 'L' channel with the
# predicted 'ab' channels
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)

# convert the output image from the Lab color space to RGB, then
# clip any values that fall outside the range [0, 1]
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)

# the current colorized image is represented as a floating point
# data type in the range [0, 1] -- let's convert to an unsigned
# 8-bit integer representation in the range [0, 255]
colorized = (255 * colorized).astype("uint8")

# show the original and output colorized images
cv2.imshow("Original", image)
cv2.imshow("Colorized", colorized)
cv2.waitKey(0)

1 answer

1


Note: Complete later, no time at the moment.


I can’t find the error in this code

When you are defining the arguments, you are trying to define a default value if the argument is omitted, but you are doing it wrong:

ap.add_argument("--i",'--image images/robin_williams.jpg', type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--p","--prototxt model/colorization_deploy_v2.prototxt", type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--m","--model model/colorization_release_v2.caffemodel", type=str, required=True,
    help="D:\Colorir_Imagens")
ap.add_argument("--c","--points model/pts_in_hull.npy", type=str, required=True,
    help="D:\Colorir_Imagens")

To set a default value, enter the parameter: default

ap.add_argument("--i",'--image', type=str, 
    default="images/robin_williams.jpg", help="D:\Colorir_Imagens")

ap.add_argument("--p","--prototxt", type=str,
    default="model/colorization_deploy_v2.prototxt", help="D:\Colorir_Imagens")

ap.add_argument("--m","--model", type=str,
    default="model/colorization_release_v2.caffemodel", help="D:\Colorir_Imagens")

ap.add_argument("--c","--points", type=str,
    default="model/pts_in_hull.npy", help="D:\Colorir_Imagens")
args = ap.parse_args()
print(args)
> Namespace(c='model/pts_in_hull.npy', i='images/robin_williams.jpg', m='model/colorization_release_v2.caffemodel', p='model/colorization_deploy_v2.prototxt')

See working

note that the parameter has been removed required if it is defined as True will require the argument and return the error similar to this:

> error: the following arguments are required: --i/--image, --p/--prototxt, --m/--model, --c/--points

See working

another important point, is that if you are informed the argument with no value will return the error similar to this:

python program.py --i
> error: argument --i/--image: expected one argument

See working

Reference:

  • Thanks for the help. It helped a lot. Have a great fds.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.