Unable to detect facial landmarks using OpenCV2

Jan 22 2021

I have developed a script using dlib and cv2 to draw facial landmarks on images having one face in that image. Here is the scripts;

import cv2
import dlib

img_path = 'landmarks.png'
detector = dlib.get_frontal_face_detector()

shape_predictor = 'shape_predictor_68_face_landmarks.dat'
predictor = dlib.shape_predictor(shape_predictor)


count = 1
ready = True
while ready:
    frame = cv2.imread("demo.jpg")
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = detector(gray)
    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)

        landmarks = predictor(gray, face)

        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y
            cv2.circle(frame, (x, y), 4, (255, 0, 0), -1)

    cv2.imshow("Frame", frame)
    cv2.waitKey(0)
    ready = False

Now, here what makes me crazy. When I try to download any of the images(with or without mask) from google to test it, this script is working fine. Likewise, you can see these results such as,

But when I try over these following images, it does nothing.

I have made a couple of searches over the internet but I haven't found anything that is serving the current purpose.

Even, I have tried the combination of

  • cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
  • eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
  • m_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')

I also have looked into the following useful links out there;

  • Face Bounding Box

  • Detect Face Landmarks in Android (Even not same domain)

  • Landmarks detection

  • OpenCV2 Detect Facial Landmarks

ale też nie działa na tych obrazach. CV2 detectorpokazuje pustą listę, gdy debuguję za pomocą skryptu, takiego jak;

Chcę tylko narysować punkty orientacyjne, korzystając z powyższych obrazów. Jakie byłoby najlepsze możliwe rozwiązanie, przez które mógłbym przejść? Być może brakuje mi czegoś w cv2& Dlib, ale nie mogę uzyskać wymaganych wyników.

Znalazłem również wynik zaufania do dlibkorzystania z zalecanej implementacji od maniaka Stack Overflow, takiego jak;

import dlib

detector = dlib.get_frontal_face_detector()

img = dlib.load_rgb_image('demo.jpg')
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
    print("Detection {}, score: {}, face_type:{}".format(
        d, scores[i], idx[i]))

Oto wynik oceny ufności dla pierwszego obrazu z podanych powyżej obrazów w drugim rzędzie;

Nie mogę się doczekać lepszych badań od któregokolwiek z niesamowitych facetów. Dzięki

Odpowiedzi

j2abro Jan 26 2021 at 09:55

Po pierwsze, może spróbuję sprawdzić, czy możesz uzyskać wyniki zaufania z dlib. Nie jestem pewien, jaki jest próg pewności, ale być może wykrywane są twarze poniżej tego limitu. Z repozytorium dlib Git Repo , oto przykład, jak uzyskać pewność na podstawie wykrycia:

if (len(sys.argv[1:]) > 0):
    img = dlib.load_rgb_image(sys.argv[1])
    dets, scores, idx = detector.run(img, 1, -1)
    for i, d in enumerate(dets):
        print("Detection {}, score: {}, face_type:{}".format(
            d, scores[i], idx[i]))

Alternatywnie, rozważ inny detektor twarzy, na przykład detektor oparty na CNN, taki jak ten wykrywacz twarzy MobileNet SSD . Nie korzystałem z tego konkretnego modelu, ale użyłem podobnych modeli, takich jak model detektora twarzy oparty na Google TPU, z bardzo dobrymi wynikami.

AliAhmad Jan 26 2021 at 13:15

Pobierz link „ shape_predictor_68_face_landmarks.dat ”: wprowadź opis linku tutaj

100% działający kod Wypróbuj ten:

import cv2
import dlib
import numpy as np

img= cv2.imread('Capture 8.PNG')
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

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

for face in faces:
  x1=face.left()
  y1=face.top()
  x2=face.right()
  y2=face.bottom()
  cv2.rectangle(img, (x1,y1), (x2,y2),(0,255,0),3)
  landmarks=predictor(gray, face)
  for n in range(0,68):
    x=landmarks.part(n).x
    y=landmarks.part(n).y
    cv2.circle(img, (x, y), 4, (0, 0, 255), -1)

cv2.imshow(img)