Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 305:ca9131968bce
added sample to replay video
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 29 Mar 2013 21:20:12 -0400 |
| parents | 514f6b98cd8c |
| children | 82b9be447608 |
comparison
equal
deleted
inserted
replaced
| 304:20f9cd972dde | 305:ca9131968bce |
|---|---|
| 22 cvBlue = (255,0,0) | 22 cvBlue = (255,0,0) |
| 23 cvColors = utils.PlottingPropertyValues([cvRed, | 23 cvColors = utils.PlottingPropertyValues([cvRed, |
| 24 cvGreen, | 24 cvGreen, |
| 25 cvBlue]) | 25 cvBlue]) |
| 26 | 26 |
| 27 def quitKey(key): | |
| 28 return chr(key&255)== 'q' or chr(key&255) == 'Q' | |
| 29 | |
| 30 def saveKey(key): | |
| 31 return chr(key&255) == 's' | |
| 32 | |
| 27 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): | 33 def drawLines(filename, origins, destinations, w = 1, resultFilename='image.png'): |
| 28 '''Draws lines over the image ''' | 34 '''Draws lines over the image ''' |
| 29 | 35 |
| 30 img = Image.open(filename) | 36 img = Image.open(filename) |
| 31 | 37 |
| 83 if lastCoordinate != None and lastCoordinate >=0: | 89 if lastCoordinate != None and lastCoordinate >=0: |
| 84 last = min(positions.length()-1, lastCoordinate) | 90 last = min(positions.length()-1, lastCoordinate) |
| 85 for i in range(0, last-1): | 91 for i in range(0, last-1): |
| 86 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) | 92 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color) |
| 87 | 93 |
| 88 def playVideo(filename, firstFrameNum = 0): | 94 def playVideo(filename, firstFrameNum = 0, frameRate = -1): |
| 89 '''Plays the video''' | 95 '''Plays the video''' |
| 96 wait = 5 | |
| 97 if frameRate > 0: | |
| 98 wait = int(round(1000./frameRate)) | |
| 90 capture = cv2.VideoCapture(filename) | 99 capture = cv2.VideoCapture(filename) |
| 91 if capture.isOpened(): | 100 if capture.isOpened(): |
| 92 key = -1 | 101 key = -1 |
| 93 ret = True | 102 ret = True |
| 94 frameNum = firstFrameNum | 103 frameNum = firstFrameNum |
| 95 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) | 104 capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) |
| 96 while ret and chr(key&255)!= 'q': | 105 while ret and not quitKey(key): |
| 97 ret, img = capture.read() | 106 ret, img = capture.read() |
| 98 if ret: | 107 if ret: |
| 99 print('frame {0}'.format(frameNum)) | 108 print('frame {0}'.format(frameNum)) |
| 100 frameNum+=1 | 109 frameNum+=1 |
| 101 cv2.imshow('frame', img) | 110 cv2.imshow('frame', img) |
| 102 key = cv2.waitKey(5) | 111 key = cv2.waitKey(wait) |
| 103 | 112 |
| 104 def getImagesFromVideo(filename, nImages = 1, saveImage = False): | 113 def getImagesFromVideo(filename, nImages = 1, saveImage = False): |
| 105 '''Returns nImages images from the video sequence''' | 114 '''Returns nImages images from the video sequence''' |
| 106 images = [] | 115 images = [] |
| 107 capture = cv2.VideoCapture(filename) | 116 capture = cv2.VideoCapture(filename) |
| 133 if not lastFrameNumArg: | 142 if not lastFrameNumArg: |
| 134 from sys import maxint | 143 from sys import maxint |
| 135 lastFrameNum = maxint | 144 lastFrameNum = maxint |
| 136 else: | 145 else: |
| 137 lastFrameNum = lastFrameNumArg | 146 lastFrameNum = lastFrameNumArg |
| 138 while ret and chr(key&255)!= 'q' and chr(key&255)!= 'Q' and frameNum < lastFrameNum: | 147 while ret and not quitKey(key) and frameNum < lastFrameNum: |
| 139 ret, img = capture.read() | 148 ret, img = capture.read() |
| 140 if ret: | 149 if ret: |
| 141 print('frame {0}'.format(frameNum)) | 150 print('frame {0}'.format(frameNum)) |
| 142 for obj in objects: | 151 for obj in objects: |
| 143 if obj.existsAtInstant(frameNum): | 152 if obj.existsAtInstant(frameNum): |
| 148 obj.projectedPositions = obj.positions | 157 obj.projectedPositions = obj.positions |
| 149 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) | 158 draw(img, obj.projectedPositions, cvRed, frameNum-obj.getFirstInstant()) |
| 150 cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) | 159 cv2.putText(img, '{0}'.format(obj.num), obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) |
| 151 cv2.imshow('frame', img) | 160 cv2.imshow('frame', img) |
| 152 key = cv2.waitKey() | 161 key = cv2.waitKey() |
| 153 if chr(key&255) == 's': | 162 if saveKey(key): |
| 154 cv2.imwrite('image.png', img) | 163 cv2.imwrite('image.png', img) |
| 155 frameNum += 1 | 164 frameNum += 1 |
| 156 | 165 |
| 157 def printCvMat(cvmat, out = stdout): | 166 def printCvMat(cvmat, out = stdout): |
| 158 '''Prints the cvmat to out''' | 167 '''Prints the cvmat to out''' |
