【发布时间】:2017-12-30 11:12:27
【问题描述】:
我一直在为 raspberry pi 相机编写一些程序,即一个只显示相机输出的程序,一个拍照然后应用人脸识别的程序等等。我得到了大部分这些工作,但我无法将所有这些都整合到 1 个程序中。
我在想的只是简单的按钮,而不是按下时会执行相关的 python 脚本,为了简单起见,假设我只想执行最简单的按钮,只是相机的输出。我有这样写的代码。
import cv2
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
camera =PiCamera()
camera.resolution=(320, 240)
camera.framerate=30
rawCapture=PiRGBArray(camera, size=(320, 240))
time.sleep(1)
for frame in camera.capture_continuous(rawCapture, format="bgr" ,
use_video_port=True):
image=frame.array
cv2.imshow("Faces" , image)
key = cv2.waitKey(1)
rawCapture.truncate(0)
现在我有另一个代码,主要在 tkinter 中完成,这只是调用代码以执行相机脚本的按钮。我希望它看起来像这样。
from Tkinter import *
def executeCameraScript():
#Code for the camera script to trigger
def option2():
#x
def option3():
#y
root = Tk()
frame= Frame(root)
frame.pack()
button=Button(frame,
text="Turn on the Camera",
command=executeCameraScript)
button.pack(side=LEFT)
button=Button(frame,
text="Option3",
command=option2)
button.pack(side=LEFT)
button=Button(frame,
text="Option3",
command=option3)
button.pack(side=LEFT)
root.mainloop()
我的问题是我应该使用什么,以便当我按下打开相机按钮时,它会执行相机脚本(最好不关闭按钮框架,这样我也可以从那里关闭脚本),有什么简单的吗?怎么做?
【问题讨论】:
-
在其他脚本中,您可以将代码放入函数中,然后您可以像其他模块一样
import该文件,并从该脚本运行函数。 -
你也可以使用模块
subprocess来运行任何程序。 -
顺便说一句:如果其他脚本运行时间很长,那么它会阻塞主脚本,因此它会冻结 - 所以你必须使用模块
threading在线程中运行它` -
你总能把它变成一个函数吗?
-
功能没问题。使用函数中的代码,您可以控制何时运行它。问题是您的脚本是长时间运行的程序,因此它会阻止 tkinter - 您必须在线程中运行它。
标签: python opencv tkinter camera raspberry-pi