Mercurial Hosting > traffic-intelligence
diff scripts/extract-appearance-images.py @ 904:8f60ecfc2f06
work in progress, almost ready
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 22 Jun 2017 18:08:46 -0400 |
parents | c69a8defe5c3 |
children | 0e017178f7ab |
line wrap: on
line diff
--- a/scripts/extract-appearance-images.py Thu Jun 22 17:02:03 2017 -0400 +++ b/scripts/extract-appearance-images.py Thu Jun 22 18:08:46 2017 -0400 @@ -1,9 +1,9 @@ #! /usr/bin/env python -import numpy as np -import argparse -from cv2 import SVM_RBF, SVM_C_SVC -#from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE # row_sample for layout in cv2.ml.SVM_load +import numpy as np, cv2 +import argparse, os +from pandas import read_csv +from matplotlib.pyplot import imsave import cvutils, moving, ml, storage @@ -12,8 +12,12 @@ parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') parser.add_argument('--gt', dest = 'classificationAnnotationFilename', help = 'name of the file containing the correct classes (user types)', required = True) +parser.add_argument('--delimiter', dest = 'classificationAnnotationFilenameDelimiter', help = 'delimiter for the fields in the correct classification file', default= ' ') parser.add_argument('-s', dest = 'nFramesStep', help = 'number of frames between each saved patch', default = 50, type = int) parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to use to extract patches from', type = int, default = None) +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') +parser.add_argument('--prefix', dest = 'imagePrefix', help = 'image prefix', default = 'img') +parser.add_argument('--ouput', dest = 'directoryName', help = 'parent directory name for the directories containing the samples for the different road users', default = '.') 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') @@ -23,34 +27,55 @@ params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) classifierParams = storage.ClassifierParameters(params.classifierFilename) -objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True) +classificationAnnotations = read_csv(args.classificationAnnotationFilename, index_col=0, delimiter = args.classificationAnnotationFilenameDelimiter, names = ["object_num", "road_user_type"]) +annotatedObjectNumbers = classificationAnnotations.index.tolist() + +# objects has the objects for which we want to extract labeled images +if args.extractAllObjectImages: + objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True) +else: + if len(annotatedObjectNumbers) > args.nObjects: + classificationAnnotations = classificationAnnotations[:args.nObjects] + annotatedObjectNumbers = classificationAnnotations.index.tolist() + objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', annotatedObjectNumbers, withFeatures = True) +for obj in objects: + if obj.getNum() in annotatedObjectNumbers: + obj.setUserType(classificationAnnotations.loc[obj.getNum(), 'road_user_type']) timeInterval = moving.TimeInterval.unionIntervals([obj.getTimeInterval() for obj in objects]) +for userType in classificationAnnotations['road_user_type'].unique(): + if not os.path.exists(args.directoryName+os.sep+moving.userTypeNames[userType]): + os.mkdir(args.directoryName+os.sep+moving.userTypeNames[userType]) + capture = cv2.VideoCapture(videoFilename) width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) if undistort: # setup undistortion [map1, map2] = cvutils.computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) + +print(timeInterval) if capture.isOpened(): ret = True frameNum = timeInterval.first capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) lastFrameNum = timeInterval.last - while ret and frameNum <= lastFrameNum: + while ret and frameNum <= timeInterval.last: ret, img = capture.read() if ret: if frameNum%50 == 0: print('frame number: {}'.format(frameNum)) - if undistort: + if undistort: # undistort only if necessary img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) - - + for obj in objects: + if obj.existsAtInstant(frameNum): + if (obj.getFirstInstant()-frameNum)%args.nFramesStep == 0: # todo find next non zero image if none + croppedImg = cvutils.imageBox(img, obj, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels) + if croppedImg is not None: + imsave(args.directoryName+os.sep+moving.userTypeNames[obj.getUserType()]+args.imagePrefix+'-{}-{}.png'.format(obj.getNum(), frameNum), croppedImg) + elif obj.getLastInstant() == frameNum: + objects.remove(obj) frameNum += 1 - - # todo speed info: distributions AND min speed equiprobable -# provide csv delimiter for the classification file as arg -