使用subprocess模块运行python detect.py --weights ./runs/train/exp19/weights/best.pt --source ./test1/101/ --save-txt --save-crop怎么写
时间: 2023-09-10 11:07:13 浏览: 45
你可以使用subprocess模块中的run()函数来运行该命令。具体代码如下:
```python
import subprocess
command = ["python", "detect.py", "--weights", "./runs/train/exp19/weights/best.pt", "--source", "./test1/101/", "--save-txt", "--save-crop"]
result = subprocess.run(command, capture_output=True, text=True)
print(result.stdout)
```
其中,command是要执行的命令,使用列表形式传入每个参数。capture_output=True表示将标准输出和标准错误输出捕获到result对象中,text=True表示将输出转换成字符串格式。最后通过print(result.stdout)打印运行结果。
相关问题
使用subprocess模块只需要运行python detect.py --weights ./runs/train/exp19/weights/best.pt --source ./test1/101/ --save-txt --save-crop怎么写
你可以使用以下代码来运行命令:
```python
import subprocess
command = "python detect.py --weights ./runs/train/exp19/weights/best.pt --source ./test1/101/ --save-txt --save-crop"
subprocess.run(command, shell=True)
```
其中,`subprocess.run()` 函数可以运行一个命令,并等待其执行完毕。`shell=True` 参数表示在 shell 中运行该命令。你需要将命令字符串赋值给 `command` 变量。然后,调用 `subprocess.run()` 函数来执行该命令。在这个例子中,它将运行命令 `python detect.py --weights ./runs/train/exp19/weights/best.pt --source ./test1/101/ --save-txt --save-crop`。
如何使用python中的subprocess.Popen运行/root/fwbot/autosend.py
你可以使用以下代码在Python中调用subprocess.Popen运行/root/fwbot/autosend.py:
import subprocess
proc = subprocess.Popen(['/usr/bin/python', '/root/fwbot/autosend.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out.decode('utf-8')) # print输出stdout
print(err.decode('utf-8')) # print输出stderr