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

歡迎訪問 生活随笔!

生活随笔

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

python

python dlib学习(三):调用cnn人脸检测

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

前言

調(diào)用訓練好的卷積神經(jīng)網(wǎng)絡(luò)(CNN)模型進行人臉檢測。
模型下載鏈接:http://dlib.net/files/mmod_human_face_detector.dat.bz2

程序

注:使用了opencv和dlib,需要自行配置環(huán)境。

# -*- coding: utf-8 -*- import sys import dlib import cv2# 導(dǎo)入cnn模型 cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])for f in sys.argv[2:]:# opencv 讀取圖片,并顯示img = cv2.imread(f, cv2.IMREAD_COLOR)# opencv的bgr格式圖片轉(zhuǎn)換成rgb格式b, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])# 進行檢測dets = cnn_face_detector(img, 1)# 打印檢測到的人臉數(shù)print("Number of faces detected: {}".format(len(dets)))# 遍歷返回的結(jié)果# 返回的結(jié)果是一個mmod_rectangles對象。這個對象包含有2個成員變量:dlib.rectangle類,表示對象的位置;dlib.confidence,表示置信度。for i, d in enumerate(dets):face = d.rectprint("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(i, face.left(), face.top(), face.right(), d.rect.bottom(), d.confidence))# 在圖片中標出人臉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()

程序中已經(jīng)有注釋,也可以參考python dlib學習(一):人臉檢測。

運行結(jié)果

命令行下,輸入:

python cnn_face_detector.py mmod_human_face_detector.dat jobs.jpg obama.jpg silicon_valley.jpg

目錄下有以下文件:

結(jié)果截圖:
(運行速度有點慢,要多等一下)
雖然dlib也支持cuda,怕我的筆記本扛不住還是用的CPU跑的。

官方例程

#!/usr/bin/python # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt # # This example shows how to run a CNN based face detector using dlib. The # example loads a pretrained model and uses it to find faces in images. The # CNN model is much more accurate than the HOG based model shown in the # face_detector.py example, but takes much more computational power to # run, and is meant to be executed on a GPU to attain reasonable speed. # # You can download the pre-trained model from: # http://dlib.net/files/mmod_human_face_detector.dat.bz2 # # 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: # ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg # # # 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 --yes DLIB_USE_CUDA # if you have a CPU that supports AVX instructions, you have an Nvidia GPU # and you have CUDA installed since this makes things run *much* 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 dlib from skimage import ioif len(sys.argv) < 3:print("Call this program like this:\n"" ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg\n""You can get the mmod_human_face_detector.dat file from:\n"" http://dlib.net/files/mmod_human_face_detector.dat.bz2")exit()cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1]) win = dlib.image_window()for f in sys.argv[2:]: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 = cnn_face_detector(img, 1)'''This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.These objects can be accessed by simply iterating over the mmod_rectangles objectThe mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.It is also possible to pass a list of images to the detector.- like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128)In this case it will return a mmod_rectangless object.This object behaves just like a list of lists and can be iterated over.'''print("Number of faces detected: {}".format(len(dets)))for i, d in enumerate(dets):print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))rects = dlib.rectangles()rects.extend([d.rect for d in dets])win.clear_overlay()win.set_image(img)win.add_overlay(rects)dlib.hit_enter_to_continue()

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。