Mercurial > hg > nsaunier > traffic-intelligence
comparison python/cvutils.py @ 1012:01db14e947e4
resolved
| author | Wendlasida |
|---|---|
| date | Fri, 01 Jun 2018 10:47:49 -0400 |
| parents | 933670761a57 |
| children | 75af46516b2b |
comparison
equal
deleted
inserted
replaced
| 1011:4f0312bee393 | 1012:01db14e947e4 |
|---|---|
| 55 def saveKey(key): | 55 def saveKey(key): |
| 56 return chr(key&255) == 's' | 56 return chr(key&255) == 's' |
| 57 | 57 |
| 58 def int2FOURCC(x): | 58 def int2FOURCC(x): |
| 59 fourcc = '' | 59 fourcc = '' |
| 60 for i in xrange(4): | 60 for i in range(4): |
| 61 fourcc += unichr((x >> 8*i)&255) | 61 fourcc += chr((x >> 8*i)&255) |
| 62 return fourcc | 62 return fourcc |
| 63 | 63 |
| 64 def rgb2gray(rgb): | 64 def rgb2gray(rgb): |
| 65 return dot(rgb[...,:3], [0.299, 0.587, 0.144]) | 65 return dot(rgb[...,:3], [0.299, 0.587, 0.144]) |
| 66 | 66 |
| 77 | 77 |
| 78 def cvMatToArray(cvmat): | 78 def cvMatToArray(cvmat): |
| 79 '''Converts an OpenCV CvMat to numpy array.''' | 79 '''Converts an OpenCV CvMat to numpy array.''' |
| 80 print('Deprecated, use new interface') | 80 print('Deprecated, use new interface') |
| 81 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) | 81 a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height) |
| 82 for i in xrange(cvmat.rows): | 82 for i in range(cvmat.rows): |
| 83 for j in xrange(cvmat.cols): | 83 for j in range(cvmat.cols): |
| 84 a[i,j] = cvmat[i,j] | 84 a[i,j] = cvmat[i,j] |
| 85 return a | 85 return a |
| 86 | 86 |
| 87 def createWhiteImage(height, width, filename): | 87 def createWhiteImage(height, width, filename): |
| 88 img = ones((height, width, 3), uint8)*255 | 88 img = ones((height, width, 3), uint8)*255 |
| 125 colorType = 'default' | 125 colorType = 'default' |
| 126 if len(filenames) == 0: | 126 if len(filenames) == 0: |
| 127 print('Empty filename list') | 127 print('Empty filename list') |
| 128 return | 128 return |
| 129 if windowNames is None: | 129 if windowNames is None: |
| 130 windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))] | 130 windowNames = ['frame{}'.format(i) for i in range(len(filenames))] |
| 131 wait = 5 | 131 wait = 5 |
| 132 if rescale == 1.: | 132 if rescale == 1.: |
| 133 for windowName in windowNames: | 133 for windowName in windowNames: |
| 134 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) | 134 cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) |
| 135 if frameRate > 0: | 135 if frameRate > 0: |
| 140 if array([cap.isOpened() for cap in captures]).all(): | 140 if array([cap.isOpened() for cap in captures]).all(): |
| 141 key = -1 | 141 key = -1 |
| 142 ret = True | 142 ret = True |
| 143 nFramesShown = 0 | 143 nFramesShown = 0 |
| 144 if firstFrameNums is not None: | 144 if firstFrameNums is not None: |
| 145 for i in xrange(len(captures)): | 145 for i in range(len(captures)): |
| 146 captures[i].set(cv2.PROP_POS_FRAMES, firstFrameNums[i]) | 146 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]) |
| 147 while ret and not quitKey(key): | 147 while ret and not quitKey(key): |
| 148 rets = [] | 148 rets = [] |
| 149 images = [] | 149 images = [] |
| 150 for cap in captures: | 150 for cap in captures: |
| 151 ret, img = cap.read() | 151 ret, img = cap.read() |
| 153 images.append(img) | 153 images.append(img) |
| 154 ret = array(rets).all() | 154 ret = array(rets).all() |
| 155 if ret: | 155 if ret: |
| 156 if printFrames: | 156 if printFrames: |
| 157 print('frame shown {0}'.format(nFramesShown)) | 157 print('frame shown {0}'.format(nFramesShown)) |
| 158 for i in xrange(len(filenames)): | 158 for i in range(len(filenames)): |
| 159 if text is not None: | 159 if text is not None: |
| 160 cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed[colorType]) | 160 cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed[colorType]) |
| 161 cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img) | 161 cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img) |
| 162 key = cv2.waitKey(wait) | 162 key = cv2.waitKey(wait) |
| 163 if saveKey(key): | 163 if saveKey(key): |
| 164 cv2.imwrite('image-{}.png'.format(frameNum), img) | 164 cv2.imwrite('image-{}.png'.format(frameNum), img) |
| 165 nFramesShown += step | 165 nFramesShown += step |
| 166 if step > 1: | 166 if step > 1: |
| 167 for i in xrange(len(captures)): | 167 for i in range(len(captures)): |
| 168 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown) | 168 captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown) |
| 169 cv2.destroyAllWindows() | 169 cv2.destroyAllWindows() |
| 170 else: | 170 else: |
| 171 print('Video captures for {} failed'.format(filenames)) | 171 print('Video captures for {} failed'.format(filenames)) |
| 172 | 172 |
| 329 if not hasattr(obj, 'projectedPositions'): | 329 if not hasattr(obj, 'projectedPositions'): |
| 330 obj.projectedPositions = obj.getPositions().homographyProject(homography) | 330 obj.projectedPositions = obj.getPositions().homographyProject(homography) |
| 331 if undistort: | 331 if undistort: |
| 332 obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix) | 332 obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix) |
| 333 cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant()) | 333 cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant()) |
| 334 if frameNum not in boundingBoxes.keys() and obj.hasFeatures(): | 334 if frameNum not in boundingBoxes and obj.hasFeatures(): |
| 335 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height) | 335 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height) |
| 336 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 1) | 336 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 1) |
| 337 objDescription = '{} '.format(obj.num) | 337 objDescription = '{} '.format(obj.num) |
| 338 if moving.userTypeNames[obj.userType] != 'unknown': | 338 if moving.userTypeNames[obj.userType] != 'unknown': |
| 339 objDescription += moving.userTypeNames[obj.userType][0].upper() | 339 objDescription += moving.userTypeNames[obj.userType][0].upper() |
| 342 objDescription += " FA" | 342 objDescription += " FA" |
| 343 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvColors[colorType][obj.getNum()]) | 343 cv2.putText(img, objDescription, obj.projectedPositions[frameNum-obj.getFirstInstant()].asint().astuple(), cv2.FONT_HERSHEY_PLAIN, 1, cvColors[colorType][obj.getNum()]) |
| 344 if obj.getLastInstant() == frameNum: | 344 if obj.getLastInstant() == frameNum: |
| 345 objects.remove(obj) | 345 objects.remove(obj) |
| 346 # plot object bounding boxes | 346 # plot object bounding boxes |
| 347 if frameNum in boundingBoxes.keys(): | 347 if frameNum in boundingBoxes: |
| 348 for rect in boundingBoxes[frameNum]: | 348 for rect in boundingBoxes[frameNum]: |
| 349 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvColors[colorType][obj.getNum()]) | 349 cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvColors[colorType][obj.getNum()]) |
| 350 # plot ground truth | 350 # plot ground truth |
| 351 if len(annotations) > 0: | 351 if len(annotations) > 0: |
| 352 for gt in annotations: | 352 for gt in annotations: |
| 504 width = img.shape[1] | 504 width = img.shape[1] |
| 505 height = img.shape[0] | 505 height = img.shape[0] |
| 506 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) | 506 [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients) |
| 507 return cv2.remap(img, map1, map2, interpolation=interpolation) | 507 return cv2.remap(img, map1, map2, interpolation=interpolation) |
| 508 | 508 |
| 509 | |
| 510 def printCvMat(cvmat, out = stdout): | |
| 511 '''Prints the cvmat to out''' | |
| 512 print('Deprecated, use new interface') | |
| 513 for i in xrange(cvmat.rows): | |
| 514 for j in xrange(cvmat.cols): | |
| 515 out.write('{0} '.format(cvmat[i,j])) | |
| 516 out.write('\n') | |
| 517 | |
| 518 def homographyProject(points, homography, output3D = False): | 509 def homographyProject(points, homography, output3D = False): |
| 519 '''Returns the coordinates of the points (2xN array) projected through homography''' | 510 '''Returns the coordinates of the points (2xN array) projected through homography''' |
| 520 if points.shape[0] != 2: | 511 if points.shape[0] != 2: |
| 521 raise Exception('points of dimension {}'.format(points.shape)) | 512 raise Exception('points of dimension {}'.format(points.shape)) |
| 522 | 513 |
