def unittest_fromfile(): print("Unittest opticalflow functions ...") timer = Timer() # read test video and show it liVideosDebug = glob.glob("data-set/04-chalearn/010/train/*/*.*") sVideoFile = random.choice(liVideosDebug) fLength = video_length(sVideoFile) arFrames = video2frames(sVideoFile, nMinDim=240) print("Video: %.1f sec | %s | %s" % (fLength, str(arFrames.shape), sVideoFile)) frames_show(arFrames, int(fLength * 1000 / len(arFrames))) # calc flow and save to disc print("Calculating optical flow ...") timer.start() arFlows = frames2flows(arFrames) print("Optical flow per frame: %.3f" % (timer.stop() / len(arFrames))) #flows2file(arFlows, "data-temp/unittest") # show color flows arFlowImages = flows2colorimages(arFlows) frames_show(arFlowImages, int(fLength * 1000 / len(arFrames))) # read flows from directory and display #arFlows2 = file2flows("data-temp/unittest", b3channels=True) #print(arFlows2.shape, np.mean(arFlows2[:,:,:,2])) #frames_show(arFlows2, 50) return
def unittest_opticalflow_fromcamera(): timer = Timer() # start video capture from webcam oStream = video_start(1, (320, 240), 15) # loop over action states while True: # show live video and wait for key stroke key = video_show(oStream, "green", "Press <blank> to start", "") # start! if key == ord(' '): # countdown n sec video_show(oStream, "orange", "Recording starts in ", sLower = None, \ tuRectangle = (224, 224), nCountdown = 3) # record video for n sec fElapsed, arFrames, _ = video_capture(oStream, "red", "Recording ", \ tuRectangle = (224, 224), nTimeDuration = 5, bOpticalFlow = False) print("\nCaptured video: %.1f sec, %s, %.1f fps" % \ (fElapsed, str(arFrames.shape), len(arFrames)/fElapsed)) # show orange wait box frame_show(oStream, "orange", "Calculating optical flow ...") # calculate and show optical flow arFrames = images_crop(arFrames, 224, 224) timer.start() arFlows = frames2flows(arFrames, bThirdChannel=True) print("Optical flow per frame: %.3f" % (timer.stop() / len(arFrames))) frames_show(flows2colorimages(arFlows), int(5 * 1000 / len(arFrames))) elif key == ord('f'): unittest_fromfile() # quit elif key == ord('q'): break # do a bit of cleanup oStream.release() cv2.destroyAllWindows() return