Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 509:935430b1d408
corrected mask bug in feature tracking, updated display-trajectories to display on undistorted image
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 23 May 2014 16:27:26 -0400 |
parents | 343cfd185ca6 |
children | ad518f0c3218 |
comparison
equal
deleted
inserted
replaced
508:6f7fa0093162 | 509:935430b1d408 |
---|---|
4 #from numpy import * | 4 #from numpy import * |
5 #from pylab import * | 5 #from pylab import * |
6 from datetime import time, datetime | 6 from datetime import time, datetime |
7 | 7 |
8 __metaclass__ = type | 8 __metaclass__ = type |
9 | |
10 commentChar = '#' | |
11 | |
12 delimiterChar = '%'; | |
13 | 9 |
14 datetimeFormat = "%Y-%m-%d %H:%M:%S" | 10 datetimeFormat = "%Y-%m-%d %H:%M:%S" |
15 | 11 |
16 ######################### | 12 ######################### |
17 # Enumerations | 13 # Enumerations |
460 | 456 |
461 ######################### | 457 ######################### |
462 # file I/O section | 458 # file I/O section |
463 ######################### | 459 ######################### |
464 | 460 |
465 def openCheck(filename, option = 'r', quit = False): | |
466 '''Open file filename in read mode by default | |
467 and checks it is open''' | |
468 try: | |
469 return open(filename, option) | |
470 except IOError: | |
471 print 'File %s could not be opened.' % filename | |
472 if quit: | |
473 from sys import exit | |
474 exit() | |
475 return None | |
476 | |
477 def readline(f, commentCharacter = commentChar): | |
478 '''Modified readline function to skip comments.''' | |
479 s = f.readline() | |
480 while (len(s) > 0) and s.startswith(commentCharacter): | |
481 s = f.readline() | |
482 return s.strip() | |
483 | |
484 def getLines(f, delimiterCharacter = delimiterChar): | |
485 '''Gets a complete entry (all the lines) in between delimiterChar.''' | |
486 dataStrings = [] | |
487 s = readline(f) | |
488 while (len(s) > 0) and (not s.startswith(delimiterCharacter)): | |
489 dataStrings += [s.strip()] | |
490 s = readline(f) | |
491 return dataStrings | |
492 | |
493 class FakeSecHead(object): | |
494 '''Add fake section header [asection] | |
495 | |
496 from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 | |
497 use read_file in Python 3.2+ | |
498 ''' | |
499 def __init__(self, fp): | |
500 self.fp = fp | |
501 self.sechead = '[main]\n' | |
502 | |
503 def readline(self): | |
504 if self.sechead: | |
505 try: return self.sechead | |
506 finally: self.sechead = None | |
507 else: return self.fp.readline() | |
508 | |
509 def removeExtension(filename, delimiter = '.'): | 461 def removeExtension(filename, delimiter = '.'): |
510 '''Returns the filename minus the extension (all characters after last .)''' | 462 '''Returns the filename minus the extension (all characters after last .)''' |
511 i = filename.rfind(delimiter) | 463 i = filename.rfind(delimiter) |
512 if i>0: | 464 if i>0: |
513 return filename[:i] | 465 return filename[:i] |
587 | 539 |
588 return optionValues | 540 return optionValues |
589 | 541 |
590 | 542 |
591 ######################### | 543 ######################### |
592 # Utils to read .ini type text files for configuration, meta data... | |
593 ######################### | |
594 | |
595 class TrackingParameters: | |
596 '''Class for tracking and safety parameters | |
597 | |
598 Note: framerate is already taken into account''' | |
599 def loadConfigFile(self, filename): | |
600 from ConfigParser import ConfigParser | |
601 from numpy import loadtxt | |
602 from os import path | |
603 | |
604 config = ConfigParser() | |
605 config.readfp(FakeSecHead(openCheck(filename))) | |
606 self.sectionHeader = config.sections()[0] | |
607 self.videoFilename = config.get(self.sectionHeader, 'video-filename') | |
608 self.databaseFilename = config.get(self.sectionHeader, 'database-filename') | |
609 self.homographyFilename = config.get(self.sectionHeader, 'homography-filename') | |
610 if (path.exists(self.homographyFilename)): | |
611 self.homography = loadtxt(self.homographyFilename) | |
612 else: | |
613 self.homography = None | |
614 self.firstFrameNum = config.getint(self.sectionHeader, 'frame1') | |
615 self.videoFrameRate = config.getfloat(self.sectionHeader, 'video-fps') | |
616 | |
617 self.maxPredictedSpeed = config.getfloat(self.sectionHeader, 'max-predicted-speed')/3.6/self.videoFrameRate | |
618 self.predictionTimeHorizon = config.getfloat(self.sectionHeader, 'prediction-time-horizon')*self.videoFrameRate | |
619 self.collisionDistance = config.getfloat(self.sectionHeader, 'collision-distance') | |
620 self.crossingZones = config.getboolean(self.sectionHeader, 'crossing-zones') | |
621 self.predictionMethod = config.get(self.sectionHeader, 'prediction-method') | |
622 self.nPredictedTrajectories = config.getint(self.sectionHeader, 'npredicted-trajectories') | |
623 self.maxNormalAcceleration = config.getfloat(self.sectionHeader, 'max-normal-acceleration')/self.videoFrameRate**2 | |
624 self.maxNormalSteering = config.getfloat(self.sectionHeader, 'max-normal-steering')/self.videoFrameRate | |
625 self.minExtremeAcceleration = config.getfloat(self.sectionHeader, 'min-extreme-acceleration')/self.videoFrameRate**2 | |
626 self.maxExtremeAcceleration = config.getfloat(self.sectionHeader, 'max-extreme-acceleration')/self.videoFrameRate**2 | |
627 self.maxExtremeSteering = config.getfloat(self.sectionHeader, 'max-extreme-steering')/self.videoFrameRate | |
628 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction') | |
629 | |
630 class SceneParameters: | |
631 def __init__(self, config, sectionName): | |
632 from ConfigParser import NoOptionError | |
633 from ast import literal_eval | |
634 try: | |
635 self.sitename = config.get(sectionName, 'sitename') | |
636 self.databaseFilename = config.get(sectionName, 'data-filename') | |
637 self.homographyFilename = config.get(sectionName, 'homography-filename') | |
638 self.calibrationFilename = config.get(sectionName, 'calibration-filename') | |
639 self.videoFilename = config.get(sectionName, 'video-filename') | |
640 self.frameRate = config.getfloat(sectionName, 'framerate') | |
641 self.date = datetime.strptime(config.get(sectionName, 'date'), datetimeFormat) # 2011-06-22 11:00:39 | |
642 self.translation = literal_eval(config.get(sectionName, 'translation')) # = [0.0, 0.0] | |
643 self.rotation = config.getfloat(sectionName, 'rotation') | |
644 self.duration = config.getint(sectionName, 'duration') | |
645 except NoOptionError as e: | |
646 print(e) | |
647 print('Not a section for scene meta-data') | |
648 | |
649 @staticmethod | |
650 def loadConfigFile(filename): | |
651 from ConfigParser import ConfigParser | |
652 config = ConfigParser() | |
653 config.readfp(openCheck(filename)) | |
654 configDict = dict() | |
655 for sectionName in config.sections(): | |
656 configDict[sectionName] = SceneParameters(config, sectionName) | |
657 return configDict | |
658 | |
659 ######################### | |
660 # running tests | 544 # running tests |
661 ######################### | 545 ######################### |
662 | 546 |
663 if __name__ == "__main__": | 547 if __name__ == "__main__": |
664 import doctest | 548 import doctest |