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

歡迎訪問 生活随笔!

生活随笔

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

python

python dlib学习(一):人脸检测

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

前言

dlib畢竟是一個很有名的庫了,有c++、Python的接口。使用dlib可以大大簡化開發,比如人臉識別,特征點檢測之類的工作都可以很輕松實現。同時也有很多基于dlib開發的應用和開源庫,比如face_recogintion庫(應用一個基于Python的開源人臉識別庫,face_recognition)等等。

環境安裝

不算復雜,我只在Linux和win下跑過。安裝配置不算難,直接貼鏈接了。
Linux下的安裝在這篇博客中介紹了(應用一個基于Python的開源人臉識別庫,face_recognition),不做贅述。
win下安裝教程:
python 安裝dlib和boost
Windows環境 安裝dlib(python) 總結

程序

注:程序中使用了python-opencv、dlib,使用前請配置好環境。
程序中已有注釋。

# -*- coding: utf-8 -*- import sys import dlib import cv2detector = dlib.get_frontal_face_detector() #獲取人臉分類器# 傳入的命令行參數 for f in sys.argv[1:]:# 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)# 等待按鍵,隨后退出,銷毀窗口 k = cv2.waitKey(0) cv2.destroyAllWindows()

運行結果

運行程序,后綴是圖片的名稱。

官方例程:

最后附上官方程序:

#!/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. In # particular, it shows how you can take a list of images from the command # line and display each on the screen with red boxes overlaid on each human # face. # # The examples/faces folder contains some jpg images of people. You can run # this program on them and see the detections by executing the # following command: # ./face_detector.py ../examples/faces/*.jpg # # This face detector is made using the now classic Histogram of Oriented # Gradients (HOG) feature combined with a linear classifier, an image # pyramid, and sliding window detection scheme. This type of object detector # is fairly general and capable of detecting many types of semi-rigid objects # in addition to human faces. Therefore, if you are interested in making # your own object detectors then read the train_object_detector.py example # program. # # # 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 sysimport dlib from skimage import iodetector = dlib.get_frontal_face_detector() win = dlib.image_window()for f in sys.argv[1:]:print("Processing file: {}".format(f))img = io.imread(f)# 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 i, d in enumerate(dets):print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(i, d.left(), d.top(), d.right(), d.bottom()))win.clear_overlay()win.set_image(img)win.add_overlay(dets)dlib.hit_enter_to_continue()# Finally, if you really want to you can ask the detector to tell you the score # for each detection. The score is bigger for more confident detections. # The third argument to run is an optional adjustment to the detection threshold, # where a negative value will return more detections and a positive value fewer. # Also, the idx tells you which of the face sub-detectors matched. This can be # used to broadly identify faces in different orientations. if (len(sys.argv[1:]) > 0):img = io.imread(sys.argv[1])dets, scores, idx = detector.run(img, 1, -1)for i, d in enumerate(dets):print("Detection {}, score: {}, face_type:{}".format(d, scores[i], idx[i]))

總結

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

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