stage3_analyseVideo.py (1602B)
1 #This is: 2 # __ __ .___ __________ 3 # / \ / \| |\______ \ 4 # \ \/\/ /| | | ___/ 5 # \ / | | | | 6 # \__/\ / |___| |____| 7 # \/ (Work in progress) 8 # 9 ##### Video to single frames comes from: http://stackoverflow.com/a/19082750/4929236 ##### 10 11 import cv2 12 import argparse 13 14 parser = argparse.ArgumentParser() 15 16 parser.add_argument('video', type=string, 17 help='Video to analyse') 18 19 args = parser.parse_args() 20 21 cap = cv2.VideoCapture(args.video) 22 while not cap.isOpened(): 23 cap = cv2.VideoCapture(args.video) 24 cv2.waitKey(1000) 25 print "Wait for the header" 26 27 pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) 28 while True: 29 flag, frame = cap.read() 30 if flag: 31 # The frame is ready and already captured 32 cv2.imshow('video', frame) 33 pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) 34 print str(pos_frame)+" frames" 35 else: 36 # The next frame is not ready, so we try to read it again 37 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1) 38 print "frame is not ready" 39 # It is better to wait for a while for the next frame to be ready 40 cv2.waitKey(1000) 41 42 if cv2.waitKey(10) == 27: 43 break 44 if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): 45 # If the number of captured frames is equal to the total number of frames, 46 # we stop 47 break