Unable to detect facial landmarks using OpenCV2
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
しかし、これらの画像でも機能していません。CV2 detector
次のようなスクリプトを使用してデバッグすると、空のリストが表示されます。

上の画像を使って基準となるランドマークを描きたいだけです。最善の解決策は何でしょうか、私は通り抜けることができますか?たぶん、cv2
&Dlib
に何かが足りないのですが、必要な結果を得ることができません。
また、次のようなStackOverflowオタクからの推奨実装を使用するための信頼スコアも見つけました。dlib
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]))
これは、2行目の上記の画像の最初の画像の信頼スコアの結果です。

そこにいる素晴らしい人たちからより良い研究を得るのを楽しみにしています。ありがとう
回答
まず、dlibから信頼スコアを取得できるかどうかを確認しようと思うかもしれません。信頼度のしきい値が何であるかはわかりませんが、制限を下回る顔が検出された可能性があります。DLIB Gitのレポ、ここでの検出からの信頼を取得する方法の例です。
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]))
または、別の顔検出器、たとえばこのMobileNetSSD顔検出器のようなCNNベースの検出器を検討してください。私はこの特定のモデルを使用していませんが、ここではGoogle TPUベースの顔検出器モデルのような同様のモデルを使用しており、非常に良い結果が得られています。
「shape_predictor_68_face_landmarks.dat」リンクをダウンロードします。ここにリンクの説明を入力
100%動作するコードこれを試してください:
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)