Mercurial Hosting > traffic-intelligence
annotate scripts/extract-appearance-images.py @ 909:cd038493f8c6
finished image extraction script for HoG-SVM training
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Mon, 26 Jun 2017 17:45:32 -0400 |
parents | a57e6fbcd8e3 |
children | 3dd5acfa1899 |
rev | line source |
---|---|
900 | 1 #! /usr/bin/env python |
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 |
909
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
6 from matplotlib.pyplot import imshow, figure |
900 | 7 |
8 import cvutils, moving, ml, storage | |
9 | |
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) |
909
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
18 parser.add_argument('-o', dest = 'overlap', help = 'maximum intersection over union of the features nFramesStep apart to save image', type = float, default = 0.2) |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 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
|
23 |
901
753a081989e2
factorized some argument handling code
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
900
diff
changeset
|
24 args = parser.parse_args() |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
25 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
|
26 classifierParams = storage.ClassifierParameters(params.classifierFilename) |
900 | 27 |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
28 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
|
29 annotatedObjectNumbers = classificationAnnotations.index.tolist() |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
30 |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
31 # 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
|
32 if args.extractAllObjectImages: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
33 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
|
34 else: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
35 if len(annotatedObjectNumbers) > args.nObjects: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
36 classificationAnnotations = classificationAnnotations[:args.nObjects] |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
37 annotatedObjectNumbers = classificationAnnotations.index.tolist() |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
38 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', annotatedObjectNumbers, withFeatures = True) |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
39 for obj in objects: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
40 if obj.getNum() in annotatedObjectNumbers: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
41 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
|
42 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
|
43 |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
44 for userType in classificationAnnotations['road_user_type'].unique(): |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
45 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
|
46 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
|
47 |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
48 capture = cv2.VideoCapture(videoFilename) |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
49 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
|
50 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
|
51 |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
52 if undistort: # setup undistortion |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
53 [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
|
54 height, width = map1.shape |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
55 |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
56 if capture.isOpened(): |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
57 ret = True |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
58 frameNum = timeInterval.first |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
59 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
|
60 lastFrameNum = timeInterval.last |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
61 while ret and frameNum <= timeInterval.last: |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
62 ret, img = capture.read() |
909
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
63 distorted = True |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
64 if ret: |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
65 if frameNum%50 == 0: |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
66 print('frame number: {}'.format(frameNum)) |
904
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
67 for obj in objects: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
68 if obj.existsAtInstant(frameNum): |
906 | 69 if (10+frameNum-obj.getFirstInstant())%args.nFramesStep == 0: |
909
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
70 currentImageFeatures = set([f.num for f in obj.getFeatures() if f.existsAtInstant(frameNum)]) |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
71 if not hasattr(obj, 'lastImageFeatures') or len(currentImageFeatures.intersection(obj.lastImageFeatures))/len(currentImageFeatures.union(obj.lastImageFeatures)) < args.overlap: |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
72 obj.lastImageFeatures = currentImageFeatures |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
73 if undistort and distorted: # undistort only if necessary |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
74 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
75 distorted = False |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
76 croppedImg = cvutils.imageBox(img, obj, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels) |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
77 if croppedImg is not None: |
cd038493f8c6
finished image extraction script for HoG-SVM training
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
906
diff
changeset
|
78 cv2.imwrite(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
|
79 elif obj.getLastInstant() == frameNum: |
8f60ecfc2f06
work in progress, almost ready
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
902
diff
changeset
|
80 objects.remove(obj) |
902
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
81 frameNum += 1 |
c69a8defe5c3
changed workflow of classify objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
901
diff
changeset
|
82 |
900 | 83 # todo speed info: distributions AND min speed equiprobable |
84 |