Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-poi.py @ 795:a34ec862371f
merged with dev branch
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 09 May 2016 15:33:11 -0400 |
| parents | 0a428b449b80 |
| children | 180b6b0231c0 |
comparison
equal
deleted
inserted
replaced
| 758:0a05883216cf | 795:a34ec862371f |
|---|---|
| 1 #! /usr/bin/env python | |
| 2 | |
| 3 import argparse | |
| 4 | |
| 5 import numpy as np | |
| 6 from sklearn import mixture | |
| 7 import matplotlib.pyplot as plt | |
| 8 | |
| 9 import storage, ml | |
| 10 | |
| 11 parser = argparse.ArgumentParser(description='The program learns and displays Gaussians fit to beginnings and ends of object trajectories (based on Mohamed Gomaa Mohamed 2015 PhD). TODO: save the data') | |
| 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) | |
| 13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object') | |
| 14 parser.add_argument('-n', dest = 'nClusters', help = 'number of point clusters', required = True, type = int) | |
| 15 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full") | |
| 16 parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image') | |
| 17 parser.add_argument('-u', dest = 'pixelsPerUnit', help = 'number pixels per unit of distance', type = float, default = 1.) | |
| 18 | |
| 19 args = parser.parse_args() | |
| 20 | |
| 21 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType) | |
| 22 | |
| 23 beginnings = [] | |
| 24 ends = [] | |
| 25 for o in objects: | |
| 26 beginnings.append(o.getPositionAt(0).aslist()) | |
| 27 ends.append(o.getPositionAt(int(o.length())-1).aslist()) | |
| 28 | |
| 29 beginnings = np.array(beginnings) | |
| 30 ends = np.array(ends) | |
| 31 | |
| 32 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) | |
| 33 beginningModel=gmm.fit(beginnings) | |
| 34 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) | |
| 35 endModel=gmm.fit(ends) | |
| 36 | |
| 37 fig = plt.figure() | |
| 38 if args.worldImageFilename is not None and args.pixelsPerUnit is not None: | |
| 39 img = plt.imread(args.worldImageFilename) | |
| 40 plt.imshow(img) | |
| 41 ml.plotGMMClusters(beginningModel, beginnings, fig, nPixelsPerUnit = args.pixelsPerUnit) | |
| 42 plt.axis('equal') | |
| 43 plt.title('Origins') | |
| 44 print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters)))) | |
| 45 | |
| 46 fig = plt.figure() | |
| 47 if args.worldImageFilename is not None and args.pixelsPerUnit is not None: | |
| 48 img = plt.imread(args.worldImageFilename) | |
| 49 plt.imshow(img) | |
| 50 ml.plotGMMClusters(endModel, ends, fig, nPixelsPerUnit = args.pixelsPerUnit) | |
| 51 plt.axis('equal') | |
| 52 plt.title('Destinations') | |
| 53 print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters)))) |
