annotate scripts/extract-appearance-images.py @ 906:a57e6fbcd8e3

minor
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 23 Jun 2017 00:03:17 -0400
parents 0e017178f7ab
children cd038493f8c6
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
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
3 import numpy as np, cv2
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
4 import argparse, os
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
5 from pandas import read_csv
905
0e017178f7ab correct bug in classify-objects script when correcting for distrotion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 904
diff changeset
6 from matplotlib.pyplot import imsave, imshow, figure
900
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)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
15 parser.add_argument('--delimiter', dest = 'classificationAnnotationFilenameDelimiter', help = 'delimiter for the fields in the correct classification file', default= ' ')
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
16 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
17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to use to extract patches from', type = int, default = None)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
18 parser.add_argument('--extract-all', dest = 'extractAllObjectImages', help = 'extracts the images for all objects, well classified or not (otherwise, extracts only for the misclassified)', action = 'store_true')
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
19 parser.add_argument('--prefix', dest = 'imagePrefix', help = 'image prefix', default = 'img')
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
20 parser.add_argument('--ouput', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', default = '.')
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
21 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
22
901
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
23
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
24 #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
25
753a081989e2 factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 900
diff changeset
26 args = parser.parse_args()
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
27 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
28 classifierParams = storage.ClassifierParameters(params.classifierFilename)
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
29
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
30 classificationAnnotations = read_csv(args.classificationAnnotationFilename, index_col=0, delimiter = args.classificationAnnotationFilenameDelimiter, names = ["object_num", "road_user_type"])
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
31 annotatedObjectNumbers = classificationAnnotations.index.tolist()
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
32
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
33 # objects has the objects for which we want to extract labeled images
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
34 if args.extractAllObjectImages:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
35 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True)
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
36 else:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
37 if len(annotatedObjectNumbers) > args.nObjects:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
38 classificationAnnotations = classificationAnnotations[:args.nObjects]
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
39 annotatedObjectNumbers = classificationAnnotations.index.tolist()
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
40 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', annotatedObjectNumbers, withFeatures = True)
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
41 for obj in objects:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
42 if obj.getNum() in annotatedObjectNumbers:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
43 obj.setUserType(classificationAnnotations.loc[obj.getNum(), 'road_user_type'])
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
44 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
45
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
46 for userType in classificationAnnotations['road_user_type'].unique():
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
47 if not os.path.exists(args.directoryName+os.sep+moving.userTypeNames[userType]):
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
48 os.mkdir(args.directoryName+os.sep+moving.userTypeNames[userType])
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
49
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
50 capture = cv2.VideoCapture(videoFilename)
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
51 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
52 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
53
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
54 if undistort: # setup undistortion
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
55 [map1, map2] = cvutils.computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
905
0e017178f7ab correct bug in classify-objects script when correcting for distrotion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 904
diff changeset
56 height, width = map1.shape
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
57
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
58 if capture.isOpened():
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
59 ret = True
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
60 frameNum = timeInterval.first
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
61 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
62 lastFrameNum = timeInterval.last
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
63 while ret and frameNum <= timeInterval.last:
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
64 ret, img = capture.read()
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
65 if ret:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
66 if frameNum%50 == 0:
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
67 print('frame number: {}'.format(frameNum))
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
68 if undistort: # undistort only if necessary
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
69 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
70 for obj in objects:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
71 if obj.existsAtInstant(frameNum):
906
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 905
diff changeset
72 if (10+frameNum-obj.getFirstInstant())%args.nFramesStep == 0:
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 905
diff changeset
73 # todo find next non zero image if none
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 905
diff changeset
74 # todo get several images if different features (measure of similarity)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
75 croppedImg = cvutils.imageBox(img, obj, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels)
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
76 if croppedImg is not None:
905
0e017178f7ab correct bug in classify-objects script when correcting for distrotion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 904
diff changeset
77 imsave(args.directoryName+os.sep+moving.userTypeNames[obj.getUserType()]+os.sep+args.imagePrefix+'-{}-{}.png'.format(obj.getNum(), frameNum), croppedImg)
904
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
78 elif obj.getLastInstant() == frameNum:
8f60ecfc2f06 work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 902
diff changeset
79 objects.remove(obj)
902
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
80 frameNum += 1
c69a8defe5c3 changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 901
diff changeset
81
900
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
82 # todo speed info: distributions AND min speed equiprobable
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff changeset
83