我有以下程序。当我用pythonw运行它以抑制控制台时,它将运行约40秒然后关闭。但是,当我使用python运行它并且控制台打开以便我可以捕获错误时,它会一直运行直到我关闭它。我不确定它为什么会这样,或者如何调试它。该计划如下。
运动检测器.pyw
import numpy as np
import cv2, time, os, Tkinter,threading, thread
from Tkinter import *
class stuff:
rectime=0
delay=0
kill=0
def getter():
try:
stuff.rectime=int(e.get())
stuff.delay=int(e2.get())
except ValueError:
if stuff.rectime==0:
stuff.rectime=int(15)
if stuff.delay==0:
stuff.delay=int(10)
box.destroy()
def hello(n):
for i in range(n):
print "Hello"
def preview():
s, img = cap.read()
while True:
cv2.imshow('Preview', img)
s, img=cap.read()
k = cv2.waitKey(30) & 0xff
if k == 27 or stuff.kill==1:
cv2.destroyAllWindows()
stuff.kill=0
break
def mbox():
def closewindow():
Messagebox.destroy()
stuff.kill=1
Messagebox=Tk()
Messagebox.wm_title("Info")
l3=Label(Messagebox, text="This is your preview! Align camera then press Okay or Enter start")
b3=Button(Messagebox, text="Okay", command=closewindow)
l3.grid(row=1,column=1)
b3.grid(row=2,column=1)
b3.bind('<Return>', lambda _: closewindow())
Messagebox.after(4, lambda: Messagebox.focus_force())
b3.focus_set()
Messagebox.mainloop()
def mbox2():
def closewindow():
Messagebox.destroy()
stuff.kill=1
Messagebox=Tk()
Messagebox.wm_title("Info")
l3=Label(Messagebox, text="Press Okay to End Program")
b3=Button(Messagebox, text="Okay", command=closewindow)
l3.grid(row=1,column=1)
b3.grid(row=2,column=1)
b3.bind('<Return>', lambda _: closewindow())
Messagebox.after(4, lambda: Messagebox.focus_force())
b3.focus_set()
Messagebox.iconify()
Messagebox.mainloop()
box=Tk()
box.wm_title("Program Details")
e=Entry(box,bd=3)
e2=Entry(box,bd=3)
b=Button(box, text="Enter",command=getter)
l=Label(box,text="Record Time",justify=CENTER)
l2=Label(box,text="Prog. Delay",justify=CENTER)
l.grid(row=1,column=1)
e.grid(row=1,column=2)
l2.grid(row=2,column=1)
e2.grid(row=2,column=2)
b.grid(row=3,column=2)
e.focus_set()
b.bind('<Return>', lambda _: getter())
box.mainloop()
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter('C:\motion\\output.avi',cv2.cv.CV_FOURCC('F','M','P','4'), 30, (640,480),True)
threads = []
#h=threading.Thread(target=hello(5))
thread.start_new_thread(preview, ())
time.sleep(1)
#thread.start_new_thread(mbox, ())
mbox()
time.sleep(1)
for i in range(stuff.delay):
time.sleep(1)
print "program will start in", stuff.delay-i,'seconds','\r',
print "\nStarting"
thread.start_new_thread(mbox2, ())
fgbg = cv2.BackgroundSubtractorMOG()
ctime=time.time()
ptime=time.time()
value=0
while True:
if out.isOpened() is False:
out.open('C:\motion\\output.avi',cv2.cv.CV_FOURCC(*'FMP4'),30,(640,480),True)
ret, frame = cap.read()
fgmask = fgbg.apply(frame)
out.write(frame)
if time.time()>ctime+2:
fgbg=cv2.BackgroundSubtractorMOG()
ret, frame = cap.read()
ctime=time.time()
value=cv2.countNonZero(fgmask)
print value,'\r',
if value > 100:
print "\n"
movement=time.time()
while time.time()<int(movement)+stuff.rectime:
out.write(frame)
ret, frame = cap.read()
print int(time.time())-int(movement),'\r',
if out.isOpened() is True:
out.release()
print "video captured" + time.strftime('%m/%d/%y at %H:%M:%S')
os.rename('C:\motion\\output.avi','c:\motion\\' + time.strftime('%m-%d-%y_%H-%M-%S') + '.avi')
if time.time()>int(ptime)+stuff.rectime:
ptime=time.time()
out.release()
k = cv2.waitKey(10)
if k == 27 or stuff.kill==1:
break
cap.release()
cv2.destroyAllWindows()