设计思路

输出结果




代码实现
import cv2import dlibimport numpy as npimport sysPREDICTOR_PATH = "E:\\pyOpencv\\shape_predictor_68_face_landmarks.dat"detector = dlib.get_frontal_face_detector()predictor = dlib.shape_predictor(PREDICTOR_PATH)class NoFaces(Exception):passim = cv2.imread("E:\\pyOpencv\\3.jpg")rects = detector(im, 1)if len(rects) >= 1:print("{} faces detected".format(len(rects)))if len(rects) == 0:raise NoFacesfor i in range(len(rects)):landmarks = np.matrix([[p.x, p.y] for p in predictor(im, rects[i]).parts()])im = im.copy()for idx, point in enumerate(landmarks):pos = (point[0, 0], point[0, 1])cv2.circle(im, pos, 1, (0, 255, 0))cv2.namedWindow("im", 2)cv2.imshow("im", im)cv2.imwrite('E:\\pyOpencv\\im.jpg',im)cv2.waitKey(0)
视频人脸实时标记

视频实时监测代码
import cv2import dlibcap = cv2.VideoCapture(0)hog_face_detector = dlib.get_frontal_face_detector()dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")while True:_, frame = cap.read()gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)faces = hog_face_detector(gray)for face in faces:face_landmarks = dlib_facelandmark(gray, face)for n in range(0, 68):x = face_landmarks.part(n).xy = face_landmarks.part(n).ycv2.circle(frame, (x, y), 1, (0, 255, 255), 1)cv2.imshow("Face Landmarks", frame)key = cv2.waitKey(1)if key == 27:breakcap.release()cv2.destroyAllWindows()
模型下载
下载:https://www.lanzoui.com/idOTTiglfuf 密码:esy9


本文介绍了如何使用OpenCV和Dlib库进行视频中人脸的实时检测、定位并标记关键点。通过捕获摄像头输入,利用dlib的frontal_face_detector和shape_predictor进行人脸检测和特征点预测,展示了代码实现和关键步骤。

被折叠的 条评论
为什么被折叠?



