Mercurial Hosting > traffic-intelligence
changeset 848:0cb69238e6f5
corrected load/save prototypes
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Thu, 21 Jul 2016 17:51:11 -0400 |
parents | 36c5bee9a887 |
children | a414a7d58483 |
files | python/storage.py scripts/learn-motion-patterns.py |
diffstat | 2 files changed, 13 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/python/storage.py Wed Jul 20 17:52:40 2016 -0400 +++ b/python/storage.py Thu Jul 21 17:51:11 2016 -0400 @@ -364,19 +364,23 @@ printDBError(error) connection.close() -def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None): +def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None, dbFilenames = None): '''save the prototype indices nMatchings, if not None, is a dictionnary between indices and number of matches''' connection = sqlite3.connect(filename) cursor = connection.cursor() try: - cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER)') + cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER, dbfilename VARCHAR, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER, PRIMARY KEY (id, dbfilename))') for i in prototypeIndices: if nMatchings is not None: n = nMatchings[i] else: n = 'NULL' - cursor.execute('INSERT INTO prototypes (id, trajectory_type, nMatchings) VALUES ({},\"{}\",{})'.format(i, trajectoryType, n)) + if dbFilenames is not None: + dbfn = dbFilenames[i] + else: + dbfn = filename + cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nMatchings) VALUES ({},\"{}\",\"{}\",{})'.format(i, dbfn, trajectoryType, n)) except sqlite3.OperationalError as error: printDBError(error) connection.commit() @@ -387,21 +391,23 @@ connection = sqlite3.connect(filename) cursor = connection.cursor() prototypeIndices = [] + dbFilenames = {} trajectoryTypes = [] nMatchings = {} try: cursor.execute('SELECT * FROM prototypes') for row in cursor: prototypeIndices.append(row[0]) - trajectoryTypes.append(row[1]) + dbFilenames[row[0]] = row[1] + trajectoryTypes.append(row[2]) if row[2] is not None: - nMatchings[row[0]] = row[2] + nMatchings[row[0]] = row[3] except sqlite3.OperationalError as error: printDBError(error) connection.close() if len(set(trajectoryTypes)) > 1: print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes))) - return prototypeIndices, trajectoryTypes[0], nMatchings + return prototypeIndices, dbFilenames, trajectoryTypes[0], nMatchings def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): '''Loads bounding box moving object from an SQLite
--- a/scripts/learn-motion-patterns.py Wed Jul 20 17:52:40 2016 -0400 +++ b/scripts/learn-motion-patterns.py Thu Jul 21 17:51:11 2016 -0400 @@ -53,7 +53,7 @@ clusterSizes = ml.computeClusterSizes(labels, prototypeIndices, -1) print(clusterSizes) -storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], args.trajectoryType, {objects[i].getNum():clusterSizes[i] for i in prototypeIndices}) +storage.savePrototypesToSqlite(args.databaseFilename, [objects[i].getNum() for i in prototypeIndices], args.trajectoryType, {objects[i].getNum():clusterSizes[i] for i in prototypeIndices}) # if saving filenames, add for example {objects[i].getNum():objects[i].dbFilename for i in prototypeIndices} if args.saveSimilarities: np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f')