def main(): usage = """run_kalmanfilter.py [input_avi_file] [optic_flow_path] [output_avi_file] [threshold] HydraGL. State space model using an extended Kalman filter to track Hydra in video Dependencies: -Vispy* -Numpy -PyCuda** -DistMesh -HDF -OpenCV2 -matplotlib Notes: * Uses OpenGL rendering. If using remotely, you'll need to set up a VirtualGL server ** If have a CUDA compatible graphics card Example: ./run_kalmanfilter.py ./video/johntest_brightcontrast_short.avi ... ./video/johntest_brightcontrast_short/flow ./video/output.avi -s 15 For help: ./run_kalmanfilter.py -h Ben Lansdell 02/16/2016 """ parser = argparse.ArgumentParser() parser.add_argument('fn_in', default='./video/johntest_brightcontrast_short.avi', help='input video file, any format readable by OpenCV', nargs = '?') parser.add_argument('flow_in', default='./video/johntest_brightcontrast_short/flow', help='input optic flow path', nargs = '?') parser.add_argument('fn_out', default='./video/johntest_brightcontrast_short_output.avi', help='avi output video file', nargs='?') parser.add_argument('-n', '--name', default='johntest_brightcontrast_short', help='name for saving run images', nargs='?') parser.add_argument('-t', '--threshold', default=9, help='threshold intensity below which is background', type = int) parser.add_argument('-s', '--gridsize', default=22, help='edge length for mesh (smaller is finer; unstable much further below 18)', type = int) parser.add_argument('-c', '--cuda', default=True, help='whether or not to do analysis on CUDA', type = bool) args = parser.parse_args() if len(sys.argv) == 1: print("No command line arguments provided, using defaults") capture = VideoStream(args.fn_in, args.threshold) frame = capture.current_frame() mask, ctrs, fd = capture.backsub() distmesh = DistMesh(frame, h0 = args.gridsize) distmesh.createMesh(ctrs, fd, frame, plot = True) #Load flow data from directory flowstream = FlowStream(args.flow_in) ret_flow, flowframe = flowstream.peek() if ret_flow: kf = IteratedMSKalmanFilter(distmesh, frame, flowframe, cuda = args.cuda, sparse = True, multi = True) else: print 'Cannot read flow stream' return #kf.compute(capture.gray_frame(), flowframe) nI = 3 count = 0 while(capture.isOpened()): count += 1 print 'Frame %d' % count ret, frame, grayframe, mask = capture.read() ret_flow, flowframe = flowstream.read() if ret is False or ret_flow is False: break #for i in range(nI): # print 'Iteration %d' % i # raw_input("Finished. Press Enter to continue") # kf.compute(grayframe, flowframe) kf.compute(grayframe, flowframe, mask, imageoutput = 'screenshots/' + args.name + '_frame_' + str(count)) capture.release() output.release() cv2.destroyAllWindows() raw_input("Finished. Press ENTER to exit")
video, flow = test_data(680, 680) #video, flow = test_data_texture(680, 680) #video, flow = test_data_image() flowframe = flow[:,:,:,0] frame = video[:,:,0] #Make mesh distmesh = DistMesh(frame, h0 = gridsize) mask, ctrs, h = findObjectThreshold(frame, threshold = threshold) distmesh.createMesh(ctrs, h, frame, plot=False) nx = 680 start = nx//3 end = 2*nx//3 kf = IteratedMSKalmanFilter(distmesh, frame, flowframe, cuda) #Now perturb the positions a bit... kf.state.X = kf.state.X*1.2 kf.state.refresh() rend = kf.state.renderer cuda = kf.state.renderer.cudagl kf.state.render() state = kf.state nx = nx deltaX = -2 nF = video.shape[2] nI = 10 count = 0
#- ave per update jacobain time #- ave per update hessian time data = np.zeros((len(gridsizes), 11)) for idx, gridsize in enumerate(gridsizes): #idx = 1; gridsize = 40 print 'Running KF for gridsize =', gridsize flowframe = flow[:,:,:,0] frame = video[:,:,0] distmesh = DistMesh(frame, h0 = gridsize) mask, ctrs, h = findObjectThreshold(frame, threshold = threshold) distmesh.createMesh(ctrs, h, frame, plot=False) kf = IteratedMSKalmanFilter(distmesh, frame, flowframe, cuda, multi = True) kf.state.render() count = 0 for i in range(3): count += 1 print 'Frame %d' % count frame = video[:,:,i] mask = (frame > 0).astype(np.uint8) flowframe = flow[:,:,:,i] time.sleep(1) kf.compute(frame, flowframe, mask) #Extract stats meshpts = stats.meshpts
(mask, ctrs, fd) = findObjectThreshold(tracking_mask, threshold = threshold) #distmesh = DistMesh(refframe, h0 = gridsize) #distmesh.createMesh(ctrs, fd, refframe, plot = True) #Save this distmesh and reload it for quicker testing #distmesh.save(dm_out) distmesh = DistMesh(refframe, h0 = gridsize) distmesh.load(dm_out) refpositions = distmesh.p #Create dummy input for flow frame flowframe = np.zeros((nx, nx, 2)) #Create Kalman Filter object to store mesh and make use of plotting functions kf = IteratedMSKalmanFilter(distmesh, refframe, flowframe, cuda = cuda) N = kf.size()/4 totalframes = 0 for vidx in range(nV): #Load MFSF data a = loadmat(mfsf_matfiles[vidx]) params = a['parmsOF'] u = a['u'] v = a['v'] if vidx > 0: #Skip to this frame and create mesh capture = TIFFStream(vid_path_in + fn_ins[vidx], threshold) nx = capture.nx nF = capture.nframes
#Make mesh distmesh = DistMesh(frame, h0 = gridsize) mask, ctrs, h = findObjectThreshold(frame, threshold = threshold) distmesh.createMesh(ctrs, h, frame, plot=False) nx = 680 start = nx//3 end = 2*nx//3 #kf = KalmanFilter(distmesh, frame, flowframe, cuda) #kf = IteratedKalmanFilter(distmesh, frame, flowframe, cuda) #kf = IteratedKalmanFilter(distmesh, frame, flowframe, cuda, sparse = False) #kf = KalmanFilterMorph(distmesh, frame, cuda) kf = IteratedMSKalmanFilter(distmesh, frame, flowframe, cuda, multi = True) #kf = MSKalmanFilter(distmesh, frame, flowframe, cuda) rend = kf.state.renderer cuda = kf.state.renderer.cudagl kf.state.render() state = kf.state nx = nx deltaX = -2 #Test creation of multi-perturbations nF = video.shape[2] nI = 10 count = 0