0

I am attempting to send a character input from one python script to another (run as a subprocess with subprocess.Popen() and subprocess.Popen.send_signal()). The sub-process uses openCV's cv2.waitKey(x) == ord('y') to determine when to perform a specific operation.

I've read that openCV's waitKey doesn't listen to console input, so how can I communicate with it? Is there a better way to send a signal to an openCV window as a subprocess?

EDIT: The subprocess does NOT need to communicate with the parent process. The parent simply needs to send a signal to the subprocess' waitKey(). Simply terminating the subprocess will cause it to exit before completing desired operations.

My first attempt (confident that it would fail) looked something like this:

path-to-python-environment = "/home/user1/virtual-env/bin/python3"
path-to-python-script = "/home/user1/scripts/myScript.py"

while true:
    inputValue = input("enter value")

    if(inputValue == "start"):
        p = subprocess.Popen([path-to-python-environment, path-to-python-script])
    elif(inputValue == "function-y"):
        p.send_signal('y')
#assuming that user will send "start" BEFORE "function-y"

The subprocess file runs in the specified environment and uses a simple loop, which is broken on a waitKey() input. It looks somewhat like:

import cv2
while true:
    img = cv2.imread("image1.png")
    cv2.imshow("picture",img)
    if cv2.waitKey(10) == ord('y')
        break
print("some string")
cv2.destroyAllWindows()
  • 1
    [mre] please, or at least some code that shows what you actually do. -- if the subprocess needs to tell the parent something, IDK if that could be done with signals. it CAN be done by the child printing something to stdout/stderr and the parent catching that. -- NO, you can't mix GUI code with other kinds of asynchronicity. `waitKey()` is GUI code, it's the crucial call in a GUI event loop. you need to run your subprocess parenting stuff in a thread. -- no, you can't abort `waitKey()` from elsewhere. make sure it's not waiting too long, and check for situations the GUI has to handle (messages) – Christoph Rackwitz Jun 29 '23 at 08:14
  • @ChristophRackwitz I have since updated the post to reflect this feedback (thanks!). – theRockers Jul 03 '23 at 18:29
  • waitKey(10) will wait for about 10 milliseconds, or less. that's imperceptible to humans, meaning I don't see the problem. the usual solution is to interleave this processing. do the waitKey(), then check for "other stuff to do", then waitKey() again. -- in this case, I'd recommend sticking to GUI interaction purely. your child process is a GUI process. you know the PID of the child. given that PID, you can send a window message (GUI event) to the child's GUI. you could send an Escape key code, then react to that in the child. – Christoph Rackwitz Jul 03 '23 at 19:45
  • `send_signal()` doesn't send a character, it sends a POSIX signal such as SIGHUP, SIGTERM, SIGUSR1 so you'd need to set up a signal handler in the receiving process - best reference is here https://pymotw.com/3/signal/index.html – Mark Setchell Jul 03 '23 at 20:07

0 Answers0