3

I have the following program. When I run it with pythonw to suppress the console, it will run for ~40 seconds then close. However, when I run it with python and the console open so I can catch errors, it runs until I shut it down. I'm not sure why it is behaving this way, or how to debug it. The program is below.

motion detector.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()
jeffpkamp
  • 2,732
  • 2
  • 27
  • 51
  • When you run it from the console is any output or are any errors ever displayed? – martineau Jan 16 '15 at 01:09
  • 1
    Your prints might be causing an unintentional synchronization when you run as console. Try commenting those out and running on the console to see what happens. – David Anderson Jan 16 '15 at 01:15
  • 1
    Catch all exceptions and write them to a log. – Voo Jan 16 '15 at 10:41
  • 1
    @Dave76 The print commands appeared to be the problem. Once I made them into comments the program ran all night without incident. Thanks. I – jeffpkamp Jan 16 '15 at 16:16

0 Answers0