Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 848:0cb69238e6f5
corrected load/save prototypes
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Thu, 21 Jul 2016 17:51:11 -0400 |
| parents | 5a68779d7777 |
| children | a414a7d58483 |
comparison
equal
deleted
inserted
replaced
| 847:36c5bee9a887 | 848:0cb69238e6f5 |
|---|---|
| 362 connection.commit() | 362 connection.commit() |
| 363 except sqlite3.OperationalError as error: | 363 except sqlite3.OperationalError as error: |
| 364 printDBError(error) | 364 printDBError(error) |
| 365 connection.close() | 365 connection.close() |
| 366 | 366 |
| 367 def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None): | 367 def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None, dbFilenames = None): |
| 368 '''save the prototype indices | 368 '''save the prototype indices |
| 369 nMatchings, if not None, is a dictionnary between indices and number of matches''' | 369 nMatchings, if not None, is a dictionnary between indices and number of matches''' |
| 370 connection = sqlite3.connect(filename) | 370 connection = sqlite3.connect(filename) |
| 371 cursor = connection.cursor() | 371 cursor = connection.cursor() |
| 372 try: | 372 try: |
| 373 cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER)') | 373 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))') |
| 374 for i in prototypeIndices: | 374 for i in prototypeIndices: |
| 375 if nMatchings is not None: | 375 if nMatchings is not None: |
| 376 n = nMatchings[i] | 376 n = nMatchings[i] |
| 377 else: | 377 else: |
| 378 n = 'NULL' | 378 n = 'NULL' |
| 379 cursor.execute('INSERT INTO prototypes (id, trajectory_type, nMatchings) VALUES ({},\"{}\",{})'.format(i, trajectoryType, n)) | 379 if dbFilenames is not None: |
| 380 dbfn = dbFilenames[i] | |
| 381 else: | |
| 382 dbfn = filename | |
| 383 cursor.execute('INSERT INTO prototypes (id, dbfilename, trajectory_type, nMatchings) VALUES ({},\"{}\",\"{}\",{})'.format(i, dbfn, trajectoryType, n)) | |
| 380 except sqlite3.OperationalError as error: | 384 except sqlite3.OperationalError as error: |
| 381 printDBError(error) | 385 printDBError(error) |
| 382 connection.commit() | 386 connection.commit() |
| 383 connection.close() | 387 connection.close() |
| 384 | 388 |
| 385 def loadPrototypesFromSqlite(filename): | 389 def loadPrototypesFromSqlite(filename): |
| 386 'Loads prototype ids and matchings (if stored)' | 390 'Loads prototype ids and matchings (if stored)' |
| 387 connection = sqlite3.connect(filename) | 391 connection = sqlite3.connect(filename) |
| 388 cursor = connection.cursor() | 392 cursor = connection.cursor() |
| 389 prototypeIndices = [] | 393 prototypeIndices = [] |
| 394 dbFilenames = {} | |
| 390 trajectoryTypes = [] | 395 trajectoryTypes = [] |
| 391 nMatchings = {} | 396 nMatchings = {} |
| 392 try: | 397 try: |
| 393 cursor.execute('SELECT * FROM prototypes') | 398 cursor.execute('SELECT * FROM prototypes') |
| 394 for row in cursor: | 399 for row in cursor: |
| 395 prototypeIndices.append(row[0]) | 400 prototypeIndices.append(row[0]) |
| 396 trajectoryTypes.append(row[1]) | 401 dbFilenames[row[0]] = row[1] |
| 402 trajectoryTypes.append(row[2]) | |
| 397 if row[2] is not None: | 403 if row[2] is not None: |
| 398 nMatchings[row[0]] = row[2] | 404 nMatchings[row[0]] = row[3] |
| 399 except sqlite3.OperationalError as error: | 405 except sqlite3.OperationalError as error: |
| 400 printDBError(error) | 406 printDBError(error) |
| 401 connection.close() | 407 connection.close() |
| 402 if len(set(trajectoryTypes)) > 1: | 408 if len(set(trajectoryTypes)) > 1: |
| 403 print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes))) | 409 print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes))) |
| 404 return prototypeIndices, trajectoryTypes[0], nMatchings | 410 return prototypeIndices, dbFilenames, trajectoryTypes[0], nMatchings |
| 405 | 411 |
| 406 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): | 412 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): |
| 407 '''Loads bounding box moving object from an SQLite | 413 '''Loads bounding box moving object from an SQLite |
| 408 (format of SQLite output by the ground truth annotation tool | 414 (format of SQLite output by the ground truth annotation tool |
| 409 or Urban Tracker | 415 or Urban Tracker |
