Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-poi.py @ 805:180b6b0231c0
added saving/loading points of interests
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 09 Jun 2016 15:36:21 -0400 |
| parents | 0a428b449b80 |
| children | 181bcb6dad3a |
comparison
equal
deleted
inserted
replaced
| 801:c5f98916297e | 805:180b6b0231c0 |
|---|---|
| 9 import storage, ml | 9 import storage, ml |
| 10 | 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') | 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) | 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') | 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) | 14 parser.add_argument('-norigins', dest = 'nOriginClusters', help = 'number of clusters for trajectory origins', required = True, type = int) |
| 15 parser.add_argument('-ndestinations', dest = 'nDestinationClusters', help = 'number of clusters for trajectory destinations (=norigins if not provided)', type = int) | |
| 15 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full") | 16 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('-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 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.) |
| 18 | 19 |
| 19 args = parser.parse_args() | 20 args = parser.parse_args() |
| 20 | 21 |
| 21 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType) | 22 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType) |
| 22 | 23 |
| 27 ends.append(o.getPositionAt(int(o.length())-1).aslist()) | 28 ends.append(o.getPositionAt(int(o.length())-1).aslist()) |
| 28 | 29 |
| 29 beginnings = np.array(beginnings) | 30 beginnings = np.array(beginnings) |
| 30 ends = np.array(ends) | 31 ends = np.array(ends) |
| 31 | 32 |
| 32 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) | 33 nDestinationClusters = args.nDestinationClusters |
| 33 beginningModel=gmm.fit(beginnings) | 34 if args.nDestinationClusters is None: |
| 34 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType) | 35 nDestinationClusters = args.nOriginClusters |
| 35 endModel=gmm.fit(ends) | |
| 36 | 36 |
| 37 fig = plt.figure() | 37 gmmId=0 |
| 38 if args.worldImageFilename is not None and args.pixelsPerUnit is not None: | 38 for nClusters, points, gmmType in zip([args.nOriginClusters, nDestinationClusters], |
| 39 img = plt.imread(args.worldImageFilename) | 39 [beginnings, ends], |
| 40 plt.imshow(img) | 40 ['beginning', 'end']): |
| 41 ml.plotGMMClusters(beginningModel, beginnings, fig, nPixelsPerUnit = args.pixelsPerUnit) | 41 # estimation |
| 42 plt.axis('equal') | 42 gmm = mixture.GMM(n_components=nClusters, covariance_type = args.covarianceType) |
| 43 plt.title('Origins') | 43 model=gmm.fit(beginnings) |
| 44 print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters)))) | 44 if not model.converged_: |
| 45 | 45 print('Warning: model for '+gmmType+' points did not converge') |
| 46 fig = plt.figure() | 46 # plot |
| 47 if args.worldImageFilename is not None and args.pixelsPerUnit is not None: | 47 fig = plt.figure() |
| 48 img = plt.imread(args.worldImageFilename) | 48 if args.worldImageFilename is not None and args.unitsPerPixel is not None: |
| 49 plt.imshow(img) | 49 img = plt.imread(args.worldImageFilename) |
| 50 ml.plotGMMClusters(endModel, ends, fig, nPixelsPerUnit = args.pixelsPerUnit) | 50 plt.imshow(img) |
| 51 plt.axis('equal') | 51 labels = ml.plotGMMClusters(model, points, fig, nUnitsPerPixel = args.unitsPerPixel) |
| 52 plt.title('Destinations') | 52 plt.axis('image') |
| 53 print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters)))) | 53 plt.title(gmmType) |
| 54 print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(model.n_components)))) | |
| 55 # save | |
| 56 storage.savePOIs(args.databaseFilename, model, gmmType, gmmId) | |
| 57 gmmId += 1 | |
| 58 | |
| 59 # fig = plt.figure() | |
| 60 # if args.worldImageFilename is not None and args.pixelsPerUnit is not None: | |
| 61 # img = plt.imread(args.worldImageFilename) | |
| 62 # plt.imshow(img) | |
| 63 # ml.plotGMMClusters(, , fig, nPixelsPerUnit = args.pixelsPerUnit) | |
| 64 # plt.axis('equal') | |
| 65 # plt.title() | |
| 66 # print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters)))) |
