当前位置 : 主页 > 编程语言 > python >

python实现图片,视频人脸识别(dlib版)

来源:互联网 收集:自由互联 发布时间:2021-04-02
图片人脸检测 #coding=utf-8import cv2import dlibpath = "img/meinv.png"img = cv2.imread(path)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#人脸分类器detector = dlib.get_frontal_face_detector()# 获取人脸检测器predictor = dlib.

图片人脸检测

#coding=utf-8

import cv2
import dlib

path = "img/meinv.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
  "C:\\Python36\\Lib\\site-packages\\dlib-data\\shape_predictor_68_face_landmarks.dat"
)

dets = detector(gray, 1)
for face in dets:
  shape = predictor(img, face) # 寻找人脸的68个标定点
  # 遍历所有点,打印出其坐标,并圈出来
  for pt in shape.parts():
    pt_pos = (pt.x, pt.y)
    cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
  cv2.imshow("image", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

视频人脸检测

# coding=utf-8
import cv2
import dlib

detector = dlib.get_frontal_face_detector() #使用默认的人类识别器模型


def discern(img):
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  dets = detector(gray, 1)
  for face in dets:
    left = face.left()
    top = face.top()
    right = face.right()
    bottom = face.bottom()
    cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
    cv2.imshow("image", img)


cap = cv2.VideoCapture(0)
while (1):
  ret, img = cap.read()
  discern(img)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

那么,OpenCV和Dlib的视频识别对比,有两个地方是不同的:

1.Dlib模型识别的准确率和效果要好于OpenCV;

2.Dlib识别的性能要比OpenCV差,使用视频测试的时候Dlib有明显的卡顿,但是OpenCV就好很多,基本看不出来;

以上就是python实现图片,视频人脸识别(dlib版)的详细内容,更多关于python 人脸识别的资料请关注易盾网络其它相关文章!

网友评论