Improvements in tests, bugfix in digits_video sample
This commit is contained in:
parent
4e3a6328ba
commit
87fc75c6d7
@ -24,32 +24,22 @@ import numpy as np
|
|||||||
import cv2
|
import cv2
|
||||||
from tst_scene_render import TestSceneRender
|
from tst_scene_render import TestSceneRender
|
||||||
|
|
||||||
def intersectionRate(s1, s2):
|
from tests_common import NewOpenCVTests, intersectionRate
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
class camshift_test(NewOpenCVTests):
|
class camshift_test(NewOpenCVTests):
|
||||||
|
|
||||||
|
framesNum = 300
|
||||||
frame = None
|
frame = None
|
||||||
selection = None
|
selection = None
|
||||||
drag_start = None
|
drag_start = None
|
||||||
show_backproj = False
|
show_backproj = False
|
||||||
track_window = None
|
track_window = None
|
||||||
render = None
|
render = None
|
||||||
|
errors = 0
|
||||||
|
|
||||||
def prepareRender(self):
|
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):
|
def runTracker(self):
|
||||||
|
|
||||||
@ -93,16 +83,18 @@ class camshift_test(NewOpenCVTests):
|
|||||||
|
|
||||||
if self.show_backproj:
|
if self.show_backproj:
|
||||||
vis[:] = prob[...,np.newaxis]
|
vis[:] = prob[...,np.newaxis]
|
||||||
|
|
||||||
trackingRect = np.array(self.track_window)
|
trackingRect = np.array(self.track_window)
|
||||||
trackingRect[2] += trackingRect[0]
|
trackingRect[2] += trackingRect[0]
|
||||||
trackingRect[3] += trackingRect[1]
|
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
|
break
|
||||||
|
|
||||||
|
self.assertLess(float(self.errors) / self.framesNum, 0.4)
|
||||||
|
|
||||||
def test_camshift(self):
|
def test_camshift(self):
|
||||||
self.prepareRender()
|
self.prepareRender()
|
||||||
self.runTracker()
|
self.runTracker()
|
@ -95,7 +95,7 @@ def evaluate_model(model, digits, samples, labels):
|
|||||||
|
|
||||||
confusion = np.zeros((10, 10), np.int32)
|
confusion = np.zeros((10, 10), np.int32)
|
||||||
for i, j in zip(labels, resp):
|
for i, j in zip(labels, resp):
|
||||||
confusion[i, j] += 1
|
confusion[int(i), int(j)] += 1
|
||||||
|
|
||||||
return err, confusion
|
return err, confusion
|
||||||
|
|
||||||
|
@ -10,17 +10,6 @@ from __future__ import print_function
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
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):
|
def detect(img, cascade):
|
||||||
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
|
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
|
||||||
flags=cv2.CASCADE_SCALE_IMAGE)
|
flags=cv2.CASCADE_SCALE_IMAGE)
|
||||||
@ -29,7 +18,7 @@ def detect(img, cascade):
|
|||||||
rects[:,2:] += rects[:,:2]
|
rects[:,2:] += rects[:,:2]
|
||||||
return rects
|
return rects
|
||||||
|
|
||||||
from tests_common import NewOpenCVTests
|
from tests_common import NewOpenCVTests, intersectionRate
|
||||||
|
|
||||||
class facedetect_test(NewOpenCVTests):
|
class facedetect_test(NewOpenCVTests):
|
||||||
|
|
||||||
|
@ -10,9 +10,31 @@ from __future__ import print_function
|
|||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import sys
|
import sys
|
||||||
|
from numpy import pi, sin, cos
|
||||||
|
|
||||||
from tests_common import NewOpenCVTests
|
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):
|
class houghcircles_test(NewOpenCVTests):
|
||||||
|
|
||||||
def test_houghcircles(self):
|
def test_houghcircles(self):
|
||||||
@ -45,13 +67,15 @@ class houghcircles_test(NewOpenCVTests):
|
|||||||
[448.4, 121.3, 9.12],
|
[448.4, 121.3, 9.12],
|
||||||
[384.6, 128.9, 8.62]]
|
[384.6, 128.9, 8.62]]
|
||||||
|
|
||||||
eps = 7
|
|
||||||
matches_counter = 0
|
matches_counter = 0
|
||||||
|
|
||||||
for i in range(len(testCircles)):
|
for i in range(len(testCircles)):
|
||||||
for j in range(len(circles)):
|
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
|
matches_counter += 1
|
||||||
|
|
||||||
self.assertGreater(float(matches_counter) / len(testCircles), .5)
|
self.assertGreater(float(matches_counter) / len(testCircles), .5)
|
||||||
self.assertLess(float(len(circles) - matches_counter) / len(circles), .7)
|
self.assertLess(float(len(circles) - matches_counter) / len(circles), .75)
|
@ -16,18 +16,7 @@ def inside(r, q):
|
|||||||
qx, qy, qw, qh = q
|
qx, qy, qw, qh = q
|
||||||
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
|
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
|
||||||
|
|
||||||
def intersectionRate(s1, s2):
|
from tests_common import NewOpenCVTests, intersectionRate
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
class peopledetect_test(NewOpenCVTests):
|
class peopledetect_test(NewOpenCVTests):
|
||||||
def test_peopledetect(self):
|
def test_peopledetect(self):
|
||||||
|
@ -53,4 +53,15 @@ class NewOpenCVTests(unittest.TestCase):
|
|||||||
|
|
||||||
def assertGreater(self, a, b, msg=None):
|
def assertGreater(self, a, b, msg=None):
|
||||||
if not a > b:
|
if not a > b:
|
||||||
self.fail('%s not greater than %s' % (repr(a), repr(b)))
|
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)))
|
@ -13,9 +13,10 @@ defaultSize = 512
|
|||||||
|
|
||||||
class TestSceneRender():
|
class TestSceneRender():
|
||||||
|
|
||||||
def __init__(self, bgImg = None, **params):
|
def __init__(self, bgImg = None, deformation = False, **params):
|
||||||
self.time = 0.0
|
self.time = 0.0
|
||||||
self.timeStep = 1.0 / 30.0
|
self.timeStep = 1.0 / 30.0
|
||||||
|
self.deformation = deformation
|
||||||
|
|
||||||
if bgImg != None:
|
if bgImg != None:
|
||||||
self.sceneBg = bgImg.copy()
|
self.sceneBg = bgImg.copy()
|
||||||
@ -26,7 +27,7 @@ class TestSceneRender():
|
|||||||
self.h = self.sceneBg.shape[1]
|
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.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
|
self.currentRect = self.initialRect
|
||||||
|
|
||||||
def setInitialRect(self, rect):
|
def setInitialRect(self, rect):
|
||||||
@ -42,6 +43,9 @@ class TestSceneRender():
|
|||||||
img = self.sceneBg.copy()
|
img = self.sceneBg.copy()
|
||||||
|
|
||||||
self.currentRect = self.initialRect + np.int( 30*cos(self.time) + 50*sin(self.time/3))
|
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))
|
cv2.fillConvexPoly(img, self.currentRect, (0, 0, 255))
|
||||||
|
|
||||||
return img
|
return img
|
||||||
|
@ -27,9 +27,12 @@ def main():
|
|||||||
if not os.path.exists(classifier_fn):
|
if not os.path.exists(classifier_fn):
|
||||||
print('"%s" not found, run digits.py first' % classifier_fn)
|
print('"%s" not found, run digits.py first' % classifier_fn)
|
||||||
return
|
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:
|
while True:
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user