comparison trafficintelligence/storage.py @ 1065:d4d052a05337

added progress report functionality
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Mon, 16 Jul 2018 00:05:17 -0400
parents 16575ca4537d
children 3939ae415be0
comparison
equal deleted inserted replaced
1064:cbc026dacf0b 1065:d4d052a05337
68 cursor.execute('SELECT COUNT(*) FROM SQLITE_MASTER WHERE type = \'table\' AND name = \''+tableName+'\'') 68 cursor.execute('SELECT COUNT(*) FROM SQLITE_MASTER WHERE type = \'table\' AND name = \''+tableName+'\'')
69 return cursor.fetchone()[0] == 1 69 return cursor.fetchone()[0] == 1
70 except sqlite3.OperationalError as error: 70 except sqlite3.OperationalError as error:
71 printDBError(error) 71 printDBError(error)
72 72
73 def tableNames(filename):
74 'Lists the names of the tables in the SQLite file'
75 if Path(filename).is_file():
76 with sqlite3.connect(filename) as connection:
77 try:
78 cursor = connection.cursor()
79 cursor.execute('SELECT name FROM sqlite_master WHERE type = \'table\'')
80 return [row[0] for row in cursor]
81 except sqlite3.OperationalError as error:
82 printDBError(error)
83 return []
84
73 def createTrajectoryTable(cursor, tableName): 85 def createTrajectoryTable(cursor, tableName):
74 if tableName.endswith('positions') or tableName.endswith('velocities'): 86 if tableName.endswith('positions') or tableName.endswith('velocities'):
75 cursor.execute("CREATE TABLE IF NOT EXISTS "+tableName+" (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))") 87 cursor.execute("CREATE TABLE IF NOT EXISTS "+tableName+" (trajectory_id INTEGER, frame_number INTEGER, x_coordinate REAL, y_coordinate REAL, PRIMARY KEY(trajectory_id, frame_number))")
76 else: 88 else:
77 print('Unallowed name {} for trajectory table'.format(tableName)) 89 print('Unallowed name {} for trajectory table'.format(tableName))
257 if object, features can be loaded with withFeatures or nLongestObjectFeatures used to select the n longest features 269 if object, features can be loaded with withFeatures or nLongestObjectFeatures used to select the n longest features
258 270
259 The number loaded is either the first objectNumbers objects, 271 The number loaded is either the first objectNumbers objects,
260 or the indices in objectNumbers from the database''' 272 or the indices in objectNumbers from the database'''
261 objects = [] 273 objects = []
262 if Path(filename).exists(): 274 if Path(filename).is_file():
263 with sqlite3.connect(filename) as connection: 275 with sqlite3.connect(filename) as connection:
264 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep) 276 objects = loadTrajectoriesFromTable(connection, 'positions', trajectoryType, objectNumbers, timeStep)
265 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep) 277 objectVelocities = loadTrajectoriesFromTable(connection, 'velocities', trajectoryType, objectNumbers, timeStep)
266 278
267 if len(objectVelocities) > 0: 279 if len(objectVelocities) > 0:
450 (format of SQLite output by the ground truth annotation tool 462 (format of SQLite output by the ground truth annotation tool
451 or Urban Tracker 463 or Urban Tracker
452 464
453 Load descriptions?''' 465 Load descriptions?'''
454 objects = [] 466 objects = []
455 if Path(filename).exists(): 467 if Path(filename).is_file():
456 with sqlite3.connect(filename) as connection: 468 with sqlite3.connect(filename) as connection:
457 if objectType == 'bb': 469 if objectType == 'bb':
458 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep) 470 topCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbtop', objectNumbers, timeStep)
459 bottomCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbbottom', objectNumbers, timeStep) 471 bottomCorners = loadTrajectoriesFromTable(connection, 'bounding_boxes', 'bbbottom', objectNumbers, timeStep)
460 userTypes = loadObjectAttributesFromTable(connection.cursor(), objectNumbers) # string format is same as object 472 userTypes = loadObjectAttributesFromTable(connection.cursor(), objectNumbers) # string format is same as object
509 def loadInteractionsFromSqlite(filename): 521 def loadInteractionsFromSqlite(filename):
510 '''Loads interaction and their indicators 522 '''Loads interaction and their indicators
511 523
512 TODO choose the interactions to load''' 524 TODO choose the interactions to load'''
513 interactions = [] 525 interactions = []
514 if Path(filename).exists(): 526 if Path(filename).is_file():
515 with sqlite3.connect(filename) as connection: 527 with sqlite3.connect(filename) as connection:
516 cursor = connection.cursor() 528 cursor = connection.cursor()
517 try: 529 try:
518 cursor.execute('SELECT INT.id, INT.object_id1, INT.object_id2, INT.first_frame_number, INT.last_frame_number, IND.indicator_type, IND.frame_number, IND.value from interactions INT, indicators IND WHERE INT.id = IND.interaction_id ORDER BY INT.id, IND.indicator_type, IND.frame_number') 530 cursor.execute('SELECT INT.id, INT.object_id1, INT.object_id2, INT.first_frame_number, INT.last_frame_number, IND.indicator_type, IND.frame_number, IND.value from interactions INT, indicators IND WHERE INT.id = IND.interaction_id ORDER BY INT.id, IND.indicator_type, IND.frame_number')
519 interactionNum = -1 531 interactionNum = -1
555 connection.commit() 567 connection.commit()
556 568
557 def loadBoundingBoxTableForDisplay(filename): 569 def loadBoundingBoxTableForDisplay(filename):
558 '''Loads bounding boxes from bounding_boxes table for display over trajectories''' 570 '''Loads bounding boxes from bounding_boxes table for display over trajectories'''
559 boundingBoxes = {} # list of bounding boxes for each instant 571 boundingBoxes = {} # list of bounding boxes for each instant
560 if Path(filename).exists(): 572 if Path(filename).is_file():
561 with sqlite3.connect(filename) as connection: 573 with sqlite3.connect(filename) as connection:
562 cursor = connection.cursor() 574 cursor = connection.cursor()
563 try: 575 try:
564 cursor.execute('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'bounding_boxes\'') 576 cursor.execute('SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'bounding_boxes\'')
565 result = cursor.fetchall() 577 result = cursor.fetchall()
626 printDBError(error) 638 printDBError(error)
627 connection.commit() 639 connection.commit()
628 640
629 def loadPrototypeAssignmentsFromSqlite(filename, objectType): 641 def loadPrototypeAssignmentsFromSqlite(filename, objectType):
630 prototypeAssignments = {} 642 prototypeAssignments = {}
631 if Path(filename).exists(): 643 if Path(filename).is_file():
632 with sqlite3.connect(filename) as connection: 644 with sqlite3.connect(filename) as connection:
633 cursor = connection.cursor() 645 cursor = connection.cursor()
634 try: 646 try:
635 tableName, objectIdColumnName = prototypeAssignmentNames(objectType) 647 tableName, objectIdColumnName = prototypeAssignmentNames(objectType)
636 cursor.execute('SELECT * FROM '+tableName) 648 cursor.execute('SELECT * FROM '+tableName)
646 return prototypeAssignments 658 return prototypeAssignments
647 659
648 def loadPrototypesFromSqlite(filename, withTrajectories = True): 660 def loadPrototypesFromSqlite(filename, withTrajectories = True):
649 'Loads prototype ids and matchings (if stored)' 661 'Loads prototype ids and matchings (if stored)'
650 prototypes = [] 662 prototypes = []
651 if Path(filename).exists(): 663 if Path(filename).is_file():
652 with sqlite3.connect(filename) as connection: 664 with sqlite3.connect(filename) as connection:
653 cursor = connection.cursor() 665 cursor = connection.cursor()
654 objects = [] 666 objects = []
655 try: 667 try:
656 cursor.execute('SELECT * FROM prototypes') 668 cursor.execute('SELECT * FROM prototypes')
707 def loadPOIsFromSqlite(filename): 719 def loadPOIsFromSqlite(filename):
708 'Loads all 2D Gaussians in the database' 720 'Loads all 2D Gaussians in the database'
709 from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields 721 from sklearn import mixture # todo if not avalaible, load data in duck-typed class with same fields
710 from ast import literal_eval 722 from ast import literal_eval
711 pois = [] 723 pois = []
712 if Path(filename).exists(): 724 if Path(filename).is_file():
713 with sqlite3.connect(filename) as connection: 725 with sqlite3.connect(filename) as connection:
714 cursor = connection.cursor() 726 cursor = connection.cursor()
715 try: 727 try:
716 cursor.execute('SELECT * from gaussians2d') 728 cursor.execute('SELECT * from gaussians2d')
717 gmmId = None 729 gmmId = None
1332 self.meanVehicleSpeed = config.getfloat(self.sectionHeader, 'mean-veh-speed') 1344 self.meanVehicleSpeed = config.getfloat(self.sectionHeader, 'mean-veh-speed')
1333 self.stdVehicleSpeed = config.getfloat(self.sectionHeader, 'std-veh-speed') 1345 self.stdVehicleSpeed = config.getfloat(self.sectionHeader, 'std-veh-speed')
1334 1346
1335 def __init__(self, filename = None): 1347 def __init__(self, filename = None):
1336 self.configFilename = filename 1348 self.configFilename = filename
1337 if filename is not None and Path(filename).exists(): 1349 if filename is not None and Path(filename).is_file():
1338 self.loadConfigFile(filename) 1350 self.loadConfigFile(filename)
1339 else: 1351 else:
1340 print('Configuration filename {} could not be loaded.'.format(filename)) 1352 print('Configuration filename {} could not be loaded.'.format(filename))
1341 1353
1342 def convertToFrames(self, frameRate, speedRatio = 3.6): 1354 def convertToFrames(self, frameRate, speedRatio = 3.6):
1422 config = ConfigParser(strict=False) 1434 config = ConfigParser(strict=False)
1423 config.read_file(addSectionHeader(utils.openCheck(filename))) 1435 config.read_file(addSectionHeader(utils.openCheck(filename)))
1424 parentPath = Path(filename).parent 1436 parentPath = Path(filename).parent
1425 self.sectionHeader = config.sections()[0] 1437 self.sectionHeader = config.sections()[0]
1426 self.homographyFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'homography-filename')) 1438 self.homographyFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'homography-filename'))
1427 if Path(self.homographyFilename).exists(): 1439 if Path(self.homographyFilename).is_file():
1428 self.homography = loadtxt(self.homographyFilename) 1440 self.homography = loadtxt(self.homographyFilename)
1429 else: 1441 else:
1430 self.homography = None 1442 self.homography = None
1431 1443
1432 def loadDistortionConfig(self, filename = None): 1444 def loadDistortionConfig(self, filename = None):
1441 config = ConfigParser(strict=False) 1453 config = ConfigParser(strict=False)
1442 config.read_file(addSectionHeader(utils.openCheck(filename))) 1454 config.read_file(addSectionHeader(utils.openCheck(filename)))
1443 parentPath = Path(filename).parent 1455 parentPath = Path(filename).parent
1444 self.sectionHeader = config.sections()[0] 1456 self.sectionHeader = config.sections()[0]
1445 self.intrinsicCameraFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'intrinsic-camera-filename')) 1457 self.intrinsicCameraFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'intrinsic-camera-filename'))
1446 if Path(self.intrinsicCameraFilename).exists(): 1458 if Path(self.intrinsicCameraFilename).is_file():
1447 self.intrinsicCameraMatrix = loadtxt(self.intrinsicCameraFilename) 1459 self.intrinsicCameraMatrix = loadtxt(self.intrinsicCameraFilename)
1448 else: 1460 else:
1449 self.intrinsicCameraMatrix = None 1461 self.intrinsicCameraMatrix = None
1450 distortionCoefficients = getValuesFromINIFile(filename, 'distortion-coefficients', '=') 1462 distortionCoefficients = getValuesFromINIFile(filename, 'distortion-coefficients', '=')
1451 self.distortionCoefficients = [float(x) for x in distortionCoefficients] 1463 self.distortionCoefficients = [float(x) for x in distortionCoefficients]
1463 self.sectionHeader = config.sections()[0] 1475 self.sectionHeader = config.sections()[0]
1464 # Tracking/display parameters 1476 # Tracking/display parameters
1465 self.videoFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'video-filename')) 1477 self.videoFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'video-filename'))
1466 self.databaseFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'database-filename')) 1478 self.databaseFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'database-filename'))
1467 self.homographyFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'homography-filename')) 1479 self.homographyFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'homography-filename'))
1468 if Path(self.homographyFilename).exists(): 1480 if Path(self.homographyFilename).is_file():
1469 self.homography = loadtxt(self.homographyFilename) 1481 self.homography = loadtxt(self.homographyFilename)
1470 else: 1482 else:
1471 self.homography = None 1483 self.homography = None
1472 self.intrinsicCameraFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'intrinsic-camera-filename')) 1484 self.intrinsicCameraFilename = utils.getRelativeFilename(parentPath, config.get(self.sectionHeader, 'intrinsic-camera-filename'))
1473 if Path(self.intrinsicCameraFilename).exists(): 1485 if Path(self.intrinsicCameraFilename).is_file():
1474 self.intrinsicCameraMatrix = loadtxt(self.intrinsicCameraFilename) 1486 self.intrinsicCameraMatrix = loadtxt(self.intrinsicCameraFilename)
1475 else: 1487 else:
1476 self.intrinsicCameraMatrix = None 1488 self.intrinsicCameraMatrix = None
1477 distortionCoefficients = getValuesFromINIFile(filename, 'distortion-coefficients', '=') 1489 distortionCoefficients = getValuesFromINIFile(filename, 'distortion-coefficients', '=')
1478 self.distortionCoefficients = [float(x) for x in distortionCoefficients] 1490 self.distortionCoefficients = [float(x) for x in distortionCoefficients]
1503 self.lcssMetric = config.get(self.sectionHeader, 'lcss-metric') 1515 self.lcssMetric = config.get(self.sectionHeader, 'lcss-metric')
1504 self.minLcssSimilarity = config.getfloat(self.sectionHeader, 'min-lcss-similarity') 1516 self.minLcssSimilarity = config.getfloat(self.sectionHeader, 'min-lcss-similarity')
1505 1517
1506 def __init__(self, filename = None): 1518 def __init__(self, filename = None):
1507 self.configFilename = filename 1519 self.configFilename = filename
1508 if filename is not None and Path(filename).exists(): 1520 if filename is not None and Path(filename).is_file():
1509 self.loadConfigFile(filename) 1521 self.loadConfigFile(filename)
1510 else: 1522 else:
1511 print('Configuration filename {} could not be loaded.'.format(filename)) 1523 print('Configuration filename {} could not be loaded.'.format(filename))
1512 1524
1513 def processVideoArguments(args): 1525 def processVideoArguments(args):