示例#1
0
    def show(self, out):
        if not self.wi:
            return
        wi = self.wi
        t, depthimage, colorimage, shape = out
        wi.set_image(colorimage)
        wi.clear_overlay()
        self.framecount += 1
        if shape is not None:

            def topoint(dp):
                return dlib.point(dp.x, dp.y)

            if True:
                fobp = dlib.full_object_detection(
                    shape.rect,
                    [topoint(shape.part(i)) for i in range(shape.num_parts)])
                #for jj in range(fobp.num_parts):
                if False:
                    p = fobp.part(jj)
                    if jj > self.framecount % 68:
                        break
                    self.wi.add_overlay_circle(dlib.dpoint(p.x, p.y),
                                               2,
                                               color=dlib.rgb_pixel(
                                                   255, 255, 0))
                wi.add_overlay(fobp)
示例#2
0
 def _normalizePoint(p: dlib.dpoint) -> dlib.dpoint:
     smallestX = points[LANDMARK_NUM["TEMPLE_LEFT"]].x
     biggestX  = points[LANDMARK_NUM["TEMPLE_RIGHT"]].x
     smallestY = points[LANDMARK_NUM["CHIN_CENTER"]].y
     biggestY  = points[LANDMARK_NUM["EYEBROW_LEFT_TOP"]].y
     return dlib.dpoint(_normalize(smallestX, biggestX, p.x)
                       , _normalize(smallestY, biggestY, p.y)
                        )
def test_getBiggestFace():
    emptyPoints = dlib.dpoints(194)

    biggest = dlib.dpoints(40)
    biggest.append(dlib.dpoint(100, 100))
    biggest.resize(194 + 1)
    faces = ([emptyPoints] * 5) + [biggest]

    assert _getBiggestFace(faces) == biggest
示例#4
0
def _points2dpoints(ps: dlib.points) -> dlib.dpoints:
    """convert dlib.points object to dlib.dpoints object.
        All points() are should be converted to dpoints,
        as we use float values
    """
    ret = dlib.dpoints()
    for p in ps:
        ret.append(dlib.dpoint(float(p.x), float(p.y)))

    return ret
def test_faceCalibration():
    correct: RawFaceData = RawFaceData(113, 366.82966074187624,
                                       dlib.dpoint(0, 0))
    cap = MockedCap(True, faceFrame)
    with mock.patch('FaceDataServer.faceDetection.input', return_value=None):
        result: RawFaceData = faceCalibration(cap)

    assert result.eyeDistance == correct.eyeDistance
    assert result.faceHeigh == correct.faceHeigh
    assert result.faceCenter.x == correct.faceCenter.x
    assert result.faceCenter.y == correct.faceCenter.y
def test_normalization():
    # inList {{{
    inList = [
        0, 1, 10, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 11, 96, 97, 98, 99,
        114, 115, 116, 117, 118, 119, 12, 120, 121, 122, 123, 124, 125, 126,
        127, 128, 129, 13, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
        14, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 15, 150, 151,
        152, 153, 154, 155, 156, 157, 158, 159, 16, 160, 161, 162, 163, 164,
        165, 166, 167, 168, 169, 17, 170, 171, 172, 173, 174, 175, 176, 177,
        178, 179, 18, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 19,
        190, 191, 192, 193, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30,
        31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47,
        48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63,
        64, 65, 66, 67, 68, 69, 7, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 8,
        80, 81, 82, 83, 84, 85, 100, 101, 102, 103, 9, 104, 105, 106, 107, 108,
        109, 110, 111, 112, 113
    ]
    # }}}

    testPoints = dlib.dpoints(list(map(lambda n: dlib.dpoint(n, n), inList)))
    correct = dlib.dpoints(
        list(map(lambda n: dlib.dpoint(n, n), list(range(0, 194)))))
    assert _normalization(testPoints) == correct
def test_facemark():
    # definition of correct_points {{{
    correct_points = [
        (-301, -48), (-299, -31), (-295, -14), (-291, 4),
        (-286, 21), (-280, 37), (-274, 54), (-267, 70), (-258, 85), (-249, 99),
        (-237, 113), (-224, 125), (-209, 135), (-194, 144), (-178, 151),
        (-162, 158), (-145, 165), (-129, 172), (-112, 178), (-95, 183),
        (-78, 188), (-60, 191), (-42, 192), (-24, 191), (-7, 187), (8, 180),
        (21, 169), (32, 155), (40, 140), (47, 124), (54, 107), (61, 90),
        (67, 73), (72, 56), (77, 39), (81, 22), (85, 5), (88, -13), (90, -30),
        (91, -48), (90, -66), (-39, -66), (-46, -55), (-52, -42), (-56, -28),
        (-53, -14), (-42, -6), (-28, -2), (-15, -1), (0, 0), (14, 1), (28, -1),
        (40, -7), (50, -17), (53, -31), (50, -45), (45, -58), (38, -71),
        (-91, 67), (-82, 62), (-71, 58), (-61, 55), (-51, 51), (-40, 47),
        (-30, 44), (-19, 42), (-9, 41), (3, 40), (14, 39), (25, 38), (36, 40),
        (43, 47), (44, 57), (40, 67), (35, 76), (26, 83), (16, 88), (5, 90),
        (-6, 91), (-18, 90), (-28, 88), (-39, 86), (-50, 83), (-60, 80),
        (-71, 77), (-81, 73), (41, 55), (34, 58), (25, 60), (16, 61), (8, 63),
        (-1, 64), (-10, 65), (-20, 65), (-29, 65), (-38, 65), (-47, 65),
        (-56, 65), (-65, 66), (-75, 66), (-84, 65), (-75, 65), (-66, 64),
        (-57, 64), (-48, 63), (-39, 61), (-30, 61), (-21, 61), (-12, 61),
        (-3, 60), (5, 59), (14, 57), (23, 55),
        (32, 54), (20, -136), (23, -142), (28, -147), (33, -151), (39, -154),
        (45, -157), (52, -158), (59, -158), (66, -157), (72, -153), (76, -148),
        (76, -141), (73, -136), (67, -133), (60, -131), (54, -130), (46, -130),
        (39, -131), (33, -133), (26, -134), (-87, -130), (-93, -136),
        (-101, -141), (-109, -144), (-118, -146), (-127, -145), (-136, -144),
        (-144, -141), (-152, -137), (-159, -133), (-167, -127), (-161, -123),
        (-153, -121), (-144, -120), (-135, -119), (-126, -120), (-117, -121),
        (-108, -123), (-99, -124), (-90, -124), (19, -171), (21, -180),
        (27, -188), (36, -193), (45, -196), (54, -199), (64, -201), (73, -201),
        (82, -199), (89, -194), (94, -186), (97, -175), (89, -174), (81, -177),
        (72, -178), (64, -178), (54, -177), (45, -174), (36, -171), (27, -168),
        (-49, -179), (-63, -189), (-79, -194), (-97, -196), (-115, -197),
        (-133, -197), (-151, -195), (-167, -189), (-183, -181), (-197, -171),
        (-209, -159), (-197, -156), (-180, -161), (-163, -166), (-146, -169),
        (-127, -171), (-109, -171), (-91, -170), (-74, -167), (-55, -166)
    ]
    # }}}

    correct = dlib.dpoints()
    for p in correct_points:
        correct.append(dlib.dpoint(p[0], p[1]))

    assert facemark(faceFrame) == correct
示例#8
0
def test_Face_fromDPoints():
    points = dlib.dpoints([dlib.dpoint(x, x) for x in range(194)])
    # Those values are defined in LANDMARK_NUM
    correct = Face(
        AbsoluteCoord(49.0, 49.0), RelativeCoord(0.0, 0.0),
        RelativeCoord(40.0, 40.0), RelativeCoord(19.0, 19.0),
        Eye(Coord(129.0, 129.0), Coord(120.0, 120.0), Coord(124.0, 124.0),
            Coord(114.0, 114.0)),
        Eye(Coord(149.0, 149.0), Coord(140.0, 140.0), Coord(135.0, 135.0),
            Coord(145.0, 145.0)),
        Mouth(Coord(79.0, 79.0), Coord(65.0, 65.0), Coord(71.0, 71.0),
              Coord(58.0, 58.0)),
        Nose(Coord(49.0, 49.0), Coord(54.0, 54.0), Coord(44.0, 44.0)),
        EyeBrow(Coord(169.0, 169.0), Coord(159.0, 159.0), Coord(164.0, 164.0),
                Coord(154.0, 154.0)),
        EyeBrow(Coord(190.0, 190.0), Coord(179.0, 179.0), Coord(174.0, 174.0),
                Coord(185.0, 185.0)))
    assert Face.fromDPoints(points) == correct
示例#9
0
def test_RawFaceData_thresholded_affect():
    target = RawFaceData(1, 1, dlib.dpoint(0, 0))
    assert target.thresholded(RawFaceData(0, 0, dlib.dpoint(1, 1))) == \
            RawFaceData(0, 0, target.faceCenter)
示例#10
0
def test_RawFaceData_thresholded_noaffect(eD, fH, fC):
    target = RawFaceData(1, 1, dlib.dpoint(0, 0))
    assert target.thresholded(RawFaceData(eD, fH, fC)) == target
示例#11
0
 def _2P(t: Tuple[float, float]):
     return dlib.dpoint(t[0], t[1])
示例#12
0
    assert Face.fromDPoints(points) == correct


# is this good test?
@given(FaceStrategies, finiteFloatCallable)
def test_Face_mul_and_div(f, d):
    assume(d != 0.0)
    assume(1 / d * d == 1)
    assert round_Face(f * d / d) == round_Face(f)


# }}}


# RawFaceData {{{
@pytest.mark.parametrize('eD,fH,fC', [(2, 2, dlib.dpoint(0, 0)),
                                      (1, 1, dlib.dpoint(0, 0)),
                                      (1, 1, dlib.dpoint(-1, -1))])
def test_RawFaceData_thresholded_noaffect(eD, fH, fC):
    target = RawFaceData(1, 1, dlib.dpoint(0, 0))
    assert target.thresholded(RawFaceData(eD, fH, fC)) == target


def test_RawFaceData_thresholded_affect():
    target = RawFaceData(1, 1, dlib.dpoint(0, 0))
    assert target.thresholded(RawFaceData(0, 0, dlib.dpoint(1, 1))) == \
            RawFaceData(0, 0, target.faceCenter)


def test_RawFaceData_default():
    assert RawFaceData.default() == \
示例#13
0
 def overlay_circle(self, x, y):
     if not self.wi:
         return
     self.wi.add_overlay_circle(dlib.dpoint(x, y), 3)
 def _pSub(t: Tuple[float, float], c: Tuple[float, float]) -> dlib.dpoint:
     return dlib.dpoint(t[0] - c[0], t[1] - c[1])
 def _mkp(t: Tuple[float, float]) -> dlib.dpoint:
     """ make dlib.dpoint from Tuple"""
     return dlib.dpoint(t[0], t[1])
示例#16
0
def main():
    sg.change_look_and_feel('LightGreen')

    layout = [
        [sg.Text('OpenCV Demo - SSnap', size=(40, 1), justification='center')],
        [sg.Image(filename='', key='image')],
        [
            sg.Button('Pig nose', size=(15, 1), key='nose'),
            sg.Button('Glasses', size=(15, 1), key='glass'),
            sg.Button('Mắt biếc', size=(15, 1), key='beyes'),
            sg.Button('Moustache', size=(15, 1), key='rau')
        ], [sg.Button('Show landmarks', size=(15, 1), key='show landmarks')],
        [sg.Button('Exit', size=(15, 1))]
    ]

    window = sg.Window('OpenCV Demo - SSnap',
                       layout,
                       location=(800, 600),
                       finalize=True)

    cap = cv2.VideoCapture(0)

    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    #cap = cv2.VideoCapture(0)

    flag_nose = False
    flag_glass = False
    flag_rau = False
    flag_landmarks = False
    flag_beyes = False
    dpoint = dlib.dpoint(0, 0)
    old_landmarks = 0
    old_landmarks1 = 0
    flag_landmarks1 = True

    while True:
        start_time = time.time()

        event, _ = window.read(timeout=0, timeout_key='timeout')

        if event == 'Exit' or event is None:
            break
        if event == 'nose':
            flag_nose = not flag_nose
        if event == 'glass':
            flag_glass = not flag_glass
        if event == 'beyes':
            flag_beyes = not flag_beyes
        if event == 'rau':
            flag_rau = not flag_rau
        if event == 'show landmarks':
            flag_landmarks = not flag_landmarks

        ret, frame = cap.read()

        if ret:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            faces = detector(gray)
            ow = 0
            oh = 0
            for face in faces:
                landmarks = predictor(gray, face)
                landmarks = np.array(landmarks.parts())
                ddpoint = landmarks[30]
                if math.hypot(ddpoint.x - dpoint.x, ddpoint.y - dpoint.y) > 10:
                    old_landmarks = landmarks
                    dpoint = ddpoint
                    flag_landmarks1 = True
                else:
                    landmarks = old_landmarks
                    # print("sau khi doi", landmarks[30],old_landmarks[30])
                    flag_landmarks1 = False

                min_x = min_y = sys.maxsize
                max_x = max_y = -sys.maxsize
                for i in range(27):
                    a = landmarks[i]
                    min_x = min(a.x, min_x)
                    min_y = min(a.y, min_y)
                    max_x = max(a.x, max_x)
                    max_y = max(a.y, max_y)
                # cv2.circle(frame, (min_x, min_y), 4, (0, 255, 0), -1)
                # cv2.circle(frame, (max_x, max_y), 4, (0, 255, 0), -1)

                min_x = max(min_x - 50, 0)
                min_y = max(min_y - 50, 0)
                max_x += 50
                max_y += 50
                new_face = frame[min_y:max_y, min_x:max_x]
                nw, nh = new_face.shape[:2]
                dpo = landmarks[26] - landmarks[17]
                angle = math.atan2(0, 1) - math.atan2(dpo.y, dpo.x)
                new_face = rotateImage(new_face, -angle)
                gray1 = cv2.cvtColor(new_face, cv2.COLOR_BGR2GRAY)
                fac = detector(gray1)
                for fa in fac:
                    landmarks = predictor(gray1, fa)
                    landmarks = np.array(landmarks.parts())
                    if flag_landmarks1:
                        old_landmarks1 = landmarks
                    else:
                        landmarks = old_landmarks1
                        # print("sau khi doi",landmarks[30],old_landmarks1[30])
                    if flag_nose:
                        show(landmarks[31].x, landmarks[29].y, landmarks[35].x,
                             landmarks[33].y, ow, oh, img_nose_mask, img_nose,
                             new_face)
                    if flag_glass:
                        show(landmarks[17].x, landmarks[17].y, landmarks[26].x,
                             landmarks[30].y, ow, oh, img_glasses_mask,
                             img_glasses, new_face)
                    if flag_beyes:
                        show(landmarks[17].x, landmarks[17].y, landmarks[26].x,
                             landmarks[30].y, ow, oh, img_beyes_mask,
                             img_beyes, new_face)
                    if flag_rau:
                        show(landmarks[48].x, landmarks[33].y, landmarks[54].x,
                             landmarks[52].y, ow, oh, img_moustache_mask,
                             img_moustache, new_face)
                    if flag_landmarks:
                        for n in range(0, 68):
                            test_pos(landmarks, new_face, n)

                new_face = rotateImage(new_face, angle)
                dnw, dnh = new_face.shape[:2]
                dnw = int((dnw - nw) / 2)
                dnh = int((dnh - nh) / 2)
                new_face = new_face[dnw + 5:dnw + nw - 5, dnh + 5:dnh + nh - 5]
                dnw, dnh = new_face.shape[:2]
                frame[min_y + 5:min_y + min(dnw, nw - 5) + 5,
                      min_x + 5:min_x + min(dnh, nh - 5) + 5] = new_face

        cv2.putText(frame, str(round(1.0 / (time.time() - start_time), 3)),
                    (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
        imgbytes = cv2.imencode('.png', frame)[1].tobytes()
        window['image'].update(data=imgbytes)

        #print("FPS: ", 1.0 / (time.time() - start_time))

    window.close()