Mercurial > hg > nsaunier > traffic-intelligence
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 883:5852a3cdd455 | 884:ac4bcbcc9cda |
|---|---|
| 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.') | |
| 6 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) | |
| 7 parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int) | |
| 8 | |
| 9 #Place this program in the same folder as the video you want to analyze | |
| 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 | |
| 18 #You can change the .mp4 to something else if your video files are in a different format | |
| 19 #videoFilename = raw_input("Please Type in the video filename\n") | |
| 20 | |
| 21 args = parser.parse_args() | |
| 22 cap = cv2.VideoCapture(args.videoFilename) | |
| 23 i = args.videoFilename.rfind('.') | |
| 24 if i>0: | |
| 25 outputFilename = args.videoFilename[:i]+'.csv' | |
| 26 else: | |
| 27 outputFilename = args.videoFilename+'.csv' | |
| 28 vehNumber = 0 | |
| 29 out = open(outputFilename, 'a') | |
| 30 | |
| 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()): | |
| 33 ret, frame = cap.read() | |
| 34 frame_no = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) | |
| 35 #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| 36 | |
| 37 cv2.imshow('frame',frame) | |
| 38 | |
| 39 key= cv2.waitKey(0) | |
| 40 #Change the keys to record the vehicle in this section | |
| 41 if key == ord('q'): | |
| 42 break | |
| 43 if key == ord('u'): | |
| 44 out.write('\n'+str(vehNumber)+','+str(frame_no)+',') | |
| 45 vehNumber+=1 | |
| 46 print('New Vehicle') | |
| 47 line_number = 0 | |
| 48 if key == ord('i'): | |
| 49 out.write(str(frame_no)+',') | |
| 50 line_number = line_number+1 | |
| 51 print('Line number '+str(line_number)) | |
| 52 if key == ord('o'): | |
| 53 out.write('SKIP') | |
| 54 print('SKIPPED') | |
| 55 #Change the number of frames skipped or the keys in this section | |
| 56 elif key == ord('d'): | |
| 57 cap.set(1,frame_no+100) | |
| 58 elif key == ord('s'): | |
| 59 cap.set(1,frame_no+10) | |
| 60 elif key == ord('a'): | |
| 61 cap.set(1,frame_no+1) | |
| 62 elif key == ord('x'): | |
| 63 cap.set(1,frame_no-10) | |
| 64 elif key == ord('c'): | |
| 65 cap.set(1,frame_no-100) | |
| 66 elif key == ord('l'): | |
| 67 frame_no = int(input("Please enter the frame number you would like to skip to\n")) | |
| 68 cap.set(1,frame_no-5) | |
| 69 | |
| 70 | |
| 71 cap.release() | |
| 72 cv2.destroyAllWindows() | |
| 73 | |
| 74 #97a | |
| 75 #115s | |
| 76 #100d | |
| 77 #102f | |
| 78 #103g | |
| 79 #104h | |
| 80 #106j | |
| 81 #107k | |
| 82 #108l |
