微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从命令行打开/关闭相机

如何解决从命令行打开/关闭相机

我正在使用 python OpenCV 来录制进程的视频,并希望能够通过命令行输入打开/关闭相机。目前我是通过命令行输入打开相机,但是必须按某个键才能停止视频馈送并保存视频,这并不理想。

我尝试使用 pyautogui 模块使用第二个程序来触发第一个视频录制脚本结束录制所需的击键,但它不起作用。

如果可以让程序A开始录制并继续录制直到它被程序B触发,那将实现我想要做的事情。睡眠方法不起作用,因为过程长度变化很大。

理想情况下,开始的输入如下所示:

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

停止的输入看起来像:

camera_stop_program.exe --end True

我尝试了使用 python 输入和使用子进程在命令行上运行的各种组合,但没有任何运气。我无法让这两个程序相互通信。另外,该程序是用python编写的,将使用PyInstaller转换为exe文件

谢谢!

解决方法

试试这个:

from subprocess import check_call

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

或者你也可以试试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 &')

这里有更多选项:How to stop another already running script in python? https://www.raspberrypi.org/forums/viewtopic.php?t=245623

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。