Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在OpenCV窗口中覆盖Dlib中的面部关键点_Python_Python 2.7_Opencv_Numpy_Dlib - Fatal编程技术网

Python 如何在OpenCV窗口中覆盖Dlib中的面部关键点

Python 如何在OpenCV窗口中覆盖Dlib中的面部关键点,python,python-2.7,opencv,numpy,dlib,Python,Python 2.7,Opencv,Numpy,Dlib,我正在与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

我正在与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)
结果:

现在,我需要将它们叠加到图像中,这就是我遇到问题的地方。我从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()
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)  
当我试图显示它时,它只会给我一个小小的灰色窗口,里面什么都没有:

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)  
有人能告诉我我做错了什么吗?我需要在图像上显示点,比如

编辑:我的其余代码:

win.add_overlay(dets)
iwl = annotate_landmarks(img, lms)
cv2.imshow("Landmarks", iwl)
dlib.hit_enter_to_continue()
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)范围内)

以下是我对叠加所做的操作,并将其保存到png文件中

def get_landmarks(im):
    rects = detector(im, 1)
    if len(rects) > 1:
            raise TooManyFaces
    if len(rects) == 0:
            raise NoFaces
    return np.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()])

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


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

win.clear_overlay()
win.set_image(img)

# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
    print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        k, d.left(), d.top(), d.right(), d.bottom()))
    # Get the landmarks/parts for the face in box d.
    shape = predictor(img, d)
    print("Part 0: {}, Part 1: {} ...".format(shape.part(0),
                                              shape.part(1)))
    # Draw the face landmarks on the screen.
    lms = get_landmarks(img)
    iw1 = annotate_landmarks(img, lms)
    print ("Keypoints:" + (str(lms)))
    cv2.imwrite("1.png", iw1)
    win.add_overlay(shape)

win.add_overlay(dets)
#cv2.imwrite(dets, '1.png')
dlib.hit_enter_to_continue()

您能否附加当前正在计算机上运行的最后一段代码。cv2.imshow('image',img)-看起来您需要显示imB而不是img@Evgeniy这给了我一扇空白的灰色窗户,里面有一个红色的正方形。