5803浏览
查看: 5803|回复: 0

[教程] 【Mind+Python】人脸检测

[复制链接]
本帖最后由 云天 于 2021-8-7 22:57 编辑

python的“face_recognition”包进行人脸检测、用OpenCV在原图像中绘制人脸box框和landmark关键点。
【安装】
【Mind+Python】人脸检测图1

人脸检测face_recognition可以输出一幅图像中人脸框的左上和右下点的坐标
【Mind+Python】人脸检测图2

  1. import cv2
  2. import face_recognition
  3. cap = cv2.VideoCapture(0)
  4. while True:
  5.     ret, frame = cap.read()
  6.     frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
  7.     # Find all the faces in the video
  8.     face_locations = face_recognition.face_locations(frame)
  9.     number_of_faces = len(face_locations)
  10.     print("I found {} face(s) in this video.".format(number_of_faces))
  11.     for face_location in face_locations:
  12.         # Print the location of each face in this image. Each face is a list of co-ordinates in (top, right, bottom, left) order.
  13.         top, right, bottom, left = face_location
  14.         print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom,
  15.                                                                                                     right))
  16.         cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 3)
  17.     cv2.imshow("Frame", frame)
  18.     ch = cv2.waitKey(1)
  19.     if ch & 0xFF == ord('q'):
  20.         break
  21. cap.release()
  22. cv2.destroyAllWindows()
复制代码


landmark关键点face_recognition可输出如下图所示人脸五官的landmark坐标(共68个坐标点)。
【Mind+Python】人脸检测图3
  1. import numpy as np
  2. import cv2
  3. import face_recognition
  4. cap = cv2.VideoCapture(0)
  5. while True:
  6.     ret, frame = cap.read()
  7.     frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
  8.     # Find all facial features in all the faces in the video
  9.     face_landmarks_list = face_recognition.face_landmarks(frame)
  10.     for face_landmarks in face_landmarks_list:
  11.         # Loop over each facial feature (eye, nose, mouth, lips, etc)
  12.         for name, list_of_points in face_landmarks.items():
  13.             hull = np.array(face_landmarks[name])
  14.             hull_landmark = cv2.convexHull(hull)
  15.             cv2.drawContours(frame, hull_landmark, -1, (0, 255, 0), 3)
  16.     cv2.imshow("Frame", frame)
  17.     ch = cv2.waitKey(1)
  18.     if ch & 0xFF == ord('q'):
  19.         break
  20. cap.release()
  21. cv2.destroyAllWindows()
复制代码




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail