def test_peopledetect(self):

        hog = cv2.HOGDescriptor()
        hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

        dirPath = 'samples/gpu/'
        samples = ['basketball1.png', 'basketball2.png']

        testPeople = [[[23, 76, 164, 477], [440, 22, 637, 478]],
                      [[23, 76, 164, 477], [440, 22, 637, 478]]]

        eps = 0.5

        for sample in samples:

            img = self.get_sample(dirPath + sample, 0)

            found, w = hog.detectMultiScale(img,
                                            winStride=(8, 8),
                                            padding=(32, 32),
                                            scale=1.05)
            found_filtered = []
            for ri, r in enumerate(found):
                for qi, q in enumerate(found):
                    if ri != qi and inside(r, q):
                        break
                else:
                    found_filtered.append(r)

            matches = 0

            for i in range(len(found_filtered)):
                for j in range(len(testPeople)):

                    found_rect = (found_filtered[i][0], found_filtered[i][1],
                                  found_filtered[i][0] + found_filtered[i][2],
                                  found_filtered[i][1] + found_filtered[i][3])

                    if intersectionRate(
                            found_rect,
                            testPeople[j][0]) > eps or intersectionRate(
                                found_rect, testPeople[j][1]) > eps:
                        matches += 1

            self.assertGreater(matches, 0)
Beispiel #2
0
    def test_peopledetect(self):

        hog = cv2.HOGDescriptor()
        hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )

        dirPath = 'samples/data/'
        samples = ['basketball1.png', 'basketball2.png']

        testPeople = [
        [[23, 76, 164, 477], [440, 22, 637, 478]],
        [[23, 76, 164, 477], [440, 22, 637, 478]]
        ]

        eps = 0.5

        for sample in samples:

            img = self.get_sample(dirPath + sample, 0)

            found, w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
            found_filtered = []
            for ri, r in enumerate(found):
                for qi, q in enumerate(found):
                    if ri != qi and inside(r, q):
                        break
                else:
                    found_filtered.append(r)

            matches = 0

            for i in range(len(found_filtered)):
                for j in range(len(testPeople)):

                    found_rect = (found_filtered[i][0], found_filtered[i][1],
                        found_filtered[i][0] + found_filtered[i][2],
                        found_filtered[i][1] + found_filtered[i][3])

                    if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:
                        matches += 1

            self.assertGreater(matches, 0)
    def runTracker(self):

        framesCounter = 0
        self.selection = True

        xmin, ymin, xmax, ymax = self.render.getCurrentRect()

        self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)

        while True:
            framesCounter += 1
            self.frame = self.render.getNextFrame()
            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)
            mask = cv2.inRange(hsv, np.array((0., 60., 32.)),
                               np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.render.getCurrentRect() + 50
                x0 -= 100
                y0 -= 100

                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv2.calcHist([hsv_roi], [0], mask_roi, [16], [0, 180])
                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.selection = False

            if self.track_window and self.track_window[
                    2] > 0 and self.track_window[3] > 0:
                self.selection = None
                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                             10, 1)
                track_box, self.track_window = cv2.CamShift(
                    prob, self.track_window, term_crit)

            trackingRect = np.array(self.track_window)
            trackingRect[2] += trackingRect[0]
            trackingRect[3] += trackingRect[1]

            if intersectionRate(self.render.getCurrentRect(),
                                trackingRect) < 0.4:
                self.errors += 1

            if framesCounter > self.framesNum:
                break

        self.assertLess(float(self.errors) / self.framesNum, 0.4)
Beispiel #4
0
    def runTracker(self):

        framesCounter = 0
        self.selection = True

        xmin, ymin, xmax, ymax = self.render.getCurrentRect()

        self.track_window = (xmin, ymin, xmax - xmin, ymax - ymin)

        while True:
            framesCounter += 1
            self.frame = self.render.getNextFrame()
            hsv = cv.cvtColor(self.frame, cv.COLOR_BGR2HSV)
            mask = cv.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.)))

            if self.selection:
                x0, y0, x1, y1 = self.render.getCurrentRect() + 50
                x0 -= 100
                y0 -= 100

                hsv_roi = hsv[y0:y1, x0:x1]
                mask_roi = mask[y0:y1, x0:x1]
                hist = cv.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] )
                cv.normalize(hist, hist, 0, 255, cv.NORM_MINMAX)
                self.hist = hist.reshape(-1)
                self.selection = False

            if self.track_window and self.track_window[2] > 0 and self.track_window[3] > 0:
                self.selection = None
                prob = cv.calcBackProject([hsv], [0], self.hist, [0, 180], 1)
                prob &= mask
                term_crit = ( cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1 )
                _track_box, self.track_window = cv.CamShift(prob, self.track_window, term_crit)

            trackingRect = np.array(self.track_window)
            trackingRect[2] += trackingRect[0]
            trackingRect[3] += trackingRect[1]

            if intersectionRate(self.render.getCurrentRect(), trackingRect) < 0.4:
                self.errors += 1

            if framesCounter > self.framesNum:
                break

        self.assertLess(float(self.errors) / self.framesNum, 0.4)
Beispiel #5
0
    def test_facedetect(self):
        cascade_fn = self.repoPath + '/data/haarcascades/haarcascade_frontalface_alt.xml'
        nested_fn = self.repoPath + '/data/haarcascades/haarcascade_eye.xml'

        cascade = cv.CascadeClassifier(cascade_fn)
        nested = cv.CascadeClassifier(nested_fn)

        samples = [
            'samples/data/lena.jpg', 'cv/cascadeandhog/images/mona-lisa.png'
        ]

        faces = []
        eyes = []

        testFaces = [
            #lena
            [[218, 200, 389, 371], [244, 240, 294, 290], [309, 246, 352, 289]],

            #lisa
            [[167, 119, 307, 259], [188, 153, 229, 194], [236, 153, 277, 194]]
        ]

        for sample in samples:

            img = self.get_sample(sample)
            gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
            gray = cv.GaussianBlur(gray, (5, 5), 5.1)

            rects = detect(gray, cascade)
            faces.append(rects)

            if not nested.empty():
                for x1, y1, x2, y2 in rects:
                    roi = gray[y1:y2, x1:x2]
                    subrects = detect(roi.copy(), nested)

                    for rect in subrects:
                        rect[0] += x1
                        rect[2] += x1
                        rect[1] += y1
                        rect[3] += y1

                    eyes.append(subrects)

        faces_matches = 0
        eyes_matches = 0

        eps = 0.8

        for i in range(len(faces)):
            for j in range(len(testFaces)):
                if intersectionRate(faces[i][0], testFaces[j][0]) > eps:
                    faces_matches += 1
                    #check eyes
                    if len(eyes[i]) == 2:
                        if intersectionRate(
                                eyes[i][0],
                                testFaces[j][1]) > eps and intersectionRate(
                                    eyes[i][1], testFaces[j][2]) > eps:
                            eyes_matches += 1
                        elif intersectionRate(
                                eyes[i][1],
                                testFaces[j][1]) > eps and intersectionRate(
                                    eyes[i][0], testFaces[j][2]) > eps:
                            eyes_matches += 1

        self.assertEqual(faces_matches, 2)
        self.assertEqual(eyes_matches, 2)
Beispiel #6
0
    def test_facedetect(self):
        import sys, getopt

        cascade_fn = self.repoPath + '/data/haarcascades/haarcascade_frontalface_alt.xml'
        nested_fn  = self.repoPath + '/data/haarcascades/haarcascade_eye.xml'

        cascade = cv2.CascadeClassifier(cascade_fn)
        nested = cv2.CascadeClassifier(nested_fn)

        samples = ['samples/data/lena.jpg', 'samples/data/cv/cascadeandhog/images/mona-lisa.png']

        faces = []
        eyes = []

        testFaces = [
        #lena
        [[218, 200, 389, 371],
        [ 244, 240, 294, 290],
        [ 309, 246, 352, 289]],

        #lisa
        [[167, 119, 307, 259],
        [188, 153, 229, 194],
        [236, 153, 277, 194]]
        ]

        for sample in samples:

            img = self.get_sample(  sample)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            gray = cv2.GaussianBlur(gray, (5, 5), 5.1)

            rects = detect(gray, cascade)
            faces.append(rects)

            if not nested.empty():
                for x1, y1, x2, y2 in rects:
                    roi = gray[y1:y2, x1:x2]
                    subrects = detect(roi.copy(), nested)

                    for rect in subrects:
                        rect[0] += x1
                        rect[2] += x1
                        rect[1] += y1
                        rect[3] += y1

                    eyes.append(subrects)

        faces_matches = 0
        eyes_matches = 0

        eps = 0.8

        for i in range(len(faces)):
            for j in range(len(testFaces)):
                if intersectionRate(faces[i][0], testFaces[j][0]) > eps:
                    faces_matches += 1
                    #check eyes
                    if len(eyes[i]) == 2:
                        if intersectionRate(eyes[i][0], testFaces[j][1]) > eps and intersectionRate(eyes[i][1] , testFaces[j][2]) > eps:
                            eyes_matches += 1
                        elif intersectionRate(eyes[i][1], testFaces[j][1]) > eps and intersectionRate(eyes[i][0], testFaces[j][2]) > eps:
                            eyes_matches += 1

        self.assertEqual(faces_matches, 2)
        self.assertEqual(eyes_matches, 2)