Mercurial Hosting > traffic-intelligence
comparison python/cvutils.py @ 203:e2f31813ade6
added code to display trajectories on videa
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Mar 2012 18:10:19 -0500 |
parents | 8e7b354666ec |
children | 966c2cd2bd9f |
comparison
equal
deleted
inserted
replaced
202:b0b964ba9489 | 203:e2f31813ade6 |
---|---|
83 for i in range(cvmat.rows): | 83 for i in range(cvmat.rows): |
84 for j in range(cvmat.cols): | 84 for j in range(cvmat.cols): |
85 cvmat[i,j] = a[i,j] | 85 cvmat[i,j] = a[i,j] |
86 return cvmat | 86 return cvmat |
87 | 87 |
88 def draw(img, positions, color, lastCoordinate = None): | |
89 last = lastCoordinate+1 | |
90 if lastCoordinate != None and lastCoordinate >=0: | |
91 last = min(positions.length()-1, lastCoordinate) | |
92 for i in range(0, last-1): | |
93 cv2.line(img, positions[i].astuple(), positions[i+1].astuple(), color) | |
94 | |
88 def playVideo(filename): | 95 def playVideo(filename): |
89 '''Plays the video''' | 96 '''Plays the video''' |
90 capture = cv2.VideoCapture(filename) | 97 capture = cv2.VideoCapture(filename) |
91 if capture.isOpened(): | 98 if capture.isOpened(): |
92 key = -1 | 99 key = -1 |
93 while key!= 1048689: # 'q' | 100 while key!= 113: # 'q' |
94 ret, img = capture.read() | 101 ret, img = capture.read() |
95 if ret: | 102 if ret: |
96 cv2.imshow('frame', img) | 103 cv2.imshow('frame', img) |
97 key = cv2.waitKey(5) | 104 key = cv2.waitKey(5) |
98 | 105 |
108 ret, img = capture.read() | 115 ret, img = capture.read() |
109 if img.size>0: | 116 if img.size>0: |
110 images.append(img) | 117 images.append(img) |
111 return images | 118 return images |
112 | 119 |
120 def displayTrajectories(videoFilename, objects, homography = None): | |
121 '''Displays the objects overlaid frame by frame over the video ''' | |
122 capture = cv2.VideoCapture(videoFilename) | |
123 if capture.isOpened(): | |
124 key = -1 | |
125 frameNum = 1 | |
126 while key!= 113: # 'q' | |
127 ret, img = capture.read() | |
128 if ret: | |
129 print(frameNum) | |
130 for obj in objects: | |
131 if obj.existsAtInstant(frameNum): | |
132 #obj.getTimeInterval() | |
133 if homography != None and obj.getFirstInstant() == frameNum: | |
134 obj.projectedPositions = obj.positions.project(homography) | |
135 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) | |
136 cv2.imshow('frame', img) | |
137 key = cv2.waitKey(50) | |
138 frameNum += 1 | |
139 | |
113 def printCvMat(cvmat, out = stdout): | 140 def printCvMat(cvmat, out = stdout): |
114 '''Prints the cvmat to out''' | 141 '''Prints the cvmat to out''' |
115 for i in xrange(cvmat.rows): | 142 for i in xrange(cvmat.rows): |
116 for j in xrange(cvmat.cols): | 143 for j in xrange(cvmat.cols): |
117 out.write('{0} '.format(cvmat[i,j])) | 144 out.write('{0} '.format(cvmat[i,j])) |