annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import sys, argparse, cv2
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
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.''',
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
6 epilog = '''The output should give you a .csv file with the same name as your video file with columns in this format:
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
7 vehicle number, frame number
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
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.''',
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
9 formatter_class=argparse.RawDescriptionHelpFormatter)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True)
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
11 parser.add_argument('-o', dest = 'outputFilename', help = 'name of the output file (csv file)')
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
12 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
13
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14 args = parser.parse_args()
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
15
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
16 print('''Commands:
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
17 u: New vehicle crossing the first line
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
18 i: Vehicle crossing subsequent lines
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
19 o: Press o when you make a mistake in input
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
20 p: Press p for a new pedestrian event (eg crossing)
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
21 d: Skip 100 frames
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
22 s: Skip 10 frames
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
23 c: Go back 100 frames
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
24 x: Go back 10 frames
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
25 Spacebar: Go forward one frame
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
26 l: Skip to frame number
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
27 q: Quit and end program''')
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
28
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 cap = cv2.VideoCapture(args.videoFilename)
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
30 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum)
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
31 cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
32
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
33 # output filename
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
34 if args.outputFilename is None:
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
35 i = args.videoFilename.rfind('.')
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
36 if i>0:
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
37 outputFilename = args.videoFilename[:i]+'.csv'
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
38 else:
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
39 outputFilename = args.videoFilename+'.csv'
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40 else:
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
41 outputFilename = args.outputFilename
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
42 vehNumber = 0
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
43 lineNum = -1
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
44 out = open(outputFilename, 'a')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 while(cap.isOpened()):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
47 ret, frame = cap.read()
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
48 frameNum = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
49 cv2.putText(frame, str(frameNum), (1,20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0,0))
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
50 cv2.imshow('Video',frame)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
51
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52 key= cv2.waitKey(0)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
53 #Change the keys to record the vehicle in this section
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
54 if key == ord('q'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
55 break
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
56 elif key == ord('u') or key == ord('i'):
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
57 if key == ord('u'):
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
58 vehNumber += 1
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
59 lineNum = 0
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
60 print('New Vehicle')
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
61 out.write('{},{}\n'.format(vehNumber,frameNum))
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
62 if vehNumber >= 1 and key == ord('i'):
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
63 lineNum = lineNum+1
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
64 print('Line number {}'.format(lineNum))
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
65 elif key == ord('o'):
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
66 out.write('{},SKIP\n'.format(vehNumber))
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
67 print('SKIPPED')
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
68 elif key == ord('p'):
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
69 print("New Pedestrian")
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
70 out.write('Pedestrian,{}\n'.format(frameNum))
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71 #Change the number of frames skipped or the keys in this section
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72 elif key == ord('d'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
73 cap.set(1,frameNum+100)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
74 elif key == ord('s'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
75 cap.set(1,frameNum+10)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
76 elif key == ord('a'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
77 cap.set(1,frameNum+1)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
78 elif key == ord('x'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
79 cap.set(1,frameNum-10)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
80 elif key == ord('c'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
81 cap.set(1,frameNum-100)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
82 elif key == ord('l'):
885
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
83 frameNum = int(raw_input("Please enter the frame number you would like to skip to\n"))
7f61854fcc6d first updated version of manual data collection script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 884
diff changeset
84 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,frameNum-5)
884
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
85
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
86
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
87 cap.release()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
88 cv2.destroyAllWindows()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
89
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
90 #97a
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
91 #115s
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
92 #100d
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
93 #102f
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
94 #103g
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
95 #104h
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
96 #106j
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
97 #107k
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
98 #108l