diff 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
line wrap: on
line diff
--- a/scripts/learn-poi.py	Tue May 31 17:07:23 2016 -0400
+++ b/scripts/learn-poi.py	Thu Jun 09 15:36:21 2016 -0400
@@ -11,10 +11,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')
 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object')
-parser.add_argument('-n', dest = 'nClusters', help = 'number of point clusters', required = True, type = int)
+parser.add_argument('-norigins', dest = 'nOriginClusters', help = 'number of clusters for trajectory origins', required = True, type = int)
+parser.add_argument('-ndestinations', dest = 'nDestinationClusters', help = 'number of clusters for trajectory destinations (=norigins if not provided)', type = int)
 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full")
 parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image')
-parser.add_argument('-u', dest = 'pixelsPerUnit', help = 'number pixels per unit of distance', type = float, default = 1.)
+parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.)
 
 args = parser.parse_args()
 
@@ -29,25 +30,37 @@
 beginnings = np.array(beginnings)
 ends = np.array(ends)
 
-gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType)
-beginningModel=gmm.fit(beginnings)
-gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType)
-endModel=gmm.fit(ends)
+nDestinationClusters = args.nDestinationClusters
+if args.nDestinationClusters is None:
+    nDestinationClusters = args.nOriginClusters
 
-fig = plt.figure()
-if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
-    img = plt.imread(args.worldImageFilename)
-    plt.imshow(img)
-ml.plotGMMClusters(beginningModel, beginnings, fig, nPixelsPerUnit = args.pixelsPerUnit)
-plt.axis('equal')
-plt.title('Origins')
-print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters))))
-
-fig = plt.figure()
-if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
-    img = plt.imread(args.worldImageFilename)
-    plt.imshow(img)
-ml.plotGMMClusters(endModel, ends, fig, nPixelsPerUnit = args.pixelsPerUnit)
-plt.axis('equal')
-plt.title('Destinations')
-print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))
+gmmId=0
+for nClusters, points, gmmType in zip([args.nOriginClusters, nDestinationClusters],
+                                   [beginnings, ends],
+                                   ['beginning', 'end']):
+    # estimation
+    gmm = mixture.GMM(n_components=nClusters, covariance_type = args.covarianceType)
+    model=gmm.fit(beginnings)
+    if not model.converged_:
+        print('Warning: model for '+gmmType+' points did not converge')
+    # plot
+    fig = plt.figure()
+    if args.worldImageFilename is not None and args.unitsPerPixel is not None:
+        img = plt.imread(args.worldImageFilename)
+        plt.imshow(img)
+    labels = ml.plotGMMClusters(model, points, fig, nUnitsPerPixel = args.unitsPerPixel)
+    plt.axis('image')
+    plt.title(gmmType)
+    print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(model.n_components))))
+    # save
+    storage.savePOIs(args.databaseFilename, model, gmmType, gmmId)
+    gmmId += 1
+                     
+# fig = plt.figure()
+# if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
+#     img = plt.imread(args.worldImageFilename)
+#     plt.imshow(img)
+# ml.plotGMMClusters(, , fig, nPixelsPerUnit = args.pixelsPerUnit)
+# plt.axis('equal')
+# plt.title()
+# print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))