Mercurial Hosting > traffic-intelligence
changeset 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 | d2eb8c93f7de |
files | scripts/manual_video_analysis.py |
diffstat | 1 files changed, 56 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/manual_video_analysis.py Fri Mar 17 17:52:19 2017 -0400 +++ b/scripts/manual_video_analysis.py Tue Mar 21 17:48:14 2017 -0400 @@ -2,70 +2,86 @@ import sys, argparse, cv2 -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.') +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.''', + epilog = '''The output should give you a .csv file with the same name as your video file with columns in this format: +vehicle number, frame number +You can easily spot mistakes in the csv file for a line with number, SKIP. If this happens, just delete the previous vehicle observation.''', + formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file', required = True) +parser.add_argument('-o', dest = 'outputFilename', help = 'name of the output file (csv file)') parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', default = 0, type = int) -#Place this program in the same folder as the video you want to analyze -#Use this program in combination with a screen marker program (For example, Presentation Assistant) to draw multiple lines on the screen -#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. -#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. -#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 -#The output should give you a .csv file with the same name as your video file with columns in this format: -#vehicle number, frame number at first line, frame number at second line, frame number at third line, etc... -#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. - -#You can change the .mp4 to something else if your video files are in a different format -#videoFilename = raw_input("Please Type in the video filename\n") - args = parser.parse_args() + +print('''Commands: +u: New vehicle crossing the first line +i: Vehicle crossing subsequent lines +o: Press o when you make a mistake in input +p: Press p for a new pedestrian event (eg crossing) +d: Skip 100 frames +s: Skip 10 frames +c: Go back 100 frames +x: Go back 10 frames +Spacebar: Go forward one frame +l: Skip to frame number +q: Quit and end program''') + cap = cv2.VideoCapture(args.videoFilename) -i = args.videoFilename.rfind('.') -if i>0: - outputFilename = args.videoFilename[:i]+'.csv' +cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum) +cv2.namedWindow('Video', cv2.WINDOW_NORMAL) + +# output filename +if args.outputFilename is None: + i = args.videoFilename.rfind('.') + if i>0: + outputFilename = args.videoFilename[:i]+'.csv' + else: + outputFilename = args.videoFilename+'.csv' else: - outputFilename = args.videoFilename+'.csv' + outputFilename = args.outputFilename vehNumber = 0 +lineNum = -1 out = open(outputFilename, 'a') -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") while(cap.isOpened()): ret, frame = cap.read() - frame_no = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) - #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) - - cv2.imshow('frame',frame) + frameNum = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) + cv2.putText(frame, str(frameNum), (1,20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0,0)) + cv2.imshow('Video',frame) key= cv2.waitKey(0) #Change the keys to record the vehicle in this section if key == ord('q'): break - if key == ord('u'): - out.write('\n'+str(vehNumber)+','+str(frame_no)+',') - vehNumber+=1 - print('New Vehicle') - line_number = 0 - if key == ord('i'): - out.write(str(frame_no)+',') - line_number = line_number+1 - print('Line number '+str(line_number)) - if key == ord('o'): - out.write('SKIP') + elif key == ord('u') or key == ord('i'): + if key == ord('u'): + vehNumber += 1 + lineNum = 0 + print('New Vehicle') + out.write('{},{}\n'.format(vehNumber,frameNum)) + if vehNumber >= 1 and key == ord('i'): + lineNum = lineNum+1 + print('Line number {}'.format(lineNum)) + elif key == ord('o'): + out.write('{},SKIP\n'.format(vehNumber)) print('SKIPPED') + elif key == ord('p'): + print("New Pedestrian") + out.write('Pedestrian,{}\n'.format(frameNum)) #Change the number of frames skipped or the keys in this section elif key == ord('d'): - cap.set(1,frame_no+100) + cap.set(1,frameNum+100) elif key == ord('s'): - cap.set(1,frame_no+10) + cap.set(1,frameNum+10) elif key == ord('a'): - cap.set(1,frame_no+1) + cap.set(1,frameNum+1) elif key == ord('x'): - cap.set(1,frame_no-10) + cap.set(1,frameNum-10) elif key == ord('c'): - cap.set(1,frame_no-100) + cap.set(1,frameNum-100) elif key == ord('l'): - frame_no = int(input("Please enter the frame number you would like to skip to\n")) - cap.set(1,frame_no-5) + frameNum = int(raw_input("Please enter the frame number you would like to skip to\n")) + cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,frameNum-5) cap.release()