Mercurial Hosting > traffic-intelligence
changeset 248:571ba5ed22e2
added utils for bus processing
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Wed, 18 Jul 2012 02:54:22 -0400 |
parents | 8f0ed138d373 |
children | 99173da7afae 4978b5baf8f1 |
files | python/cvutils.py python/moving.py python/tests/moving.txt python/utils.py |
diffstat | 4 files changed, 54 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Wed Jul 18 02:54:02 2012 -0400 +++ b/python/cvutils.py Wed Jul 18 02:54:22 2012 -0400 @@ -129,7 +129,7 @@ images.append(img) return images - def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0): + def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0, lastFrameNumArg = None): '''Displays the objects overlaid frame by frame over the video ''' capture = cv2.VideoCapture(videoFilename) if capture.isOpened(): @@ -137,7 +137,12 @@ ret = True frameNum = firstFrameNum capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) - while ret and key!= 113: # 'q' + if not lastFrameNumArg: + from sys import maxint + lastFrameNum = maxint + else: + lastFrameNum = lastFrameNumArg + while ret and key!= 113 and frameNum < lastFrameNum: # 'q' ret, img = capture.read() if ret: print('frame {0}'.format(frameNum))
--- a/python/moving.py Wed Jul 18 02:54:02 2012 -0400 +++ b/python/moving.py Wed Jul 18 02:54:22 2012 -0400 @@ -4,7 +4,7 @@ import utils; import cvutils; -from math import sqrt, hypot; +from math import sqrt; #from shapely.geometry import Polygon @@ -48,13 +48,31 @@ return (self.first >= interval2.first) and (self.last <= interval2.last) def union(self, interval2): - '''Largest interval comprising self and interval2''' + '''Smallest interval comprising self and interval2''' return TimeInterval(min(self.first, interval2.first), max(self.last, interval2.last)) def intersection(self, interval2): '''Largest interval comprised in both self and interval2''' return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) + def distance(self, interval2): + if not self.intersection(interval2).empty(): + return 0 + elif self.first > interval2.last: + return self.first - interval2.last + elif self.last < interval2.first: + return interval2.first - self.last + else: + return None + + +def unionIntervals(intervals): + 'returns the smallest interval containing all intervals' + inter = intervals[0] + for i in intervals[1:]: + inter = inter.union(i) + return inter + class TimeInterval(Interval): '''Temporal interval based on frame numbers (hence the modified length method) @@ -379,7 +397,8 @@ # def add(x, y): return x+y # sq = map(add, [x*x for x in self.positions[0]], [y*y for y in self.positions[1]]) # return sqrt(sq) - return [hypot(x,y) for x,y in zip(self.positions[0], self.positions[1])] + from numpy import hypot + return hypot(self.positions[0], self.positions[1]) def cumulatedDisplacement(self): 'Returns the sum of the distances between each successive point' @@ -506,6 +525,9 @@ def drawOnWorldImage(self, nPixelsPerUnitDistance, imageHeight, options = '', withOrigin = False, **kwargs): self.positions.drawOnWorldImage(nPixelsPerUnitDistance, imageHeight, options, withOrigin, **kwargs) + def play(self, videoFilename, homography = None): + cvutils.displayTrajectories(videoFilename, [self], homography, self.getFirstInstant(), self.getLastInstant()) + def getInstantsCrossingLane(self, p1, p2): '''Returns the instant(s) at which the object passes from one side of the segment to the other
--- a/python/tests/moving.txt Wed Jul 18 02:54:02 2012 -0400 +++ b/python/tests/moving.txt Wed Jul 18 02:54:22 2012 -0400 @@ -29,6 +29,17 @@ >>> TimeInterval(2,5).equal(TimeInterval(5,2)) False +>>> TimeInterval(3,6).distance(TimeInterval(4,6)) +0 +>>> TimeInterval(3,6).distance(TimeInterval(6,10)) +0 +>>> TimeInterval(3,6).distance(TimeInterval(8,10)) +2 +>>> TimeInterval(20,30).distance(TimeInterval(3,15)) +5 +>>> Interval.union([TimeInterval(3,6), TimeInterval(8,10),TimeInterval(11,15)]) +[3,15] + >>> Point(3,4)-Point(1,7) (2.000000,-3.000000) @@ -36,7 +47,7 @@ 13 >>> Point.distanceNorm2(Point(3,4),Point(1,7)) -3.6055512754639891 +3.605551275463989 >>> Point(3,2).inPolygon([Point(0,0),Point(1,0),Point(1,1),Point(0,1)]) False @@ -60,23 +71,3 @@ (0.500000,0.500000) >>> t1.getTrajectoryInPolygon(np.array([[10,10],[14,10],[14,13],[10,13]])).length() 0 - ->>> indic1 = TemporalIndicator('bla', [0,3,-4], TimeInterval(4,6)) ->>> indic1.empty() -False ->>> indic1[5] -3 ->>> indic1[3] ->>> [v for v in indic1] -[0, 3, -4] ->>> indic1 = TemporalIndicator('bla', {2:0,4:3,5:-5}) ->>> indic1[4] -3 ->>> indic1[3] ->>> [v for v in indic1] -[0, 3, -5] - ->>> indicatorMap([1,2,3], t1, 1) -{(1.0, 3.0): 2.0, (2.0, 6.0): 3.0, (0.0, 0.0): 1.0} ->>> indicatorMap([1,2,3], t1, 4) -{(0.0, 1.0): 3.0, (0.0, 0.0): 1.5}
--- a/python/utils.py Wed Jul 18 02:54:02 2012 -0400 +++ b/python/utils.py Wed Jul 18 02:54:22 2012 -0400 @@ -164,6 +164,16 @@ # maths section ######################### +def framesToTime(nFrames, frameRate, initialTime = (0.,0.,0.)): + 'returns hour, minutes and seconds' + from math import floor + seconds = floor(float(nFrames)/float(frameRate)) + h = floor(seconds/3600.) + seconds = seconds - h*3600 + m = floor(seconds/60) + seconds = seconds - m*60 + return (h+initialTime[0], m+initialTime[1], seconds+initialTime[2]) + def sortXY(X,Y): 'returns the sorted (x, Y(x)) sorted on X' D = {}