Ahmad M.
Ahmad M.

Reputation: 524

Passing Hard Arguments

I am trying to hardcode arguments that are passed to the OpenCV Command Line Parser. In the object_detection file, I have added the code as such, so that I do not have to enter the file name via command-line:

Line 40-48:

void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame);

void callback(int pos, void* userdata);

int main(int argc, char** argv)
{
    // This line added below:
    argv[1]="-input /home/Videos/test.mp4";

    CommandLineParser parser(argc, argv, keys);
    parser.about("Use this script to run object detection deep learning networks using OpenCV.");
    if (argc == 1 || parser.has("help"))

OpenCV skips using this file, and switches to camera instead. How can I hardcode the arguments, before they are passed to the Command Line Parser ?

Upvotes: 0

Views: 118

Answers (3)

MSalters
MSalters

Reputation: 179991

The problem is that argc,argv are an old-style C mechanism. You can't simply insert a value in the C array char**. Instead, you must create a new array with one extra element. (Your existing code overwrites argv[1])

Now there's more unusual stuff. argv is an array with argc+1 elements, the last one of which is nullptr. Since you're inserting a new element, you must create an char*[argc+2]*, copy all argc elements, append the pointer to your string, and then terminate the new array with nullptr.

You can now pass the new array and argc+1 to CommandLineParser.

Upvotes: 1

Paul-Marie
Paul-Marie

Reputation: 1063

The goal of argc and is to pass argument t"-input /home/Videos/test.mp4"); argv[2] = NULL;hrough the main, if you are using a GNU / UNIX distribution, just pass argument like it:

argc = 2
argv[1] = strdup("-input /home/Videos/test.mp4");
argv[2] = NULL;

CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run object detection deep learning networks using OpenCV.");
if (argc == 1 || parser.has("help"))

or just use "normal" way and pass argument through our binary, like

./a.out "-input /home/Videos/test.mp4"

Upvotes: -1

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14673

and what is argc? if it's 0, it'll be ignored, and pointer in argv will be NULL, so you get UB. Also, modifying system storage supplied to main is a bad idea, it's likely an UB because you don't have proper storage available to store string.

The only proper way about this is create intermediate variables. Note that array should be NULL-terminated.

Upvotes: 2

Related Questions