您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

媒体大数据实例入门:opencv、FR、人脸特征点技术


目录

三种算法的实践

1.opencv

2.使用FR(face_recognition)

3.人脸特征点技术


三种算法的实践

1.opencv

准备工具:安装opencv-python

pip install opencv-python

需要包:

'Python路径下/Lib/site-packages/

cv2/data/haarcascade_frontalface_default.xml'

代码如下:


import cv2
def detect(filename):
    face_cascade = cv2.CascadeClassifier(r'C:\Users\Bryce gu\PycharmProjects\pythonProject1\venv\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
	img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
	for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    	cv2.imshow('Person Detected!', img)
    	cv2.waitKey(0)
    	cv2.destroyAllWindows()
if __name__ == '__main__':
    detect(r'C:\Users\Bryce gu\Desktop\test.jpg')

opencv的结果:


2.FR

请读者注意,在这一段中,默认已经安装相应最新版本的opencv和python。

准备工具:dlib和face_recognition


pip install dlib
pip install face_recognition

代码如下:


import face_recognition
import cv2
image = face_recognition.load_image_file(r"C:\Users\Bryce gu\Desktop\test.jpg")
face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)       # The number of faces
org = cv2.imread(r"C:\Users\Bryce gu\Desktop\test.jpg")
img = cv2.imread(r"C:\Users\Bryce gu\Desktop\test.jpg")
cv2.imshow(r"C:\Users\Bryce gu\Desktop\test.jpg",img)  # 原始图片
for i in range(0,face_num2):
    top = face_locations_noCNN[i][0]
    right = face_locations_noCNN[i][1]
    bottom = face_locations_noCNN[i][2]
    left = face_locations_noCNN[i][3]
    start = (left, top)
    end = (right, bottom)
    color = (0,255,255)
    thickness = 2
    cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
# # use CNN
# face_locations_useCNN = face_recognition.face_locations(image,model='cnn')
# model – Which face detection model to use. “hog” is less accurate but faster on CPUs.
# “cnn” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is “hog”.
# print("face_location_useCNN:")
# print(face_locations_useCNN)
# face_num1=len(face_locations_useCNN)
# print(face_num1)       # The number of faces
# for i in range(0,face_num1):
#     top = face_locations_useCNN[i][0]
#     right = face_locations_useCNN[i][1]
#     bottom = face_locations_useCNN[i][2]
#     left = face_locations_useCNN[i][3]
#
#     start = (left, top)
#     end = (right, bottom)
#
#     color = (0,255,255)
#     thickness = 2
#     cv2.rectangle(img, start, end, color, thickness)    # opencv 里面画矩形的函数
# # Show the result
# cv2.imshow("useCNN",img)

FR的运行结果:


3.人脸特征点技术


准备工具:下载包shape_predictor_68_face_landmarks.dat

代码如下:

#coding=utf-8
import cv2
import dlib
path = r"C:\Users\Bryce gu\Desktop\test.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(r"C:\Users\Bryce gu\PycharmProjects\pythonProject1\venv\Lib\site-packages\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()

运行结果:



分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进