comparison scripts/manual_video_analysis.py @ 885:7f61854fcc6d

first updated version of manual data collection script
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 21 Mar 2017 17:48:14 -0400
parents ac4bcbcc9cda
children
comparison
equal deleted inserted replaced
884:ac4bcbcc9cda 885:7f61854fcc6d
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 2
3 import sys, argparse, cv2 3 import sys, argparse, cv2
4 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.') 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)
6 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) 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)')
7 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int) 12 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int)
8 13
9 #Place this program in the same folder as the video you want to analyze 14 args = parser.parse_args()
10 #Use this program in combination with a screen marker program (For example, Presentation Assistant) to draw multiple lines on the screen
11 #Press the "New vehicle" key (u) when a vehicle touches the first line. This will start a new line in the csv file and record the first frame number.
12 #Press the "Next line" key (i) when the same vehicle touches every line except the first one, in order. This will record the subsequent frame numbers.
13 #Press the SKIP key (o) when you make a mistake in the input. This will write SKIP in the csv file so that you can easily identify where you should remove the line
14 #The output should give you a .csv file with the same name as your video file with columns in this format:
15 #vehicle number, frame number at first line, frame number at second line, frame number at third line, etc...
16 #You can easily spot mistakes in the csv file if the number of columns is wrong for a specific line, or you see a SKIP in a line. If this happens, just delete the row.
17 15
18 #You can change the .mp4 to something else if your video files are in a different format 16 print('''Commands:
19 #videoFilename = raw_input("Please Type in the video filename\n") 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''')
20 28
21 args = parser.parse_args()
22 cap = cv2.VideoCapture(args.videoFilename) 29 cap = cv2.VideoCapture(args.videoFilename)
23 i = args.videoFilename.rfind('.') 30 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum)
24 if i>0: 31 cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
25 outputFilename = args.videoFilename[:i]+'.csv' 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'
26 else: 40 else:
27 outputFilename = args.videoFilename+'.csv' 41 outputFilename = args.outputFilename
28 vehNumber = 0 42 vehNumber = 0
43 lineNum = -1
29 out = open(outputFilename, 'a') 44 out = open(outputFilename, 'a')
30 45
31 print ("Commands:\nu: New Vehicle crossing the first line\ni: Vehicle crossing subsequent lines\no: Press o when you make a mistake in input\nd: Skip 100 frames\ns: Skip 10 frames\nc: Go back 100 frames\nx: Go back 10 frames\nSpacebar: Go forward one frame\nl: Skip to frame number\nq: Quit and end program")
32 while(cap.isOpened()): 46 while(cap.isOpened()):
33 ret, frame = cap.read() 47 ret, frame = cap.read()
34 frame_no = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) 48 frameNum = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))
35 #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 49 cv2.putText(frame, str(frameNum), (1,20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0,0))
36 50 cv2.imshow('Video',frame)
37 cv2.imshow('frame',frame)
38 51
39 key= cv2.waitKey(0) 52 key= cv2.waitKey(0)
40 #Change the keys to record the vehicle in this section 53 #Change the keys to record the vehicle in this section
41 if key == ord('q'): 54 if key == ord('q'):
42 break 55 break
43 if key == ord('u'): 56 elif key == ord('u') or key == ord('i'):
44 out.write('\n'+str(vehNumber)+','+str(frame_no)+',') 57 if key == ord('u'):
45 vehNumber+=1 58 vehNumber += 1
46 print('New Vehicle') 59 lineNum = 0
47 line_number = 0 60 print('New Vehicle')
48 if key == ord('i'): 61 out.write('{},{}\n'.format(vehNumber,frameNum))
49 out.write(str(frame_no)+',') 62 if vehNumber >= 1 and key == ord('i'):
50 line_number = line_number+1 63 lineNum = lineNum+1
51 print('Line number '+str(line_number)) 64 print('Line number {}'.format(lineNum))
52 if key == ord('o'): 65 elif key == ord('o'):
53 out.write('SKIP') 66 out.write('{},SKIP\n'.format(vehNumber))
54 print('SKIPPED') 67 print('SKIPPED')
68 elif key == ord('p'):
69 print("New Pedestrian")
70 out.write('Pedestrian,{}\n'.format(frameNum))
55 #Change the number of frames skipped or the keys in this section 71 #Change the number of frames skipped or the keys in this section
56 elif key == ord('d'): 72 elif key == ord('d'):
57 cap.set(1,frame_no+100) 73 cap.set(1,frameNum+100)
58 elif key == ord('s'): 74 elif key == ord('s'):
59 cap.set(1,frame_no+10) 75 cap.set(1,frameNum+10)
60 elif key == ord('a'): 76 elif key == ord('a'):
61 cap.set(1,frame_no+1) 77 cap.set(1,frameNum+1)
62 elif key == ord('x'): 78 elif key == ord('x'):
63 cap.set(1,frame_no-10) 79 cap.set(1,frameNum-10)
64 elif key == ord('c'): 80 elif key == ord('c'):
65 cap.set(1,frame_no-100) 81 cap.set(1,frameNum-100)
66 elif key == ord('l'): 82 elif key == ord('l'):
67 frame_no = int(input("Please enter the frame number you would like to skip to\n")) 83 frameNum = int(raw_input("Please enter the frame number you would like to skip to\n"))
68 cap.set(1,frame_no-5) 84 cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,frameNum-5)
69 85
70 86
71 cap.release() 87 cap.release()
72 cv2.destroyAllWindows() 88 cv2.destroyAllWindows()
73 89