日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python dlib学习(二):人脸特征点标定

發布時間:2025/3/21 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python dlib学习(二):人脸特征点标定 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

上次介紹了人臉檢測的程序(python dlib學習(一):人臉檢測),這次介紹人臉特征點標定。dlib提供了訓練好的模型,可以識別人臉的68個特征點。
下載鏈接:http://pan.baidu.com/s/1i46vPu1。

程序

還是直接上代碼,注釋在程序中。用到了python-opencv、dlib。

# -*- coding: utf-8 -*- import sys import dlib import cv2 import oscurrent_path = os.getcwd() # 獲取當前路徑 predictor_path = current_path + "\\model\\shape_predictor_68_face_landmarks.dat" # shape_predictor_68_face_landmarks.dat是進行人臉標定的模型,它是基于HOG特征的,這里是他所在的路徑 face_directory_path = current_path + "\\faces\\" # 存放人臉圖片的路徑detector = dlib.get_frontal_face_detector() #獲取人臉分類器 predictor = dlib.shape_predictor(predictor_path) # 獲取人臉檢測器# 傳入的命令行參數 for f in sys.argv[1:]:# 圖片路徑,目錄+文件名face_path = face_directory_path + f# opencv 讀取圖片,并顯示img = cv2.imread(f, cv2.IMREAD_COLOR)# 摘自官方文檔:# image is a numpy ndarray containing either an 8bit grayscale or RGB image.# opencv讀入的圖片默認是bgr格式,我們需要將其轉換為rgb格式;都是numpy的ndarray類。b, g, r = cv2.split(img) # 分離三個顏色通道img2 = cv2.merge([r, g, b]) # 融合三個顏色通道生成新圖片dets = detector(img, 1) #使用detector進行人臉檢測 dets為返回的結果print("Number of faces detected: {}".format(len(dets))) # 打印識別到的人臉個數# enumerate是一個Python的內置方法,用于遍歷索引# index是序號;face是dets中取出的dlib.rectangle類的對象,包含了人臉的區域等信息# left()、top()、right()、bottom()都是dlib.rectangle類的方法,對應矩形四條邊的位置for index, face in enumerate(dets):print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))# 這里不需要畫出人臉的框了# left = face.left()# top = face.top()# right = face.right()# bottom = face.bottom()# cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)# cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)# cv2.imshow(f, img)shape = predictor(img, face) # 尋找人臉的68個標定點 # print(shape)# print(shape.num_parts)# 遍歷所有點,打印出其坐標,并用藍色的圈表示出來for index, pt in enumerate(shape.parts()):print('Part {}: {}'.format(index, pt))pt_pos = (pt.x, pt.y)cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)# 在新窗口中顯示cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)cv2.imshow(f, img)# 等待按鍵,隨后退出,銷毀窗口 k = cv2.waitKey(0) cv2.destroyAllWindows()

還有一點補充的:
我的文件夾結構是這樣的:

faces中存放圖片,運行程序時指定名字,會到這個文件夾中讀取圖片。

model文件夾中存放模型。

運行結果

官方例程

最后給出官方例程,也可以參考官方例程。

#!/usr/bin/python # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt # # This example program shows how to find frontal human faces in an image and # estimate their pose. The pose takes the form of 68 landmarks. These are # points on the face such as the corners of the mouth, along the eyebrows, on # the eyes, and so forth. # # This face detector is made using the classic Histogram of Oriented # Gradients (HOG) feature combined with a linear classifier, an image pyramid, # and sliding window detection scheme. The pose estimator was created by # using dlib's implementation of the paper: # One Millisecond Face Alignment with an Ensemble of Regression Trees by # Vahid Kazemi and Josephine Sullivan, CVPR 2014 # and was trained on the iBUG 300-W face landmark dataset. # # Also, note that you can train your own models using dlib's machine learning # tools. See train_shape_predictor.py to see an example. # # You can get the shape_predictor_68_face_landmarks.dat file from: # http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 # # COMPILING/INSTALLING THE DLIB PYTHON INTERFACE # You can install dlib using the command: # pip install dlib # # Alternatively, if you want to compile dlib yourself then go into the dlib # root folder and run: # python setup.py install # or # python setup.py install --yes USE_AVX_INSTRUCTIONS # if you have a CPU that supports AVX instructions, since this makes some # things run faster. # # Compiling dlib should work on any operating system so long as you have # CMake and boost-python installed. On Ubuntu, this can be done easily by # running the command: # sudo apt-get install libboost-python-dev cmake # # Also note that this example requires scikit-image which can be installed # via the command: # pip install scikit-image # Or downloaded from http://scikit-image.org/download.html. import sys import os import dlib import glob from skimage import ioif len(sys.argv) != 3:print("Give the path to the trained shape predictor model as the first ""argument and then the directory containing the facial images.\n""For example, if you are in the python_examples folder then ""execute this program by running:\n"" ./face_landmark_detection.py shape_predictor_68_face_landmarks.dat ../examples/faces\n""You can download a trained facial shape predictor from:\n"" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2")exit()predictor_path = sys.argv[1] faces_folder_path = sys.argv[2]detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(predictor_path) win = dlib.image_window()for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):print("Processing file: {}".format(f))img = io.imread(f)win.clear_overlay()win.set_image(img)# Ask the detector to find the bounding boxes of each face. The 1 in the# second argument indicates that we should upsample the image 1 time. This# will make everything bigger and allow us to detect more faces.dets = detector(img, 1)print("Number of faces detected: {}".format(len(dets)))for k, d in enumerate(dets):print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom()))# Get the landmarks/parts for the face in box d.shape = predictor(img, d)print("Part 0: {}, Part 1: {} ...".format(shape.part(0),shape.part(1)))# Draw the face landmarks on the screen.win.add_overlay(shape)win.add_overlay(dets)dlib.hit_enter_to_continue()

總結

以上是生活随笔為你收集整理的python dlib学习(二):人脸特征点标定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。