diff --git a/modules/python/test/test_camshift.py b/modules/python/test/test_camshift.py index 064206edb17d28169c0d2c518bea9168c471d435..766e0c4bb019354651a272dee42800ffd1794f07 100644 --- a/modules/python/test/test_camshift.py +++ b/modules/python/test/test_camshift.py @@ -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 diff --git a/modules/python/test/test_digits.py b/modules/python/test/test_digits.py index 2aa32df4c32d27c36e8ffc53dc322fc121918f34..2d5c900438092b3be0cd95f5a5735071465ec7fc 100644 --- a/modules/python/test/test_digits.py +++ b/modules/python/test/test_digits.py @@ -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 diff --git a/modules/python/test/test_facedetect.py b/modules/python/test/test_facedetect.py index fad1cf97d14c4748545cf8d9bda738d753982a6b..50c108e056e6645d62d3e658429f94d3f53d3f64 100644 --- a/modules/python/test/test_facedetect.py +++ b/modules/python/test/test_facedetect.py @@ -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): diff --git a/modules/python/test/test_houghcircles.py b/modules/python/test/test_houghcircles.py index c012d30b4943d138178f1d4f880cd2b5109122d0..318153ab7e7b104c322958b0acff2a3597970bf2 100644 --- a/modules/python/test/test_houghcircles.py +++ b/modules/python/test/test_houghcircles.py @@ -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 diff --git a/modules/python/test/test_peopledetect.py b/modules/python/test/test_peopledetect.py index 9aed0ed0afb74905d6ab6c1c86108a4d8a5d5c46..fb0a9e9cae91e88a70d9cecd4861ca3b7ee0de87 100644 --- a/modules/python/test/test_peopledetect.py +++ b/modules/python/test/test_peopledetect.py @@ -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): diff --git a/modules/python/test/tests_common.py b/modules/python/test/tests_common.py index c1cc12d6e48eaf2f20edfcc9b9728d9b37295584..d76346bb497a41bd1dc750ebf395e5c76fb231d1 100644 --- a/modules/python/test/tests_common.py +++ b/modules/python/test/tests_common.py @@ -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 diff --git a/modules/python/test/tst_scene_render.py b/modules/python/test/tst_scene_render.py index e952c4236e4575b750d668736000e121e55ec2b6..adda5bf24f46bfccf1a777fd9e18bbf416d18a64 100644 --- a/modules/python/test/tst_scene_render.py +++ b/modules/python/test/tst_scene_render.py @@ -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 diff --git a/samples/python/digits_video.py b/samples/python/digits_video.py index 5f57cb8f91df75b62b730631919e72d87857e5b5..c85deb6d0a63d3a9311f8fa56c75ff71326bd80b 100755 --- a/samples/python/digits_video.py +++ b/samples/python/digits_video.py @@ -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()