0

I am using python OpenCV to record videos of a process and want the ability to turn the camera on/off with a command line input. Currently I am turning the camera on with a command line input, but have to press a certain key to stop the video feed and save the video, which is not ideal.

I attempted using the pyautogui module to use a second program to trigger the keystroke that the first video recording script needed to end the recording, but it does not work.

If it is possible to have program A start recording and continue recording until it is triggered by program B, that would achieve what I want to do. Sleep methods do not work because the process length varies greatly.

Ideally the input to start would look like:

camera_recording_program.exe --save C:/path --camera 1

and the input to stop would look like:

camera_stop_program.exe --end True

I have tried various combinations of using python inputs and running things on the command line with subprocess without any luck. I am not able to get the two programs to communicate to each other. Also, the program is written in python and will use PyInstaller to convert to exe file.

Thank you!

paperstsoap
  • 335
  • 4
  • 13
  • What you can do is set an env variable which is True at the beginning of the first script and in the main loop you can have a condition `if stopvid == False: break` and in the other file you can set the value of that env variable as false when you run that file – Atharva Gundawar Jun 25 '21 at 03:23
  • @AtharvaGundawar Do you have any references or examples you can point me in the direction of? I have never done anything like that before, but conceptually I understand your suggestion. – paperstsoap Jun 25 '21 at 13:05
  • Ill put up an answer. – Atharva Gundawar Jun 25 '21 at 14:04

1 Answers1

0

Try This:

from subprocess import check_call

check_call(["pkill", "-f", "filetostop.py"])

Or you can also try this

import os,signal
import time
 
def get_now_time():
    # Get current local time
    now_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    return now_time
 
def kill(pid):
    print('pid',pid)
    # pgid=os.getpgid(pid)
    # print(pgid)
    # a = os.killpg(pgid,signal.SIGKILL)
    a = os.kill(pid,signal.SIGKILL)
         Print('The process that killed pid to %s, the return value is: %s' % (pid, a))
 
def kill_target(target):
    cmd_run="ps aux | grep {}".format(target)
    out=os.popen(cmd_run).read()
    for line in out.splitlines():
        print(line)
                 If 'also judges the path where the kill is going' in line:
            pid = int(line.split()[1])
            kill(pid)
 # It is recommended to add the & symbol after the run command, it seems to be running in another thread
os.system('python ./run.py &') 
       
while True:
         # shut down 
    if some_condition:
                 Print('pause')
        kill_target('run.py')
         # turn on
    if some_else_condition:
        os.system('python ./run.py &')

and here are some more options : How to stop another already running script in python? https://www.raspberrypi.org/forums/viewtopic.php?t=245623

Atharva Gundawar
  • 475
  • 3
  • 10