HOG
import cv2
import dlib
cap = cv2.VideoCapture(0)
while(True):
ret,img = cap.read()
hog_face_detector = dlib.get_frontal_face_detector()
detections = hog_face_detector(img,1)
for face in detections:
x = face.left()
y = face.top()
r = face.right()
b = face.bottom()
cv2.rectangle(img,(x,y),(r,b),(255,255,0),2)
cv2.imshow("frame",img)
if cv2.waitKey(1)&0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
CNN
import cv2
import dlib
cap = cv2.VideoCapture(0)
while(True):
ret,img = cap.read()
face_detector = dlib.cnn_face_detection_model_v1("./weights/mmod_human_face_detector.dat")
detections = face_detector(img,1)
for face in detections:
x = face.rect.left()
y = face.rect.top()
r = face.rect.right()
b = face.rect.bottom()
cv2.rectangle(img,(x,y),(r,b),(255),2)
cv2.imshow("frame",img)
if cv2.waitKey(1)&0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
人脸关键点检测
import cv2
import dlib
cap = cv2.VideoCapture(0)
while(True):
ret,img = cap.read()
hog_face_detector = dlib.get_frontal_face_detector()
face_detector = dlib.shape_predictor("./weights/shape_predictor_68_face_landmarks.dat")
detections = hog_face_detector(img,1)
for face in detections:
x = face.left()
y = face.top()
r = face.right()
b = face.bottom()
points68 = face_detector(img,face)
for point in points68.parts():
cv2.circle(img,(point.x,point.y),2,(0,255,0),1)
cv2.rectangle(img,(x,y),(r,b),(255),2)
cv2.imshow("frame",img)
if cv2.waitKey(1)&0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()