Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.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 | 30bd0f2223b7 |
| children | 21f10332c72b |
comparison
equal
deleted
inserted
replaced
| 801:c5f98916297e | 805:180b6b0231c0 |
|---|---|
| 382 dropTables(connection, ['objects', 'objects_features']) | 382 dropTables(connection, ['objects', 'objects_features']) |
| 383 elif dataType == 'interaction': | 383 elif dataType == 'interaction': |
| 384 dropTables(connection, ['interactions', 'indicators']) | 384 dropTables(connection, ['interactions', 'indicators']) |
| 385 elif dataType == 'bb': | 385 elif dataType == 'bb': |
| 386 dropTables(connection, ['bounding_boxes']) | 386 dropTables(connection, ['bounding_boxes']) |
| 387 elif dataType == 'pois': | |
| 388 dropTables(connection, ['gaussians2d']) | |
| 387 else: | 389 else: |
| 388 print('Unknown data type {} to delete from database'.format(dataType)) | 390 print('Unknown data type {} to delete from database'.format(dataType)) |
| 389 connection.close() | 391 connection.close() |
| 390 else: | 392 else: |
| 391 print('{} does not exist'.format(filename)) | 393 print('{} does not exist'.format(filename)) |
| 503 printDBError(error) | 505 printDBError(error) |
| 504 return boundingBoxes | 506 return boundingBoxes |
| 505 connection.close() | 507 connection.close() |
| 506 return boundingBoxes | 508 return boundingBoxes |
| 507 | 509 |
| 510 ######################### | |
| 511 # saving and loading for scene interpretation | |
| 512 ######################### | |
| 513 | |
| 514 def savePOIs(filename, gmm, gmmType, gmmId): | |
| 515 '''Saves a Gaussian mixture model (of class sklearn.mixture.GMM) | |
| 516 gmmType is a type of GMM, learnt either from beginnings or ends of trajectories''' | |
| 517 connection = sqlite3.connect(filename) | |
| 518 cursor = connection.cursor() | |
| 519 if gmmType not in ['beginning', 'end']: | |
| 520 print('Unknown POI type {}. Exiting'.format(gmmType)) | |
| 521 import sys | |
| 522 sys.exit() | |
| 523 try: | |
| 524 cursor.execute('CREATE TABLE IF NOT EXISTS gaussians2d (id INTEGER, type VARCHAR, x_center REAL, y_center REAL, covar00 REAL, covar01 REAL, covar10 REAL, covar11 REAL, covariance_type VARCHAR, weight, mixture_id INTEGER, PRIMARY KEY(id, mixture_id))') | |
| 525 for i in xrange(gmm.n_components): | |
| 526 cursor.execute('INSERT INTO gaussians2d VALUES({}, \'{}\', {}, {}, {}, {}, {}, {}, \'{}\', {}, {})'.format(i, gmmType, gmm.means_[i][0], gmm.means_[i][1], gmm.covars_[i][0,0], gmm.covars_[i][0,1], gmm.covars_[i][1,0], gmm.covars_[i][1,1], gmm.covariance_type, gmm.weights_[i], gmmId)) | |
| 527 connection.commit() | |
| 528 except sqlite3.OperationalError as error: | |
| 529 printDBError(error) | |
| 530 connection.close() | |
| 531 | |
| 532 def loadPOIs(filename): | |
| 533 'Loads all 2D Gaussians in the database' | |
| 534 from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields | |
| 535 connection = sqlite3.connect(filename) | |
| 536 cursor = connection.cursor() | |
| 537 pois = [] | |
| 538 try: | |
| 539 cursor.execute('SELECT * from gaussians2d') | |
| 540 gmmId = None | |
| 541 gmm = [] | |
| 542 for row in cursor: | |
| 543 if gmmId is None or row[10] != gmmId: | |
| 544 if len(gmm) > 0: | |
| 545 tmp = mixture.GMM(len(gmm), covarianceType) | |
| 546 tmp.means_ = array([gaussian['mean'] for gaussian in gmm]) | |
| 547 tmp.covars_ = array([gaussian['covar'] for gaussian in gmm]) | |
| 548 tmp.weights_ = array([gaussian['weight'] for gaussian in gmm]) | |
| 549 tmp.gmmTypes = [gaussian['type'] for gaussian in gmm] | |
| 550 pois.append(tmp) | |
| 551 gaussian = {'type': row[1], | |
| 552 'mean': row[2:4], | |
| 553 'covar': array(row[4:8]).reshape(2,2), | |
| 554 'weight': row[9]} | |
| 555 gmm = [gaussian] | |
| 556 covarianceType = row[8] | |
| 557 gmmId = row[10] | |
| 558 else: | |
| 559 gmm.append({'type': row[1], | |
| 560 'mean': row[2:4], | |
| 561 'covar': array(row[4:8]).reshape(2,2), | |
| 562 'weight': row[9]}) | |
| 563 if len(gmm) > 0: | |
| 564 tmp = mixture.GMM(len(gmm), covarianceType) | |
| 565 tmp.means_ = array([gaussian['mean'] for gaussian in gmm]) | |
| 566 tmp.covars_ = array([gaussian['covar'] for gaussian in gmm]) | |
| 567 tmp.weights_ = array([gaussian['weight'] for gaussian in gmm]) | |
| 568 tmp.gmmTypes = [gaussian['type'] for gaussian in gmm] | |
| 569 pois.append(tmp) | |
| 570 except sqlite3.OperationalError as error: | |
| 571 printDBError(error) | |
| 572 connection.close() | |
| 573 return pois | |
| 574 | |
| 508 ######################### | 575 ######################### |
| 509 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) | 576 # saving and loading for scene interpretation (Mohamed Gomaa Mohamed's PhD) |
| 510 ######################### | 577 ######################### |
| 511 | 578 |
| 512 def writePrototypesToSqlite(prototypes,nMatching, outputFilename): | 579 def writePrototypesToSqlite(prototypes,nMatching, outputFilename): |
