def main():
cap = cv2.VideoCapture(0)
frame_size = (int(cap.get(3)), int(cap.get(4)))
fourcc = cv2.cv.CV_FOURCC(*"XVID")
prev_frame = None
last_motion = None
motion_filename = None
motion_file = None
while cap.isOpened():
now = datetime.datetime.now()
success, frame = cap.read()
assert success, "failed reading frame"
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame_gray = cv2.GaussianBlur(frame_gray, (21, 21), 0)
if have_motion(prev_frame, frame_gray):
if motion_file is None:
motion_filename = now.strftime("%Y_%m_%d_%H_%M_%S_MOTION.avi")
motion_file = cv2.VideoWriter(motion_filename, fourcc, 20.0, frame_size)
last_motion = now
print "Motion!", last_motion
if motion_file is not None:
motion_file.write(frame)
if now - last_motion > MOTION_RECORD_TIME:
motion_file.release()
motion_file = None
Process(target = push_file, args = (motion_filename, )).start()
prev_frame = frame_gray
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
评论列表
文章目录