comparison python/cvutils.py @ 227:b7612c6d5702

cleaned the code
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 27 Jun 2012 09:52:06 -0400
parents 91197f6a03fe
children ab1a11176d7b
comparison
equal deleted inserted replaced
226:91197f6a03fe 227:b7612c6d5702
90 if lastCoordinate != None and lastCoordinate >=0: 90 if lastCoordinate != None and lastCoordinate >=0:
91 last = min(positions.length()-1, lastCoordinate) 91 last = min(positions.length()-1, lastCoordinate)
92 for i in range(0, last-1): 92 for i in range(0, last-1):
93 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) 93 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color)
94 94
95 def playVideo(filename, firstFrame = 0): 95 def playVideo(filename, firstFrameNum = 0):
96 '''Plays the video''' 96 '''Plays the video'''
97 capture = cv2.VideoCapture(filename) 97 capture = cv2.VideoCapture(filename)
98 if capture.isOpened(): 98 if capture.isOpened():
99 key = -1 99 key = -1
100 frameNum = 1 100 ret = True
101 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrame) 101 frameNum = firstFrameNum
102 while key!= 113: # 'q' 102 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
103 while ret and key!= 113: # 'q'
103 ret, img = capture.read() 104 ret, img = capture.read()
104 if ret: 105 if ret:
105 print('frame {0}'.format(frameNum)) 106 print('frame {0}'.format(frameNum))
106 frameNum+=1 107 frameNum+=1
107 cv2.imshow('frame', img) 108 cv2.imshow('frame', img)
126 cv2.imwrite('image{0:04d}.png'.format(numImg), img) 127 cv2.imwrite('image{0:04d}.png'.format(numImg), img)
127 else: 128 else:
128 images.append(img) 129 images.append(img)
129 return images 130 return images
130 131
131 def displayTrajectories(videoFilename, objects, homography = None): 132 def displayTrajectories(videoFilename, objects, homography = None, firstFrameNum = 0):
132 '''Displays the objects overlaid frame by frame over the video ''' 133 '''Displays the objects overlaid frame by frame over the video '''
133 capture = cv2.VideoCapture(videoFilename) 134 capture = cv2.VideoCapture(videoFilename)
134 if capture.isOpened(): 135 if capture.isOpened():
135 key = -1 136 key = -1
136 frameNum = 0 137 ret = True
137 while key!= 113: # 'q' 138 frameNum = firstFrameNum
139 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum)
140 while ret and key!= 113: # 'q'
141 print('capture')
138 ret, img = capture.read() 142 ret, img = capture.read()
139 if ret: 143 if ret:
140 print(frameNum) 144 print('frame {0}'.format(frameNum))
141 for obj in objects: 145 for obj in objects:
142 if obj.existsAtInstant(frameNum): 146 if obj.existsAtInstant(frameNum):
143 if obj.getFirstInstant() == frameNum: 147 if obj.getFirstInstant() == frameNum:
144 if homography != None: 148 if homography != None:
145 obj.projectedPositions = obj.positions.project(homography) 149 obj.projectedPositions = obj.positions.project(homography)