Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/cvutils.py @ 1029:c6cf75a2ed08
reorganization of imports
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 18 Jun 2018 22:50:59 -0400 |
| parents | cc5cb04b04b0 |
| children | 956a66096e91 |
comparison
equal
deleted
inserted
replaced
| 1028:cc5cb04b04b0 | 1029:c6cf75a2ed08 |
|---|---|
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 '''Image/Video utilities''' | 2 '''Image/Video utilities''' |
| 3 | 3 |
| 4 from trafficintelligence import utils, moving | 4 from sys import stdout |
| 5 from os import listdir | |
| 6 from subprocess import run | |
| 7 from math import floor, log10, ceil | |
| 8 from time import time | |
| 9 | |
| 10 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil | |
| 11 from numpy.linalg import inv | |
| 12 from matplotlib.mlab import find | |
| 13 from matplotlib.pyplot import imread, imsave, imshow, figure, subplot | |
| 5 | 14 |
| 6 try: | 15 try: |
| 7 import cv2 | 16 import cv2 |
| 8 opencvAvailable = True | 17 opencvAvailable = True |
| 9 except ImportError: | 18 except ImportError: |
| 14 skimageAvailable = True | 23 skimageAvailable = True |
| 15 except ImportError: | 24 except ImportError: |
| 16 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') | 25 print('Scikit-image library could not be loaded (HoG-based classification methods will not be available)') |
| 17 skimageAvailable = False | 26 skimageAvailable = False |
| 18 | 27 |
| 19 from sys import stdout | 28 from trafficintelligence import utils, moving |
| 20 from os import listdir | |
| 21 from subprocess import run | |
| 22 from math import floor, log10, ceil | |
| 23 | |
| 24 from numpy import dot, array, append, float32, loadtxt, savetxt, append, zeros, ones, identity, abs as npabs, logical_and, unravel_index, sum as npsum, isnan, mgrid, median, floor as npfloor, ceil as npceil | |
| 25 from numpy.linalg import inv | |
| 26 from matplotlib.mlab import find | |
| 27 from matplotlib.pyplot import imread, imsave | |
| 28 | 29 |
| 29 videoFilenameExtensions = ['mov', 'avi', 'mp4', 'MOV', 'AVI', 'MP4'] | 30 videoFilenameExtensions = ['mov', 'avi', 'mp4', 'MOV', 'AVI', 'MP4'] |
| 30 trackerExe = 'feature-based-tracking' | 31 trackerExe = 'feature-based-tracking' |
| 31 #importaggdraw # agg on top of PIL (antialiased drawing) | 32 #importaggdraw # agg on top of PIL (antialiased drawing) |
| 32 | 33 |
| 101 for i in range(0, last): | 102 for i in range(0, last): |
| 102 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color, **kwargs) | 103 cv2.line(img, positions[i].asint().astuple(), positions[i+1].asint().astuple(), color, **kwargs) |
| 103 | 104 |
| 104 def cvImshow(windowName, img, rescale = 1.0): | 105 def cvImshow(windowName, img, rescale = 1.0): |
| 105 'Rescales the image (in particular if too large)' | 106 'Rescales the image (in particular if too large)' |
| 106 from cv2 import resize | |
| 107 if rescale != 1.: | 107 if rescale != 1.: |
| 108 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) | 108 size = (int(round(img.shape[1]*rescale)), int(round(img.shape[0]*rescale))) |
| 109 resizedImg = resize(img, size) | 109 resizedImg = cv2.resize(img, size) |
| 110 cv2.imshow(windowName, resizedImg) | 110 cv2.imshow(windowName, resizedImg) |
| 111 else: | 111 else: |
| 112 cv2.imshow(windowName, img) | 112 cv2.imshow(windowName, img) |
| 113 | 113 |
| 114 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): | 114 def computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients): |
| 301 if maskFilename is not None: | 301 if maskFilename is not None: |
| 302 cmd += ['--mask-filename', maskFilename] | 302 cmd += ['--mask-filename', maskFilename] |
| 303 if undistort: | 303 if undistort: |
| 304 cmd += ['--undistort', 'true'] | 304 cmd += ['--undistort', 'true'] |
| 305 if intrinsicCameraMatrix is not None: # we currently have to save a file | 305 if intrinsicCameraMatrix is not None: # we currently have to save a file |
| 306 from time import time | |
| 307 intrinsicCameraFilename = '/tmp/intrinsic-{}.txt'.format(time()) | 306 intrinsicCameraFilename = '/tmp/intrinsic-{}.txt'.format(time()) |
| 308 savetxt(intrinsicCameraFilename, intrinsicCameraMatrix) | 307 savetxt(intrinsicCameraFilename, intrinsicCameraMatrix) |
| 309 cmd += ['--intrinsic-camera-filename', intrinsicCameraFilename] | 308 cmd += ['--intrinsic-camera-filename', intrinsicCameraFilename] |
| 310 if distortionCoefficients is not None: | 309 if distortionCoefficients is not None: |
| 311 cmd += ['--distortion-coefficients '+' '.join([str(x) for x in distortionCoefficients])] | 310 cmd += ['--distortion-coefficients '+' '.join([str(x) for x in distortionCoefficients])] |
| 626 def HOG(image, rescaleSize = (64, 64), orientations = 9, pixelsPerCell = (8,8), cellsPerBlock = (2,2), blockNorm = 'L1', visualize = False, transformSqrt = False): | 625 def HOG(image, rescaleSize = (64, 64), orientations = 9, pixelsPerCell = (8,8), cellsPerBlock = (2,2), blockNorm = 'L1', visualize = False, transformSqrt = False): |
| 627 bwImg = color.rgb2gray(image) | 626 bwImg = color.rgb2gray(image) |
| 628 inputImg = transform.resize(bwImg, rescaleSize) | 627 inputImg = transform.resize(bwImg, rescaleSize) |
| 629 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt, True) | 628 features = hog(inputImg, orientations, pixelsPerCell, cellsPerBlock, blockNorm, visualize, transformSqrt, True) |
| 630 if visualize: | 629 if visualize: |
| 631 from matplotlib.pyplot import imshow, figure, subplot | |
| 632 hogViz = features[1] | 630 hogViz = features[1] |
| 633 features = features[0] | 631 features = features[0] |
| 634 figure() | 632 figure() |
| 635 subplot(1,2,1) | 633 subplot(1,2,1) |
| 636 imshow(inputImg) | 634 imshow(inputImg) |
