Mercurial Hosting > traffic-intelligence
changeset 807:52aa03260f03 opencv3
reversed all code to OpenCV 2.4.13
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 10 Jun 2016 15:26:19 -0400 |
parents | c6f497291fd8 |
children | 082a5c2685f4 429bb43e8278 |
files | python/cvutils.py python/ml.py python/tests/cvutils.txt samples/val-dor-117-111.png scripts/classify-objects.py scripts/train-object-classification.py scripts/undistort-video.py |
diffstat | 7 files changed, 105 insertions(+), 86 deletions(-) [+] |
line wrap: on
line diff
--- a/python/cvutils.py Fri Jun 10 12:29:58 2016 -0400 +++ b/python/cvutils.py Fri Jun 10 15:26:19 2016 -0400 @@ -149,9 +149,9 @@ def playVideo(filename, firstFrameNum = 0, frameRate = -1, interactive = False, printFrames = True, text = None, rescale = 1., step = 1): '''Plays the video''' windowName = 'frame' + wait = 5 if rescale == 1.: cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) - wait = 5 if frameRate > 0: wait = int(round(1000./frameRate)) if interactive: @@ -161,58 +161,58 @@ key = -1 ret = True frameNum = firstFrameNum - capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) while ret and not quitKey(key): - #ret, img = capture.read() - for i in xrange(step): - ret, img = capture.read() + ret, img = capture.read() if ret: if printFrames: print('frame {0}'.format(frameNum)) - frameNum+=step if text is not None: - cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) - cvImshow(windowName, img, rescale) + cv2.putText(img, text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed) + cv2.imshow('frame', img)#cvImshow(windowName, img, rescale) key = cv2.waitKey(wait) if saveKey(key): cv2.imwrite('image-{}.png'.format(frameNum), img) + frameNum += step + if step > 1: + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) cv2.destroyAllWindows() else: print('Video capture for {} failed'.format(filename)) def infoVideo(filename): '''Provides all available info on video ''' - cvPropertyNames = {cv2.CAP_PROP_FORMAT: "format", - cv2.CAP_PROP_FOURCC: "codec (fourcc)", - cv2.CAP_PROP_FPS: "fps", - cv2.CAP_PROP_FRAME_COUNT: "number of frames", - cv2.CAP_PROP_FRAME_HEIGHT: "heigh", - cv2.CAP_PROP_FRAME_WIDTH: "width", - cv2.CAP_PROP_RECTIFICATION: "rectification", - cv2.CAP_PROP_SATURATION: "saturation"} + cvPropertyNames = {cv2.cv.CV_CAP_PROP_FORMAT: "format", + cv2.cv.CV_CAP_PROP_FOURCC: "codec (fourcc)", + cv2.cv.CV_CAP_PROP_FPS: "fps", + cv2.cv.CV_CAP_PROP_FRAME_COUNT: "number of frames", + cv2.cv.CV_CAP_PROP_FRAME_HEIGHT: "heigh", + cv2.cv.CV_CAP_PROP_FRAME_WIDTH: "width", + cv2.cv.CV_CAP_PROP_RECTIFICATION: "rectification", + cv2.cv.CV_CAP_PROP_SATURATION: "saturation"} capture = cv2.VideoCapture(filename) if capture.isOpened(): - for cvprop in [#cv2.CAP_PROP_BRIGHTNESS - #cv2.CAP_PROP_CONTRAST - #cv2.CAP_PROP_CONVERT_RGB - #cv2.CAP_PROP_EXPOSURE - cv2.CAP_PROP_FORMAT, - cv2.CAP_PROP_FOURCC, - cv2.CAP_PROP_FPS, - cv2.CAP_PROP_FRAME_COUNT, - cv2.CAP_PROP_FRAME_HEIGHT, - cv2.CAP_PROP_FRAME_WIDTH, - #cv2.CAP_PROP_GAIN, - #cv2.CAP_PROP_HUE - #cv2.CAP_PROP_MODE - #cv2.CAP_PROP_POS_AVI_RATIO - #cv2.CAP_PROP_POS_FRAMES - #cv2.CAP_PROP_POS_MSEC - #cv2.CAP_PROP_RECTIFICATION, - #cv2.CAP_PROP_SATURATION + for cvprop in [#cv2.cv.CV_CAP_PROP_BRIGHTNESS + #cv2.cv.CV_CAP_PROP_CONTRAST + #cv2.cv.CV_CAP_PROP_CONVERT_RGB + #cv2.cv.CV_CAP_PROP_EXPOSURE + cv2.cv.CV_CAP_PROP_FORMAT, + cv2.cv.CV_CAP_PROP_FOURCC, + cv2.cv.CV_CAP_PROP_FPS, + cv2.cv.CV_CAP_PROP_FRAME_COUNT, + cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, + cv2.cv.CV_CAP_PROP_FRAME_WIDTH, + #cv2.cv.CV_CAP_PROP_GAIN, + #cv2.cv.CV_CAP_PROP_HUE + #cv2.cv.CV_CAP_PROP_MODE + #cv2.cv.CV_CAP_PROP_POS_AVI_RATIO + #cv2.cv.CV_CAP_PROP_POS_FRAMES + #cv2.cv.CV_CAP_PROP_POS_MSEC + #cv2.cv.CV_CAP_PROP_RECTIFICATION, + #cv2.cv.CV_CAP_PROP_SATURATION ]: prop = capture.get(cvprop) - if cvprop == cv2.CAP_PROP_FOURCC and prop > 0: + if cvprop == cv2.cv.CV_CAP_PROP_FOURCC and prop > 0: prop = int2FOURCC(int(prop)) print('Video {}: {}'.format(cvPropertyNames[cvprop], prop)) else: @@ -223,12 +223,12 @@ images = [] capture = cv2.VideoCapture(videoFilename) if capture.isOpened(): - rawCount = capture.get(cv2.CAP_PROP_FRAME_COUNT) + rawCount = capture.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) if rawCount < 0: rawCount = lastFrameNum+1 nDigits = int(floor(log10(rawCount)))+1 ret = False - capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) frameNum = firstFrameNum while frameNum<=lastFrameNum and frameNum<rawCount: ret, img = capture.read() @@ -244,7 +244,7 @@ images.append(img) frameNum +=step if step > 1: - capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) capture.release() else: print('Video capture for {} failed'.format(videoFilename)) @@ -253,7 +253,7 @@ def getFPS(videoFilename): capture = cv2.VideoCapture(videoFilename) if capture.isOpened(): - fps = capture.get(cv2.CAP_PROP_FPS) + fps = capture.get(cv2.cv.CV_CAP_PROP_FPS) capture.release() return fps else: @@ -291,8 +291,8 @@ def displayTrajectories(videoFilename, objects, boundingBoxes = {}, homography = None, firstFrameNum = 0, lastFrameNumArg = None, printFrames = True, rescale = 1., nFramesStep = 1, saveAllImages = False, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., annotations = [], gtMatches = {}, toMatches = {}): '''Displays the objects overlaid frame by frame over the video ''' capture = cv2.VideoCapture(videoFilename) - width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) - height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) + width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) + height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) windowName = 'frame' if rescale == 1.: @@ -304,7 +304,7 @@ key = -1 ret = True frameNum = firstFrameNum - capture.set(cv2.CAP_PROP_POS_FRAMES, firstFrameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, firstFrameNum) if lastFrameNumArg is None: lastFrameNum = maxint else: @@ -364,7 +364,7 @@ cv2.imwrite('image-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) frameNum += nFramesStep if nFramesStep > 1: - capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) cv2.destroyAllWindows() else: print('Cannot load file ' + videoFilename) @@ -607,3 +607,15 @@ return array(inputData, dtype = float32), array([classLabel]*nImages) +######################### +# running tests +######################### + +if __name__ == "__main__": + import doctest + import unittest + suite = doctest.DocFileSuite('tests/cvutils.txt') + #suite = doctest.DocTestSuite() + unittest.TextTestRunner().run(suite) + #doctest.testmod() + #doctest.testfile("example.txt")
--- a/python/ml.py Fri Jun 10 12:29:58 2016 -0400 +++ b/python/ml.py Fri Jun 10 15:26:19 2016 -0400 @@ -20,9 +20,7 @@ ##################### class StatModel(object): - '''Abstract class for loading/saving model - - Issues with OpenCV, does not seem to work''' + '''Abstract class for loading/saving model''' def load(self, filename): if path.exists(filename): self.model.load(filename) @@ -34,25 +32,22 @@ class SVM(StatModel): '''wrapper for OpenCV SimpleVectorMachine algorithm''' - def __init__(self, svmType = cv2.ml.SVM_C_SVC, kernelType = cv2.ml.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): - self.model = cv2.ml.SVM_create() - self.model.setType(svmType) - self.model.setKernel(kernelType) - self.model.setDegree(degree) - self.model.setGamma(gamma) - self.model.setCoef0(coef0) - self.model.setC(Cvalue) - self.model.setNu(nu) - self.model.setP(p) + def __init__(self, svmType = cv2.SVM_C_SVC, kernelType = cv2.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): + self.model = cv2.SVM() + self.params = dict(svm_type = svmType, kernel_type = kernelType, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p) + # OpenCV3 + # self.model = cv2.SVM() + # self.model.setType(svmType) + # self.model.setKernel(kernelType) + # self.model.setDegree(degree) + # self.model.setGamma(gamma) + # self.model.setCoef0(coef0) + # self.model.setC(Cvalue) + # self.model.setNu(nu) + # self.model.setP(p) - def load(self, filename): - if path.exists(filename): - cv2.ml.SVM_load(filename) - else: - print('Provided filename {} does not exist: model not loaded!'.format(filename)) - - def train(self, samples, layout, responses): - self.model.train(samples, layout, responses) + def train(self, samples, responses): + self.model.train(samples, responses, params = self.params) def predict(self, hog): return self.model.predict(hog)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/tests/cvutils.txt Fri Jun 10 15:26:19 2016 -0400 @@ -0,0 +1,10 @@ +>>> import cv2, cvutils +>>> from numpy import array, round +>>> img = cv2.imread("../samples/val-dor-117-111.png") +>>> width = img.shape[1] +>>> height = img.shape[0] +>>> intrinsicCameraMatrix = array([[ 377.42, 0. , 639.12], [ 0. , 378.43, 490.2 ], [ 0. , 0. , 1. ]]) +>>> [map1, map2] = cvutils.computeUndistortMaps(width, height, 1.31, intrinsicCameraMatrix, [-0.11759321, 0.0148536, 0.00030756, -0.00020578, -0.00091816]) +>>> undistorted = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) +>>> (undistorted.shape == array([int(round(height*1.31)), int(round(width*1.31)), 3])).all() +True
--- a/scripts/classify-objects.py Fri Jun 10 12:29:58 2016 -0400 +++ b/scripts/classify-objects.py Fri Jun 10 15:26:19 2016 -0400 @@ -4,7 +4,7 @@ import numpy as np import sys, argparse -from cv2.ml import SVM_RBF, SVM_C_SVC +#from cv2.ml import SVM_RBF, SVM_C_SVC import cv2 from scipy.stats import norm, lognorm @@ -12,8 +12,8 @@ parser = argparse.ArgumentParser(description='The program processes indicators for all pairs of road users in the scene') parser.add_argument('--cfg', dest = 'configFilename', help = 'name of the configuration file', required = True) -parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = SVM_RBF, type = long) -parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = SVM_C_SVC, type = long) +parser.add_argument('--kernel', dest = 'kernelType', help = 'kernel type for the support vector machine (SVM)', default = cv2.SVM_RBF, type = long) +parser.add_argument('--svm', dest = 'svmType', help = 'SVM type', default = cv2.SVM_C_SVC, type = long) parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file (overrides the configuration file)') parser.add_argument('-i', dest = 'videoFilename', help = 'name of the video file (overrides the configuration file)') parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to classify', type = int, default = None) @@ -79,8 +79,8 @@ timeInterval = moving.TimeInterval.unionIntervals(intervals) capture = cv2.VideoCapture(videoFilename) -width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) -height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) +width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) +height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) pastObjects = [] if params.undistort: # setup undistortion @@ -88,7 +88,7 @@ if capture.isOpened(): ret = True frameNum = timeInterval.first - capture.set(cv2.CAP_PROP_POS_FRAMES, frameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, frameNum) lastFrameNum = timeInterval.last while ret and frameNum <= lastFrameNum:
--- a/scripts/train-object-classification.py Fri Jun 10 12:29:58 2016 -0400 +++ b/scripts/train-object-classification.py Fri Jun 10 15:26:19 2016 -0400 @@ -2,7 +2,9 @@ import numpy as np import sys, argparse -from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE +from cv2 import SVM_RBF, SVM_C_SVC +#from cv2.ml import SVM_RBF, SVM_C_SVC, ROW_SAMPLE # row_sample for layout in cv2.ml.SVM_load + import cvutils, moving, ml @@ -48,20 +50,20 @@ # Training the Support Vector Machine print "Training Pedestrian-Cyclist-Vehicle Model" model = ml.SVM(args.svmType, args.kernelType) -model.train(np.concatenate(trainingSamplesPBV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPBV.values())) +model.train(np.concatenate(trainingSamplesPBV.values()), np.concatenate(trainingLabelsPBV.values())) model.save(args.directoryName + "/modelPBV.xml") print "Training Cyclist-Vehicle Model" model = ml.SVM(args.svmType, args.kernelType) -model.train(np.concatenate(trainingSamplesBV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsBV.values())) +model.train(np.concatenate(trainingSamplesBV.values()), np.concatenate(trainingLabelsBV.values())) model.save(args.directoryName + "/modelBV.xml") print "Training Pedestrian-Cyclist Model" model = ml.SVM(args.svmType, args.kernelType) -model.train(np.concatenate(trainingSamplesPB.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPB.values())) +model.train(np.concatenate(trainingSamplesPB.values()), np.concatenate(trainingLabelsPB.values())) model.save(args.directoryName + "/modelPB.xml") print "Training Pedestrian-Vehicle Model" model = ml.SVM(args.svmType, args.kernelType) -model.train(np.concatenate(trainingSamplesPV.values()), ROW_SAMPLE, np.concatenate(trainingLabelsPV.values())) +model.train(np.concatenate(trainingSamplesPV.values()), np.concatenate(trainingLabelsPV.values())) model.save(args.directoryName + "/modelPV.xml")
--- a/scripts/undistort-video.py Fri Jun 10 12:29:58 2016 -0400 +++ b/scripts/undistort-video.py Fri Jun 10 15:26:19 2016 -0400 @@ -16,7 +16,7 @@ parser.add_argument('--intrinsic', dest = 'intrinsicCameraMatrixFilename', help = 'name of the intrinsic camera file') parser.add_argument('--distortion-coefficients', dest = 'distortionCoefficients', help = 'distortion coefficients', nargs = '*', type = float) parser.add_argument('--undistorted-multiplication', dest = 'undistortedImageMultiplication', help = 'undistorted image multiplication', type = float) -parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int) +parser.add_argument('-f', dest = 'firstFrameNum', help = 'number of first frame number to display', type = int, default = 0) parser.add_argument('-l', dest = 'lastFrameNum', help = 'number of last frame number to save', type = int) parser.add_argument('-d', dest = 'destinationDirname', help = 'name of the directory where the undistorted frames are saved') parser.add_argument('--encode', dest = 'encodeVideo', help = 'indicate if video is generated at the end (default Xvid)', action = 'store_true') @@ -40,25 +40,25 @@ mkdir(destinationDirname) capture = cv2.VideoCapture(args.videoFilename) -width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) -height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) +width = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) +height = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) [map1, map2] = cvutils.computeUndistortMaps(width, height, args.undistortedImageMultiplication, intrinsicCameraMatrix, args.distortionCoefficients) if capture.isOpened(): ret = True frameNum = args.firstFrameNum - capture.set(cv2.CAP_PROP_POS_FRAMES, args.firstFrameNum) + capture.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, args.firstFrameNum) if args.lastFrameNum is None: from sys import maxint lastFrameNum = maxint else: lastFrameNum = args.lastFrameNum - nZerosFilename = int(ceil(log10(lastFrameNum))) - while ret and frameNum < lastFrameNum: - ret, img = capture.read() - if ret: - img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) - cv2.imwrite(destinationDirname+'undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) - frameNum += 1 + nZerosFilename = int(ceil(log10(lastFrameNum))) + while ret and frameNum < lastFrameNum: + ret, img = capture.read() + if ret: + img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR) + cv2.imwrite(destinationDirname+'undistorted-{{:0{}}}.png'.format(nZerosFilename).format(frameNum), img) + frameNum += 1 if args.encodeVideo: print('Encoding the images files in video')