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