2016-06-06 4 views
2

Ich arbeite an einem Projekt zur Gesichtserkennung mit DLIB zu überlagern, und schaffte es mir vor kurzem die Liste des Gesicht keypoints zusätzlich zu dem erzeugten Bild zurückzukehren:Wie Gesichts-keypoints von DLIB in einem OpenCV Fenster

Relevante Code:

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) 

Ergebnis:

enter image description here Nun, ich brauche sich in das Bild zu überlagern, und das ist, wo ich in ein Problem lief. Der Code Overlay bekam ich von einem Matthew Earl auf GitHub:

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 

integriert nicht richtig mit dem Rest meines Codes:

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

Und wenn ich versuche, es zu zeigen, es gibt mir nur ein kleines graues fenster mit nichts drin:

Kann mir jemand zeigen, was ich hier falsch mache? Ich brauche die Punkte auf das Bild anzuzeigen, wie this

bearbeiten: Der Rest meines Code:

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) 
+0

können Sie th an Das letzte Stück Code, das Sie gerade auf Ihrem Computer ausführen. – ZdaR

+0

cv2.imshow ('image', img) - sieht so aus, als müssten Sie imB anstelle von img – Evgeniy

+0

@Evgeniy anzeigen, dass hat mir nur ein leeres graues Fenster mit einem roten Quadrat gegeben. – Rich

Antwort

0

Hier ist, was ich gefunden habe:

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) 

hier im " Form "Objekt Sie hava alle Punkte, die Sie mit shape.part(i) (i in Reichweite (68))