diff python/cvutils.py @ 998:933670761a57

updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sun, 27 May 2018 23:22:48 -0400
parents e8eabef7857c
children 75af46516b2b
line wrap: on
line diff
--- a/python/cvutils.py	Fri May 25 18:15:18 2018 -0400
+++ b/python/cvutils.py	Sun May 27 23:22:48 2018 -0400
@@ -57,8 +57,8 @@
 
 def int2FOURCC(x):
     fourcc = ''
-    for i in xrange(4):
-        fourcc += unichr((x >> 8*i)&255)
+    for i in range(4):
+        fourcc += chr((x >> 8*i)&255)
     return fourcc
 
 def rgb2gray(rgb):
@@ -79,8 +79,8 @@
     '''Converts an OpenCV CvMat to numpy array.'''
     print('Deprecated, use new interface')
     a = zeros((cvmat.rows, cvmat.cols))#array([[0.0]*cvmat.width]*cvmat.height)
-    for i in xrange(cvmat.rows):
-        for j in xrange(cvmat.cols):
+    for i in range(cvmat.rows):
+        for j in range(cvmat.cols):
             a[i,j] = cvmat[i,j]
     return a
 
@@ -127,7 +127,7 @@
             print('Empty filename list')
             return
         if windowNames is None:
-            windowNames = ['frame{}'.format(i) for i in xrange(len(filenames))]
+            windowNames = ['frame{}'.format(i) for i in range(len(filenames))]
         wait = 5
         if rescale == 1.:
             for windowName in windowNames:
@@ -142,8 +142,8 @@
             ret = True
             nFramesShown = 0
             if firstFrameNums is not None:
-                for i in xrange(len(captures)):
-                    captures[i].set(cv2.PROP_POS_FRAMES, firstFrameNums[i])
+                for i in range(len(captures)):
+                    captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i])
             while ret and not quitKey(key):
                 rets = []
                 images = []
@@ -155,7 +155,7 @@
                 if ret:
                     if printFrames:
                         print('frame shown {0}'.format(nFramesShown))
-                    for i in xrange(len(filenames)):
+                    for i in range(len(filenames)):
                         if text is not None:
                             cv2.putText(images[i], text, (10,50), cv2.FONT_HERSHEY_PLAIN, 1, cvRed[colorType])
                         cvImshow(windowNames[i], images[i], rescale) # cv2.imshow('frame', img)
@@ -164,7 +164,7 @@
                         cv2.imwrite('image-{}.png'.format(frameNum), img)
                     nFramesShown += step
                     if step > 1:
-                        for i in xrange(len(captures)):
+                        for i in range(len(captures)):
                             captures[i].set(cv2.CAP_PROP_POS_FRAMES, firstFrameNums[i]+nFramesShown)
             cv2.destroyAllWindows()
         else:
@@ -331,7 +331,7 @@
                                 if undistort:
                                     obj.projectedPositions = obj.projectedPositions.newCameraProject(newCameraMatrix)
                             cvPlot(img, obj.projectedPositions, cvColors[colorType][obj.getNum()], frameNum-obj.getFirstInstant())
-                            if frameNum not in boundingBoxes.keys() and obj.hasFeatures():
+                            if frameNum not in boundingBoxes and obj.hasFeatures():
                                 yCropMin, yCropMax, xCropMin, xCropMax = imageBoxSize(obj, frameNum, homography, width, height)
                                 cv2.rectangle(img, (xCropMin, yCropMin), (xCropMax, yCropMax), cvBlue[colorType], 1)
                             objDescription = '{} '.format(obj.num)
@@ -344,7 +344,7 @@
                         if obj.getLastInstant() == frameNum:
                             objects.remove(obj)
                     # plot object bounding boxes
-                    if frameNum in boundingBoxes.keys():
+                    if frameNum in boundingBoxes:
                         for rect in boundingBoxes[frameNum]:
                             cv2.rectangle(img, rect[0].asint().astuple(), rect[1].asint().astuple(), cvColors[colorType][obj.getNum()])
                     # plot ground truth
@@ -506,15 +506,6 @@
         [map1, map2] = computeUndistortMaps(width, height, undistortedImageMultiplication, intrinsicCameraMatrix, distortionCoefficients)
         return cv2.remap(img, map1, map2, interpolation=interpolation)
 
-
-def printCvMat(cvmat, out = stdout):
-    '''Prints the cvmat to out'''
-    print('Deprecated, use new interface')
-    for i in xrange(cvmat.rows):
-        for j in xrange(cvmat.cols):
-            out.write('{0} '.format(cvmat[i,j]))
-        out.write('\n')
-
 def homographyProject(points, homography, output3D = False):
     '''Returns the coordinates of the points (2xN array) projected through homography'''
     if points.shape[0] != 2: