I am trying to run a face detection code in openCV and I don't want to use the command prompt to run it but I don't know what to give as an input argument to load_object_detect.
here is the sample from the code:
CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
In order for the above command to execute the code should be run from the command prompt which I am trying to avoid for now...
All I know is that its input type is const char* cascade_path...
try argv[2] = whatever path you want to put;
just before calling the function
Something like this
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
argv[2] = "SuvP";
std::cout<<"Hey "<<argv[2]<<endl;
return 0;
}
The output is Hey SuvP
The arguments from command line are stored in the array argv. Alternatively we are filling data in the array within the code and not from the command line in this case.
Related
First off, sorry if the title makes no sense. The nature of my question makes it very hard for me to phrase.
I am working on an assignment for my datastructures class and I am completely and totally brand new to c++ due to only having learned Java at my old school. The project is a weather logger that reads in data from a text file climatedata.txt. My teacher has provided us with a main function in the file (that we are NOT allowed to modify in any way) weatherlog.cpp which is below.
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
}
Initially I was confused as to why the program would never fully execute until I figured out what argc is in the scope of a main function. It seems that he wants me to provide the text file name while compiling so that argc will be 2 instead of 1 (the value I saw when debugging) so that it can actually execute the rest of the program instead of immediately exiting.
My problem is I'm not sure how to provide the program with the text file location. I've looked all over the internet but since I'm not even sure at which stage to provide the file path I haven't had any success. Is that information supposed to be passed when compiling with g++? After successfully compiling when I'm trying to run the executable? What does the terminal command to do so look like?
So I understand that you need to provide a file name in argv (Comment below if I'm incorrect). argv is an array of arguments passed by the commandline, and argc is the amount of arguments passed (automatically set). To do that simply call the program in terminal like this: ./<progam> <file_name>
Example:
compile just as you would a hello world progam.
Call the program weatherlog climatedata.txt.
If your file has spaces in its name either remove them or do this enclose its name in quotes.
argc stores number of passed in parameters, while argv points to parameters.
if (argc != 2) means checking number of input parameters passed in via Console mode. The first parameter is always the program name. From the second parameter you can pass anything you want. char* datafile = argv[1]; means taking the second parameter as data filename.
In short, open Console mode (CMD on Windows, Terminal on Linux) and type something like: yourprogram C:\path\to\climatedata.txt.
I am using opencv, and have a function that takes command line variables:
int start(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, keys);
if(parser.has("help")) {
parser.printMessage();
return 0;
}
....
}
what I would like to do, is have another function that passes variables to this one, instead of using the command line, as it is now.
the command line I pass it to run perfectly is:
-h=6 -w=8 -pf=defaultConfig.xml -t=charuco -v=bmCalib2.mov -sz=0.045 -d=1
So I have this function:
void main()
{
char* passArg[7] = { "-h=6", "-w=8","-pf=defaultConfig.xml", "-t=charuco","-v=bmCalib2.mov", "-sz=0.045", " -d=1" };
start(7,passArg);
}
It compiles and runs, but the results are different (it is a calibration application, the command line calibrates, and the function version does not).
Is this the correct way to send int argc, char** argv instead of using command line?
thank you.
Ah, already solved. I needed to adjust my char array to:
char* passArg[8] = { NULL,"-h=6", "-w=8","-pf=defaultConfig.xml", "-t=charuco","-v=bmCalib2.mov", "-sz=0.045", " -d=3" };
Obviously the .exe is usually the first variable given in a command line launch.
I know I can launch a executable in MATLAB with the !example.exe command or system(example.exe) as laid out in this question. However, when I want to pass arguments to the C++ program, anything I type is taken as a string. How can I use MATLAB variables?
For example, let's say example.exe was the following program:
int main(int argc, char *argv[]){
std::cout << argv[1] << std::endl;
}
When I call it from MATLAB as !example.exe hi, I get the output hi.
But what if my MATLAB script were actually:
hi = 'HELLO!';
!example.exe hi
My output is still hi, but I want it to be HELLO!.
How do I do this?
Thanks in advance!
You need to create a string using your variables and then run it using eval. For example:
n = 3;
command = sprintf('!example.exe %i', n);
eval(command)
Don't know if this is what you are looking for, but you can put the execution command together in a string using sprintf and then pass that to system. Something like:
hi = 'Hello!';
command = sprintf('example.exe %s', hi);
system(command);
Just use string concatenation:
hi = 'HELLO!';
system(['example.exe ' hi]);
In my C++ program, I can use ImageMagick commands through the system call. For example, to display an image named button_out.miff, I can put the following code in my program:
system("display button_out.miff -display :0");
Now I want to pass the image file name "button_out.miff" as a parameter to my c++ program. For example, if my compiled program is named test.exe, I wish when I run the command:
test.exe button_out.miff
my program can pass button_out.miff as a parameter to the ImageMagick display command. How could I do that?
I guess it’s more about string concatenation?
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
string filename = CW2AEX<>(argv[1]);
stringstream ss;
ss << "display " << filename << "-display :0";
system(ss.str().c_str());
}
}
Basically I am making a Python program and part of it needs to run a C++ executable, I am calling the exe with:
subprocess.call(["C:\\Users\\User\\Documents\\Programming\\Python\\Utilities\\XMLremapper\\TranslatorSource\\FileFixer.exe", "hi"])
But how do I make the C++ program read the input? I tried:
FILE * input = popen("pythonw.exe", "r");
cout<< input.getline() << endl << endl;
But that just outputs 0x22ff1c and definitely not "hi". What code is needed to pipe the input into the C++ program?
They are passed as parameters to the main function.
main(int argc, char *argv[])
argc is the length of argv. So it would be
main(int argc, char *argv[])
{
cout<<argv[1];
return 0;
}
If you just want to pass in a few arguments, an easy option is to read arguments on the command line, as suggested in another answer.
For more substantial input/output, where you'd naturally want to use cout or cin, a better option is to use subprocess.Popen. You can then write to and read from the other process as if they were file handles in python. For example:
proc = subprocess.Popen(["FileFixer.exe"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate("hi\n")
This tells python to run the process, passing in 'hi' followed by carriage return as standard input, which can then be read by cin in the C++ program. Standard output (the result of cout in C++) is then passed into the stdout list, and standard error is passed into stderr.
If you need more interactive communication, you can also access proc.stdout and proc.stdin as if they were filehandles in python (e.g. proc.stdin.write("hi\n")