gpt4 book ai didi

python - 如何在 OpenCV 窗口中覆盖来自 Dlib 的面部关键点

转载 作者:太空宇宙 更新时间:2023-11-03 21:52:12 26 4
gpt4 key购买 nike

我正在使用 DLib 进行面部识别项目,最近成功地返回给我面部关键点列表以及形成的图像:

相关代码:

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)

结果:

enter image description here现在,我需要将它们叠加到图像中,这就是我遇到问题的地方。我从 github 上的 Matthew Earl 那里得到的覆盖代码:

def annotate_landmarks(im, landmarks):
im = im.copy()
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
cv2.putText(im, str(idx), pos,
fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
fontScale=0.4,
color=(0, 0, 255))
cv2.circle(im, pos, 3, color=(0, 255, 255))
return im

没有与我的其余代码正确集成:

win.add_overlay(dets)
iwl = annotate_landmarks(img, lms)
cv2.imshow("Landmarks", iwl)
dlib.hit_enter_to_continue()

当我尝试显示它时,它只给我一个灰色的小窗口,里面什么也没有:

imB = im.copy()
for idx, point in enumerate(lms):
pos = (point[0, 0], point[0, 1])
cv2.putText(imB, str(idx), pos,
fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
fontScale=0.4,
color=(0, 0, 255))
cv2.circle(im, pos, 3, color=(0, 255, 255))

WIDTH = 1000
HEIGHT = 1000

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)
cv2.resizeWindow('image', WIDTH, HEIGHT)

有人可以告诉我我在这里做错了什么吗?我需要在图像上显示点,例如 this

编辑:我的其余代码:

import sys
import os
import dlib
import cv2
import glob
import numpy
from skimage import io



predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2]

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()


predictor_path = sys.argv[1]
faces_folder_path = sys.argv[2]

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
def newSection():
def terminal_size():
import fcntl, termios, struct
h, w, hp, wp = struct.unpack('HHHH',
fcntl.ioctl(0, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0)))
return w
ter_int = terminal_size()
print ("\n" + ("_" * (int(ter_int))) + "\n\n")


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)
# iwl = annotate_landmarks(img, lms)
# cv2.imshow("Landmarks", iwl)
dlib.hit_enter_to_continue()

imB = im.copy()
for idx, point in enumerate(lms):
pos = (point[0, 0], point[0, 1])
cv2.putText(imB, str(idx), pos,
fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
fontScale=0.4,
color=(0, 0, 255))
cv2.circle(im, pos, 3, color=(0, 255, 255))

WIDTH = 1000
HEIGHT = 1000

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', imB)
cv2.resizeWindow('image', WIDTH, HEIGHT)

最佳答案

这是我发现的:

sp = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
facerec = dlib.face_recognition_model_v1('dlib_face_recognition_resnet_model_v1.dat')
detector = dlib.get_frontal_face_detector()

img = io.imread('XXXX.jpg')

dets = detector(img, 1)

for k, d in enumerate(dets):
shape = sp(img, d)

在“形状”对象中,您拥有可以访问的所有点shape.part(i)(i 在范围(68)内)

关于python - 如何在 OpenCV 窗口中覆盖来自 Dlib 的面部关键点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com