comparison scripts/manual-video-analysis.py @ 886:d2eb8c93f7de

rename
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 21 Mar 2017 17:51:38 -0400
parents scripts/manual_video_analysis.py@7f61854fcc6d
children 4ea296ee1ae2
comparison
equal deleted inserted replaced
885:7f61854fcc6d 886:d2eb8c93f7de
1 #! /usr/bin/env python
2
3 import sys, argparse, cv2
4
5 parser = argparse.ArgumentParser(description=''''The program replays the video and allows to manually id vehicles and mark instants, eg when they cross given areas in the scene. Use this program in combination with a screen marker program (For example, Presentation Assistant) to draw multiple lines on the screen.''',
6 epilog = '''The output should give you a .csv file with the same name as your video file with columns in this format:
7 vehicle number, frame number
8 You can easily spot mistakes in the csv file for a line with number, SKIP. If this happens, just delete the previous vehicle observation.''',
9 formatter_class=argparse.RawDescriptionHelpFormatter)
10 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True)
11 parser.add_argument('-o', dest = 'outputFilename', help = 'name of the output file (csv file)')
12 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
13
14 args = parser.parse_args()
15
16 print('''Commands:
17 u: New vehicle crossing the first line
18 i: Vehicle crossing subsequent lines
19 o: Press o when you make a mistake in input
20 p: Press p for a new pedestrian event (eg crossing)
21 d: Skip 100 frames
22 s: Skip 10 frames
23 c: Go back 100 frames
24 x: Go back 10 frames
25 Spacebar: Go forward one frame
26 l: Skip to frame number
27 q: Quit and end program''')
28
29 cap = cv2.VideoCapture(args.videoFilename)
30 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum)
31 cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
32
33 # output filename
34 if args.outputFilename is None:
35 i = args.videoFilename.rfind('.')
36 if i>0:
37 outputFilename = args.videoFilename[:i]+'.csv'
38 else:
39 outputFilename = args.videoFilename+'.csv'
40 else:
41 outputFilename = args.outputFilename
42 vehNumber = 0
43 lineNum = -1
44 out = open(outputFilename, 'a')
45
46 while(cap.isOpened()):
47 ret, frame = cap.read()
48 frameNum = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))
49 cv2.putText(frame, str(frameNum), (1,20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0,0))
50 cv2.imshow('Video',frame)
51
52 key= cv2.waitKey(0)
53 #Change the keys to record the vehicle in this section
54 if key == ord('q'):
55 break
56 elif key == ord('u') or key == ord('i'):
57 if key == ord('u'):
58 vehNumber += 1
59 lineNum = 0
60 print('New Vehicle')
61 out.write('{},{}\n'.format(vehNumber,frameNum))
62 if vehNumber >= 1 and key == ord('i'):
63 lineNum = lineNum+1
64 print('Line number {}'.format(lineNum))
65 elif key == ord('o'):
66 out.write('{},SKIP\n'.format(vehNumber))
67 print('SKIPPED')
68 elif key == ord('p'):
69 print("New Pedestrian")
70 out.write('Pedestrian,{}\n'.format(frameNum))
71 #Change the number of frames skipped or the keys in this section
72 elif key == ord('d'):
73 cap.set(1,frameNum+100)
74 elif key == ord('s'):
75 cap.set(1,frameNum+10)
76 elif key == ord('a'):
77 cap.set(1,frameNum+1)
78 elif key == ord('x'):
79 cap.set(1,frameNum-10)
80 elif key == ord('c'):
81 cap.set(1,frameNum-100)
82 elif key == ord('l'):
83 frameNum = int(raw_input("Please enter the frame number you would like to skip to\n"))
84 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,frameNum-5)
85
86
87 cap.release()
88 cv2.destroyAllWindows()
89
90 #97a
91 #115s
92 #100d
93 #102f
94 #103g
95 #104h
96 #106j
97 #107k
98 #108l