annotate scripts/extract-appearance-images.py @ 902:c69a8defe5c3

changed workflow of classify objects
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 22 Jun 2017 16:57:34 -0400
parents 753a081989e2
children 8f60ecfc2f06
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
1 #! /usr/bin/env python
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
2
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
3 import numpy as np
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
4 import argparse
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
5 from cv2 import SVM_RBF, SVM_C_SVC
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
6 #from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE # row_sample for layout in cv2.ml.SVM_load
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
7
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
8 import cvutils, moving, ml, storage
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
9
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
10 parser = argparse.ArgumentParser(description='The program extracts labeled image patches to train the HoG-SVM classifier, and optionnally speed information')
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
11 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True)
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
13 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
14 parser.add_argument('--gt', dest = 'classificationAnnotationFilename', help = 'name of the file containing the correct classes (user types)', required = True)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
15 parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each saved patch', default = 50, type = int)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
16 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to use to extract patches from', type = int, default = None)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
17 parser.add_argument('--compute-speed-distributions', dest = 'computeSpeedDistribution', help = 'computes the distribution of the road users of each type and fits parameters to each', action = 'store_true')
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
18
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
19
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
20 #parser.add_argument('-d', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', required = True)
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
21
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
22 args = parser.parse_args()
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
23 params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args)
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
24 classifierParams = storage.ClassifierParameters(params.classifierFilename)
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
25
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
26 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
27 timeInterval = moving.TimeInterval.unionIntervals([obj.getTimeInterval() for obj in objects])
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
28
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
29 capture = cv2.VideoCapture(videoFilename)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
30 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH))
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
31 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
32
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
33 if undistort: # setup undistortion
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
34 [map1, map2] = cvutils.computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
35 if capture.isOpened():
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
36 ret = True
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
37 frameNum = timeInterval.first
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
38 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
39 lastFrameNum = timeInterval.last
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
40 while ret and frameNum <= lastFrameNum:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
41 ret, img = capture.read()
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
42 if ret:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
43 if frameNum%50 == 0:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
44 print('frame number: {}'.format(frameNum))
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
45 if undistort:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
46 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
47
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
48
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
49 frameNum += 1
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
50
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
51
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
52
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
53 # todo speed info: distributions AND min speed equiprobable
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
54
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
55 # provide csv delimiter for the classification file as arg
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
56