1

My goal : To display a simple message box ("Object Detected") whenever an object is detected in the webcam.

Ive got a C++ opencv code which displays the webcam (display_cam.cc).

Ive another basic PySimpleGUI code which is being run inside another c++ code which simply displays a text box with text "Object Detected" (gui.cc).

Inside the display_cam.cc I entered this line system("./gui");

Now the problem is that whenever the camera detects an object, the GUI box flashes but then its halting/pausing the code. Ive to close the GUI box and then the camera stream continues, until an object is detected again and the code pauses again with GUI flash.

2 Answers2

0

std::system will always block until the command exits. It needs to, because it returns the command's exit code.

You have a few ways around this:

  1. Put the std::system() call in its own std::thread.

  2. Use platform-specific features in the command string to make it non-blocking. For example, on Linux, macOS, or any other platform where std::system() happens to get you a Posix shell, you can try:

    system("./gui &")
    

    The trailing ampersand (&) runs the process in the background.

  3. Use other platform-specific APIs instead of std::system(). See Non-blocking version of system() (which is about C APIs, not specifically C++, but the solutions should be compatible).

  4. Somehow rewrite your ./gui program to exit its main process immediately while keeping its graphical window open. Again, using platform-specific process control APIs.

Maxpm
  • 24,113
  • 33
  • 111
  • 170
0

If your goal is for the "./gui" process to run independently of (i.e. in parallel with) the execution of parent process, and you're on a Mac or Linux/Unix system, the easiest way to achieve that is:

system("./gui &");   // the ampersand means "run in the background"

If you need to run on Windows, OTOH, you'd probably need to call the Win32 CreateProcess() function instead.

Spawning a child process simply to present a message dialog isn't a very good design, though, so if possible I suggest creating a message dialog from within your existing GUI process instead.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • How to then kill this process ? Else there could be lots of such processes being spawned whenever an object is detected – vinita Jun 06 '21 at 08:53