Improvements in tests, bugfix in digits_video sample
authorVladislav Sovrasov <vladislav.sovrasov@itseez.com>
Fri, 5 Feb 2016 14:46:52 +0000 (17:46 +0300)
committerVladislav Sovrasov <vladislav.sovrasov@itseez.com>
Thu, 3 Mar 2016 08:06:21 +0000 (11:06 +0300)
modules/python/test/test_camshift.py
modules/python/test/test_digits.py
modules/python/test/test_facedetect.py
modules/python/test/test_houghcircles.py
modules/python/test/test_peopledetect.py
modules/python/test/tests_common.py
modules/python/test/tst_scene_render.py
samples/python/digits_video.py

index 064206e..766e0c4 100644 (file)
@@ -24,32 +24,22 @@ import numpy as np
 import cv2
 from tst_scene_render import TestSceneRender
 
-def intersectionRate(s1, s2):
-
-    x1, y1, x2, y2 = s1
-    s1 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-
-    x1, y1, x2, y2 = s2
-    s2 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-
-    area, intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
-    return 2 * area / (cv2.contourArea(np.array(s1)) + cv2.contourArea(np.array(s2)))
-
-
-from tests_common import NewOpenCVTests
+from tests_common import NewOpenCVTests, intersectionRate
 
 class camshift_test(NewOpenCVTests):
 
+    framesNum = 300
     frame = None
     selection = None
     drag_start = None
     show_backproj = False
     track_window = None
     render = None
+    errors = 0
 
     def prepareRender(self):
 
-        self.render = TestSceneRender(self.get_sample('samples/data/pca_test1.jpg'))
+        self.render = TestSceneRender(self.get_sample('samples/data/pca_test1.jpg'), True)
 
     def runTracker(self):
 
@@ -93,16 +83,18 @@ class camshift_test(NewOpenCVTests):
 
                 if self.show_backproj:
                     vis[:] = prob[...,np.newaxis]
-
             trackingRect = np.array(self.track_window)
             trackingRect[2] += trackingRect[0]
             trackingRect[3] += trackingRect[1]
 
-            self.assertGreater(intersectionRate((self.render.getCurrentRect()), trackingRect), 0.5)
+            if intersectionRate(self.render.getCurrentRect(), trackingRect) < 0.4:
+                self.errors += 1
 
-            if framesCounter > 300:
+            if framesCounter > self.framesNum:
                 break
 
+        self.assertLess(float(self.errors) / self.framesNum, 0.4)
+
     def test_camshift(self):
         self.prepareRender()
         self.runTracker()
\ No newline at end of file
index 2aa32df..2d5c900 100644 (file)
@@ -95,7 +95,7 @@ def evaluate_model(model, digits, samples, labels):
 
     confusion = np.zeros((10, 10), np.int32)
     for i, j in zip(labels, resp):
-        confusion[i, j] += 1
+        confusion[int(i), int(j)] += 1
 
     return err, confusion
 
index fad1cf9..50c108e 100644 (file)
@@ -10,17 +10,6 @@ from __future__ import print_function
 import numpy as np
 import cv2
 
-def intersectionRate(s1, s2):
-
-    x1, y1, x2, y2 = s1
-    s1 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-
-    x1, y1, x2, y2 = s2
-    s2 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-
-    area, intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
-    return 2 * area / (cv2.contourArea(np.array(s1)) + cv2.contourArea(np.array(s2)))
-
 def detect(img, cascade):
     rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
                                      flags=cv2.CASCADE_SCALE_IMAGE)
@@ -29,7 +18,7 @@ def detect(img, cascade):
     rects[:,2:] += rects[:,:2]
     return rects
 
-from tests_common import NewOpenCVTests
+from tests_common import NewOpenCVTests, intersectionRate
 
 class facedetect_test(NewOpenCVTests):
 
index c012d30..318153a 100644 (file)
@@ -10,9 +10,31 @@ from __future__ import print_function
 import cv2
 import numpy as np
 import sys
+from numpy import pi, sin, cos
 
 from tests_common import NewOpenCVTests
 
+def circleApproximation(circle):
+
+    nPoints = 30
+    phi = 0
+    dPhi = 2*pi / nPoints
+    contour = []
+    for i in range(nPoints):
+        contour.append(([circle[0] + circle[2]*cos(i*dPhi),
+            circle[1] + circle[2]*sin(i*dPhi)]))
+
+    return np.array(contour).astype(int)
+
+def convContoursIntersectiponRate(c1, c2):
+
+    s1 = cv2.contourArea(c1)
+    s2 = cv2.contourArea(c2)
+
+    s, _ = cv2.intersectConvexConvex(c1, c2)
+
+    return 2*s/(s1+s2)
+
 class houghcircles_test(NewOpenCVTests):
 
     def test_houghcircles(self):
@@ -45,13 +67,15 @@ class houghcircles_test(NewOpenCVTests):
         [448.4, 121.3, 9.12],
         [384.6, 128.9, 8.62]]
 
-        eps = 7
         matches_counter = 0
 
         for i in range(len(testCircles)):
             for j in range(len(circles)):
-                if cv2.norm(testCircles[i] - circles[j], cv2.NORM_L2) < eps:
+
+                tstCircle = circleApproximation(testCircles[i])
+                circle = circleApproximation(circles[j])
+                if convContoursIntersectiponRate(tstCircle, circle) > 0.6:
                     matches_counter += 1
 
         self.assertGreater(float(matches_counter) / len(testCircles), .5)
-        self.assertLess(float(len(circles) - matches_counter) / len(circles), .7)
\ No newline at end of file
+        self.assertLess(float(len(circles) - matches_counter) / len(circles), .75)
\ No newline at end of file
index 9aed0ed..fb0a9e9 100644 (file)
@@ -16,18 +16,7 @@ def inside(r, q):
     qx, qy, qw, qh = q
     return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
 
-def intersectionRate(s1, s2):
-
-    x1, y1, x2, y2 = s1
-    s1 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-
-    x1, y1, x2, y2 = s2
-    s2 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
-    area, intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
-
-    return 2 * area / (cv2.contourArea(np.array(s1)) + cv2.contourArea(np.array(s2)))
-
-from tests_common import NewOpenCVTests
+from tests_common import NewOpenCVTests, intersectionRate
 
 class peopledetect_test(NewOpenCVTests):
     def test_peopledetect(self):
index c1cc12d..d76346b 100644 (file)
@@ -53,4 +53,15 @@ class NewOpenCVTests(unittest.TestCase):
 
         def assertGreater(self, a, b, msg=None):
             if not a > b:
-                self.fail('%s not greater than %s' % (repr(a), repr(b)))
\ No newline at end of file
+                self.fail('%s not greater than %s' % (repr(a), repr(b)))
+
+def intersectionRate(s1, s2):
+
+    x1, y1, x2, y2 = s1
+    s1 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
+
+    x1, y1, x2, y2 = s2
+    s2 = [[x1, y1], [x2,y1], [x2, y2], [x1, y2] ]
+    #print(np.array(s2))
+    area, intersection = cv2.intersectConvexConvex(np.array(s1), np.array(s2))
+    return 2 * area / (cv2.contourArea(np.array(s1)) + cv2.contourArea(np.array(s2)))
\ No newline at end of file
index e952c42..adda5bf 100644 (file)
@@ -13,9 +13,10 @@ defaultSize = 512
 
 class TestSceneRender():
 
-    def __init__(self, bgImg = None, **params):
+    def __init__(self, bgImg = None, deformation = False, **params):
         self.time = 0.0
         self.timeStep = 1.0 / 30.0
+        self.deformation = deformation
 
         if bgImg != None:
             self.sceneBg = bgImg.copy()
@@ -26,7 +27,7 @@ class TestSceneRender():
         self.h = self.sceneBg.shape[1]
 
         self.initialRect = np.array([ (self.h/2, self.w/2), (self.h/2, self.w/2 + self.w/10),
-         (self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)])
+         (self.h/2 + self.h/10, self.w/2 + self.w/10), (self.h/2 + self.h/10, self.w/2)]).astype(int)
         self.currentRect = self.initialRect
 
     def setInitialRect(self, rect):
@@ -42,6 +43,9 @@ class TestSceneRender():
         img = self.sceneBg.copy()
 
         self.currentRect = self.initialRect + np.int( 30*cos(self.time) + 50*sin(self.time/3))
+        if(self.deformation):
+            self.currentRect[1:3] += np.int(self.h/20*cos(self.time))
+
         cv2.fillConvexPoly(img, self.currentRect, (0, 0, 255))
 
         return img
index 5f57cb8..c85deb6 100755 (executable)
@@ -27,9 +27,12 @@ def main():
     if not os.path.exists(classifier_fn):
         print('"%s" not found, run digits.py first' % classifier_fn)
         return
-    model = SVM()
-    model.load(classifier_fn)
 
+    if True:
+        model = cv2.ml.SVM_load(classifier_fn)
+    else:
+        model = cv2.ml.SVM_create()
+        model.load_(classifier_fn) #Known bug: https://github.com/Itseez/opencv/issues/4969
 
     while True:
         ret, frame = cap.read()