Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/classify-objects.py @ 911:3dd5acfa1899
corrected potential issues with videos where one cannot reach a give frame from its number
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 28 Jun 2017 16:46:45 -0400 |
| parents | 0e017178f7ab |
| children | fd057a6b04db |
comparison
equal
deleted
inserted
replaced
| 910:b58a1061a717 | 911:3dd5acfa1899 |
|---|---|
| 13 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') | 13 parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') |
| 14 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) | 14 parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) |
| 15 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') | 15 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') |
| 16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') | 16 parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') |
| 17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) | 17 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) |
| 18 parser.add_argument('--start-frame0', dest = 'startFrame0', help = 'starts with first frame for videos with index problem where frames cannot be reached', action = 'store_true') | |
| 18 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') | 19 parser.add_argument('--plot-speed-distributions', dest = 'plotSpeedDistribution', help = 'simply plots the distributions used for each user type', action = 'store_true') |
| 19 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display (km/h)', type = float, default = 50.) | 20 parser.add_argument('--max-speed-distribution-plot', dest = 'maxSpeedDistributionPlot', help = 'if plotting the user distributions, the maximum speed to display (km/h)', type = float, default = 50.) |
| 20 | 21 |
| 21 args = parser.parse_args() | 22 args = parser.parse_args() |
| 22 params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) | 23 params, videoFilename, databaseFilename, invHomography, intrinsicCameraMatrix, distortionCoefficients, undistortedImageMultiplication, undistort, firstFrameNum = storage.processVideoArguments(args) |
| 64 plt.show() | 65 plt.show() |
| 65 sys.exit() | 66 sys.exit() |
| 66 | 67 |
| 67 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True) | 68 objects = storage.loadTrajectoriesFromSqlite(databaseFilename, 'object', args.nObjects, withFeatures = True) |
| 68 timeInterval = moving.TimeInterval.unionIntervals([obj.getTimeInterval() for obj in objects]) | 69 timeInterval = moving.TimeInterval.unionIntervals([obj.getTimeInterval() for obj in objects]) |
| 70 if args.startFrame0: | |
| 71 timeInterval.first = 0 | |
| 69 | 72 |
| 70 capture = cv2.VideoCapture(videoFilename) | 73 capture = cv2.VideoCapture(videoFilename) |
| 71 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) | 74 width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) |
| 72 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | 75 height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) |
| 73 | 76 |
| 78 pastObjects = [] | 81 pastObjects = [] |
| 79 currentObjects = [] | 82 currentObjects = [] |
| 80 if capture.isOpened(): | 83 if capture.isOpened(): |
| 81 ret = True | 84 ret = True |
| 82 frameNum = timeInterval.first | 85 frameNum = timeInterval.first |
| 83 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | 86 if not args.startFrame0: |
| 87 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) | |
| 84 lastFrameNum = timeInterval.last | 88 lastFrameNum = timeInterval.last |
| 85 | 89 |
| 86 while ret and frameNum <= lastFrameNum: | 90 while ret and frameNum <= lastFrameNum: |
| 87 ret, img = capture.read() | 91 ret, img = capture.read() |
| 88 if ret: | 92 if ret: |
| 89 if frameNum%50 == 0: | 93 if frameNum%50 == 0: |
| 90 print('frame number: {}'.format(frameNum)) | 94 print('frame number: {}'.format(frameNum)) |
| 91 if undistort: | 95 if undistort: |
| 92 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) | 96 img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) |
| 93 for obj in objects: | 97 for obj in objects: |
| 94 if obj.getFirstInstant() == frameNum: | 98 if obj.getFirstInstant() >= frameNum: # if images are skipped |
| 95 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds) | 99 obj.initClassifyUserTypeHoGSVM(speedAggregationFunc, pedBikeCarSVM, bikeCarSVM, classifierParams.maxPedestrianSpeed, classifierParams.maxCyclistSpeed, classifierParams.nFramesIgnoreAtEnds) |
| 96 currentObjects.append(obj) | 100 currentObjects.append(obj) |
| 97 objects.remove(obj) | 101 objects.remove(obj) |
| 98 | 102 |
| 99 for obj in currentObjects: | 103 for obj in currentObjects: |
| 100 if obj.getLastInstant() == frameNum: | 104 if obj.getLastInstant() <= frameNum: # if images are skipped |
| 101 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown) | 105 obj.classifyUserTypeHoGSVM(minSpeedEquiprobable = classifierParams.minSpeedEquiprobable, speedProbabilities = speedProbabilities, maxPercentUnknown = classifierParams.maxPercentUnknown) |
| 102 pastObjects.append(obj) | 106 pastObjects.append(obj) |
| 103 currentObjects.remove(obj) | 107 currentObjects.remove(obj) |
| 104 else: | 108 else: |
| 105 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock, classifierParams.hogBlockNorm) | 109 obj.classifyUserTypeHoGSVMAtInstant(img, frameNum, invHomography, width, height, classifierParams.percentIncreaseCrop, classifierParams.percentIncreaseCrop, classifierParams.minNPixels, classifierParams.hogRescaleSize, classifierParams.hogNOrientations, classifierParams.hogNPixelsPerCell, classifierParams.hogNCellsPerBlock, classifierParams.hogBlockNorm) |
