如何在opencv中将一个由点组成的矩阵叠加到图像上?

2024-09-29 00:16:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我用dlib和numpy提取面部关键点列表。在

代码:

def get_landmarks(im):
    rects = detector(im, 1)

    if len(rects) > 1:
        raise TooManyFaces
    if len(rects) == 0:
        raise NoFaces

    return numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])


for f in glob.glob(os.path.join(faces_folder_path, "*")):
    print("Processing file: {}".format(f))
    img = io.imread(f)

    win.clear_overlay()
    win.set_image(img)

    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        lms = get_landmarks(img)
        print ("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom()))
        print ("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
        newSection()
        print ("Keypoints:" + (str(lms)))
        # Draw the face landmarks on the screen.
        win.add_overlay(shape)




    win.add_overlay(dets)
    dlib.hit_enter_to_continue()

结果: 正如你所见,它工作得很好。然而,我需要的不是蓝色的对齐线,而是在关键点和图像之间有一个编号的关联,比如this。如何使用图像点覆盖numpy矩阵:

numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])

在opencv图像上?我自己尝试过:

^{pr2}$

但它只给了我一个空白的长着红色盒子的盒子。在


Tags: theinnumpyformatimgforlenwin