comparison python/storage.py @ 844:5a68779d7777

added capability to save prototypes
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 14 Jul 2016 00:34:59 -0400
parents 119c4efe6398
children 0cb69238e6f5
comparison
equal deleted inserted replaced
843:5dc7a507353e 844:5a68779d7777
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, prototypes, trajectoryType = 'feature'): 367 def savePrototypesToSqlite(filename, prototypeIndices, trajectoryType, nMatchings = None):
368 'Work in progress, do not use' 368 '''save the prototype indices
369 connection = sqlite3.connect(filename) 369 nMatchings, if not None, is a dictionnary between indices and number of matches'''
370 cursor = connection.cursor() 370 connection = sqlite3.connect(filename)
371 try: 371 cursor = connection.cursor()
372 cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, object_id INTEGER, trajectory_id INTEGER, nMatchings INTEGER, FOREIGN KEY(object_id) REFERENCES objects(id), FOREIGN KEY(trajectory_id) REFERENCES positions(trajectory_id))') 372 try:
373 #for inter in interactions: 373 cursor.execute('CREATE TABLE IF NOT EXISTS prototypes (id INTEGER PRIMARY KEY, trajectory_type VARCHAR CHECK (trajectory_type IN (\"feature\", \"object\")), nMatchings INTEGER)')
374 # saveInteraction(cursor, inter) 374 for i in prototypeIndices:
375 if nMatchings is not None:
376 n = nMatchings[i]
377 else:
378 n = 'NULL'
379 cursor.execute('INSERT INTO prototypes (id, trajectory_type, nMatchings) VALUES ({},\"{}\",{})'.format(i, trajectoryType, n))
375 except sqlite3.OperationalError as error: 380 except sqlite3.OperationalError as error:
376 printDBError(error) 381 printDBError(error)
377 connection.commit() 382 connection.commit()
378 connection.close() 383 connection.close()
379 384
380 def loadPrototypesFromSqlite(filename): 385 def loadPrototypesFromSqlite(filename):
381 pass 386 'Loads prototype ids and matchings (if stored)'
387 connection = sqlite3.connect(filename)
388 cursor = connection.cursor()
389 prototypeIndices = []
390 trajectoryTypes = []
391 nMatchings = {}
392 try:
393 cursor.execute('SELECT * FROM prototypes')
394 for row in cursor:
395 prototypeIndices.append(row[0])
396 trajectoryTypes.append(row[1])
397 if row[2] is not None:
398 nMatchings[row[0]] = row[2]
399 except sqlite3.OperationalError as error:
400 printDBError(error)
401 connection.close()
402 if len(set(trajectoryTypes)) > 1:
403 print('Different types of prototypes in database ({}).'.format(set(trajectoryTypes)))
404 return prototypeIndices, trajectoryTypes[0], nMatchings
382 405
383 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None): 406 def loadBBMovingObjectsFromSqlite(filename, objectType = 'bb', objectNumbers = None, timeStep = None):
384 '''Loads bounding box moving object from an SQLite 407 '''Loads bounding box moving object from an SQLite
385 (format of SQLite output by the ground truth annotation tool 408 (format of SQLite output by the ground truth annotation tool
386 or Urban Tracker 409 or Urban Tracker
416 dropTables(connection, ['interactions', 'indicators']) 439 dropTables(connection, ['interactions', 'indicators'])
417 elif dataType == 'bb': 440 elif dataType == 'bb':
418 dropTables(connection, ['bounding_boxes']) 441 dropTables(connection, ['bounding_boxes'])
419 elif dataType == 'pois': 442 elif dataType == 'pois':
420 dropTables(connection, ['gaussians2d']) 443 dropTables(connection, ['gaussians2d'])
444 elif dataType == 'prototype':
445 dropTables(connection, ['prototypes'])
421 else: 446 else:
422 print('Unknown data type {} to delete from database'.format(dataType)) 447 print('Unknown data type {} to delete from database'.format(dataType))
423 connection.close() 448 connection.close()
424 else: 449 else:
425 print('{} does not exist'.format(filename)) 450 print('{} does not exist'.format(filename))