annotate scripts/manual_video_analysis.py @ 884:ac4bcbcc9cda

added manual data collection script, thanks Philip Morse!
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 17 Mar 2017 17:52:19 -0400
parents
children 7f61854fcc6d
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
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
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.')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7 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
8
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9 #Place this program in the same folder as the video you want to analyze
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 #Use this program in combination with a screen marker program (For example, Presentation Assistant) to draw multiple lines on the screen
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
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.
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
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.
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
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
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
14 #The output should give you a .csv file with the same name as your video file with columns in this format:
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
15 #vehicle number, frame number at first line, frame number at second line, frame number at third line, etc...
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
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.
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
17
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
18 #You can change the .mp4 to something else if your video files are in a different format
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
19 #videoFilename = raw_input("Please Type in the video filename\n")
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
20
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
21 args = parser.parse_args()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
22 cap = cv2.VideoCapture(args.videoFilename)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
23 i = args.videoFilename.rfind('.')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
24 if i>0:
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25 outputFilename = args.videoFilename[:i]+'.csv'
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
26 else:
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
27 outputFilename = args.videoFilename+'.csv'
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
28 vehNumber = 0
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29 out = open(outputFilename, 'a')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
30
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
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")
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
32 while(cap.isOpened()):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
33 ret, frame = cap.read()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
34 frame_no = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES))
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
35 #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
36
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
37 cv2.imshow('frame',frame)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
38
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
39 key= cv2.waitKey(0)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
40 #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
41 if key == ord('q'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
42 break
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
43 if key == ord('u'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
44 out.write('\n'+str(vehNumber)+','+str(frame_no)+',')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
45 vehNumber+=1
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
46 print('New Vehicle')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
47 line_number = 0
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
48 if key == ord('i'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
49 out.write(str(frame_no)+',')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
50 line_number = line_number+1
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
51 print('Line number '+str(line_number))
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52 if key == ord('o'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
53 out.write('SKIP')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
54 print('SKIPPED')
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
55 #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
56 elif key == ord('d'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
57 cap.set(1,frame_no+100)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
58 elif key == ord('s'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
59 cap.set(1,frame_no+10)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
60 elif key == ord('a'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
61 cap.set(1,frame_no+1)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
62 elif key == ord('x'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
63 cap.set(1,frame_no-10)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
64 elif key == ord('c'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
65 cap.set(1,frame_no-100)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
66 elif key == ord('l'):
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
67 frame_no = int(input("Please enter the frame number you would like to skip to\n"))
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
68 cap.set(1,frame_no-5)
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
69
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
70
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
71 cap.release()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
72 cv2.destroyAllWindows()
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
73
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
74 #97a
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
75 #115s
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
76 #100d
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
77 #102f
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
78 #103g
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
79 #104h
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
80 #106j
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
81 #107k
ac4bcbcc9cda added manual data collection script, thanks Philip Morse!
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
82 #108l