gpt4 book ai didi

opencv - 如何在不终止子进程的情况下在被调用进程中使用子进程的输出?

转载 作者:太空宇宙 更新时间:2023-11-03 22:31:56 25 4
gpt4 key购买 nike

我在 python 中创建了两个模块。其中一个模块用于使用 Tkinter 创建 GUI,第二个模块用于捕获和存储图像。当我在 Tkinter 模块中调用 opencv 模块时,它首先运行 opencv 模块,在释放相机后,它运行 Tkinter 模块。所以我使用了子进程。打开()。现在我想将子进程输出到 Tkinter 模块中。使用Tkinter创建GUI的代码如下。

import sys
from Tkinter import *
import Tkinter
import subprocess
def capcam():
command="python2 imacap.py"
subprocess.Popen(command,shell=True)
root=Tk()
add=Frame(root)
add.grid()
root.title("Test")
capcam()
button_height=11
button_width=29
button_ipadx=2
button_ipady=2
text_box = Entry(add,justify=RIGHT,width=100, font=100)
text_box.grid(row = 0, column = 1,columnspan = 5)
text_box.insert(0, "0")
bttn_3 = Button(add, height= button_height ,width= button_width,text = "3")
bttn_3.grid(row = 3, column = 2, padx=button_ipadx, pady=button_ipady)

子进程代码如下。

import cv2.cv as cv
capture = cv.CaptureFromCAM(0)
num = 0
while True:
img = cv.QueryFrame(capture)
cv.SaveImage('pic'+str(num)+'.jpg', img)
if num == 500:
del(capture)
break
if cv.WaitKey(10) == 27:
break
num += 1

我想在运行时在主进程中获取 num 变量的值,并希望在不终止子进程的情况下将其传递给入口。

最佳答案

持有引用

p = subprocess.Popen(command,shell=True, stdout = subprocess.PIPE)

然后你可以做

num = int(p.stdout.readline()) # blocks!

如果你这样做

print(num) in the child process

另请查看模块 multiprocessing。它可以通过其他方式解决问题。

关于opencv - 如何在不终止子进程的情况下在被调用进程中使用子进程的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com