當前位置:首頁 » 編程語言 » python人臉識別演算法

python人臉識別演算法

發布時間: 2022-06-10 05:34:44

⑴ 如何利用python進行精準人臉識別

要調用api介面,建議用face++的,支付寶的人臉識別都是用的這個。可能需要一點費用,不貴,代碼里把fece++的api介面放進代碼就行,還可以可以檢測情緒,年齡等等的。

當然也有其他公司人臉識別的api介面,自己發現吧,其實很多,但基本都不會免費,有的可以試用

⑵ 如何線上部署用python基於dlib寫的人臉識別演算法

python使用dlib進行人臉檢測與人臉關鍵點標記

Dlib簡介:

首先給大家介紹一下Dlib

我使用的版本是dlib-18.17,大家也可以在我這里下載:

之後進入python_examples下使用bat文件進行編譯,編譯需要先安裝libboost-python-dev和cmake

cd to dlib-18.17/python_examples

./compile_dlib_python_mole.bat 123

之後會得到一個dlib.so,復制到dist-packages目錄下即可使用

這里大家也可以直接用我編譯好的.so庫,但是也必須安裝libboost才可以,不然python是不能調用so庫的,下載地址:

將.so復制到dist-packages目錄下

sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/1

最新的dlib18.18好像就沒有這個bat文件了,取而代之的是一個setup文件,那麼安裝起來應該就沒有這么麻煩了,大家可以去直接安裝18.18,也可以直接下載復制我的.so庫,這兩種方法應該都不麻煩~

有時候還會需要下面這兩個庫,建議大家一並安裝一下

9.安裝skimage

sudo apt-get install python-skimage1

10.安裝imtools

sudo easy_install imtools1

Dlib face landmarks Demo

環境配置結束之後,我們首先看一下dlib提供的示常式序

1.人臉檢測

dlib-18.17/python_examples/face_detector.py 源程序:

#!/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 THE DLIB PYTHON INTERFACE# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If# you are using another python version or operating system then you need to# compile the dlib python interface before you can use this file. To do this,# run compile_dlib_python_mole.bat. This 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 -U scikit-image# Or downloaded from . import sys

import dlib

from skimage import io

detector = dlib.get_frontal_face_detector()

win = dlib.image_window()

print("a");for f in sys.argv[1:]:

print("a");

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.# 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) for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))5767778798081

我把源代碼精簡了一下,加了一下注釋: face_detector0.1.py

# -*- coding: utf-8 -*-import sys

import dlib

from skimage import io#使用dlib自帶的frontal_face_detector作為我們的特徵提取器detector = dlib.get_frontal_face_detector()#使用dlib提供的圖片窗口win = dlib.image_window()#sys.argv[]是用來獲取命令行參數的,sys.argv[0]表示代碼本身文件路徑,所以參數從1開始向後依次獲取圖片路徑for f in sys.argv[1:]: #輸出目前處理的圖片地址
print("Processing file: {}".format(f)) #使用skimage的io讀取圖片
img = io.imread(f) #使用detector進行人臉檢測 dets為返回的結果
dets = detector(img, 1) #dets的元素個數即為臉的個數
print("Number of faces detected: {}".format(len(dets))) #使用enumerate 函數遍歷序列中的元素以及它們的下標
#下標i即為人臉序號
#left:人臉左邊距離圖片左邊界的距離 ;right:人臉右邊距離圖片左邊界的距離
#top:人臉上邊距離圖片上邊界的距離 ;bottom:人臉下邊距離圖片上邊界的距離
for i, d in enumerate(dets):
print("dets{}".format(d))
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}"
.format( i, d.left(), d.top(), d.right(), d.bottom())) #也可以獲取比較全面的信息,如獲取人臉與detector的匹配程度
dets, scores, idx = detector.run(img, 1)
for i, d in enumerate(dets):
print("Detection {}, dets{},score: {}, face_type:{}".format( i, d, scores[i], idx[i]))

#繪制圖片(dlib的ui庫可以直接繪制dets)
win.set_image(img)
win.add_overlay(dets) #等待點擊
dlib.hit_enter_to_continue()041424344454647484950

分別測試了一個人臉的和多個人臉的,以下是運行結果:

運行的時候把圖片文件路徑加到後面就好了

python face_detector0.1.py ./data/3.jpg12

一張臉的:

兩張臉的:

這里可以看出側臉與detector的匹配度要比正臉小的很多

2.人臉關鍵點提取

人臉檢測我們使用了dlib自帶的人臉檢測器(detector),關鍵點提取需要一個特徵提取器(predictor),為了構建特徵提取器,預訓練模型必不可少。

除了自行進行訓練外,還可以使用官方提供的一個模型。該模型可從dlib sourceforge庫下載:

arks.dat.bz2

也可以從我的連接下載:

這個庫支持68個關鍵點的提取,一般來說也夠用了,如果需要更多的特徵點就要自己去訓練了。

dlib-18.17/python_examples/face_landmark_detection.py 源程序:

#!/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

⑶ python怎麼實現人工智慧

程序學習的過程就是使用梯度下降改變演算法模型參數的過程。

比如說f(x) = aX+b; 這裡面的參數是a和b,使用數據訓練演算法模型來改變參數,達到演算法模型可以實現人臉識別、語音識別的目的。

實現人工智慧的根本是演算法,python是實現演算法的一種語言,因為python語言的易用性和數據處理的友好性,所以現在很多用python語言做機器學習。其它語言比如java、c++等也也可以實現人工智慧相關演算法。下圖是一個神經網路的示意圖。

⑷ 如何學習python 圖像識別

圖像識別技術可以用來解決人臉識別或字元識別等多種問題。 在本文中,我將對演算法進行實際編碼來演示識別手寫字,特別是手寫的數字。我將會使用Python以及Python的許多模塊,比如numpy、PIL等。 1 #從PIL庫中導入Image

⑸ 人臉識別為什麼用python開發

可以使用OpenCV,OpenCV的人臉檢測功能在一般場合還是不錯的。而ubuntu正好提供了python-opencv這個包,用它可以方便地實現人臉檢測的代碼。

寫代碼之前應該先安裝python-opencv:

#!/usr/bin/python
#-*-coding:UTF-8-*-

#face_detect.py

#FaceDetectionusingOpenCV.Basedonsamplecodefrom:
#http://python.pastebin.com/m76db1d6b

#Usage:pythonface_detect.py<image_file>

importsys,os
fromopencv.cvimport*
fromopencv.highguiimport*
fromPILimportImage,ImageDraw
frommathimportsqrt

defdetectObjects(image):
""""""
grayscale=cvCreateImage(cvSize(image.width,image.height),8,1)
cvCvtColor(image,grayscale,CV_BGR2GRAY)

storage=cvCreateMemStorage(0)
cvClearMemStorage(storage)
cvEqualizeHist(grayscale,grayscale)

cascade=cvLoadHaarClassifierCascade(
'/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
cvSize(1,1))
faces=cvHaarDetectObjects(grayscale,cascade,storage,1.1,2,
CV_HAAR_DO_CANNY_PRUNING,cvSize(20,20))

result=[]
forfinfaces:
result.append((f.x,f.y,f.x+f.width,f.y+f.height))

returnresult

defgrayscale(r,g,b):
returnint(r*.3+g*.59+b*.11)

defprocess(infile,outfile):

image=cvLoadImage(infile);
ifimage:
faces=detectObjects(image)

im=Image.open(infile)

iffaces:
draw=ImageDraw.Draw(im)
forfinfaces:
draw.rectangle(f,outline=(255,0,255))

im.save(outfile,"JPEG",quality=100)
else:
print"Error:cannotdetectfaceson%s"%infile

if__name__=="__main__":
process('input.jpg','output.jpg')

⑹ 人臉識別怎麼弄

你好
你是指使用python來做人臉識別是嗎?
這個其實非常簡單,你只需要使用python裡面的opencv庫,就可以實現。
具體的參考文檔,可以去opencv的官方網站上下載。
我之前有做過類似的項目,不過是基於人工智慧的。使用的是谷歌tensorflow的庫
如果你需要指導,可以找我,我分享一些之前收集的例子給你。

⑺ 關於python人臉識別的問題

應該是沒有找到分類器編碼文件,把 haarcascade_frontalface_default.xml, haarcascade_eye.xml文件放到項目根目錄下,再用cv.CascadeClassifier(path1), cv.CascadeClassifier(path2)兩個API導入,另python下windows的文件路徑要用 \\ 或者 /

⑻ 如何用Python實現簡單人臉識別

你可以使用opencv庫提供的人臉識別模塊,這樣子會比較快

⑼ python opencv 怎麼利用csv文件訓練人臉識別模型代碼

1.1.介紹Introction
從OpenCV2.4開始,加入了新的類FaceRecognizer,我們可以使用它便捷地進行人臉識別實驗。本文既介紹代碼使用,又介紹演算法原理。(他寫的源代碼,我們可以在OpenCV的opencv\moles\contrib\doc\facerec\src下找到,當然也可以在他的github中找到,如果你想研究源碼,自然可以去看看,不復雜)

目前支持的演算法有
Eigenfaces特徵臉createEigenFaceRecognizer()
Fisherfaces createFisherFaceRecognizer()
LocalBinary Patterns Histograms局部二值直方圖 createLBPHFaceRecognizer()
下面所有的例子中的代碼在OpenCV安裝目錄下的samples/cpp下面都能找到,所有的代碼商用或者學習都是免費的。

1.2.人臉識別Face Recognition
對人類來說,人臉識別很容易。文獻[Tu06]告訴我們,僅僅是才三天的嬰兒已經可以區分周圍熟悉的人臉了。那麼對於計算機來說,到底有多難?其實,迄今為止,我們對於人類自己為何可以區分不同的人所知甚少。是人臉內部特徵(眼睛、鼻子、嘴巴)還是外部特徵(頭型、發際線)對於人類識別更有效?我們怎麼分析一張圖像,大腦是如何對它編碼的?David Hubel和TorstenWiesel向我們展示,我們的大腦針對不同的場景,如線、邊、角或者運動這些局部特徵有專門的神經細胞作出反應。顯然我們沒有把世界看成零散的塊塊,我們的視覺皮層必須以某種方式把不同的信息來源轉化成有用的模式。自動人臉識別就是如何從一幅圖像中提取有意義的特徵,把它們放入一種有用的表示方式,然後對他們進行一些分類。基於幾何特徵的人臉的人臉識別可能是最直觀的方法來識別人臉。第一個自動人臉識別系統在[Kanade73]中又描述:標記點(眼睛、耳朵、鼻子等的位置)用來構造一個特徵向量(點與點之間的距離、角度等)。通過計算測試和訓練圖像的特徵向量的歐氏距離來進行識別。這樣的方法對於光照變化很穩健,但也有巨大的缺點:標記點的確定是很復雜的,即使是使用最先進的演算法。一些幾何特徵人臉識別近期工作在文獻[Bru92]中有描述。一個22維的特徵向量被用在一個大資料庫上,單靠幾何特徵不能提供足夠的信息用於人臉識別。

特徵臉方法在文獻[TP91]中有描述,他描述了一個全面的方法來識別人臉:面部圖像是一個點,這個點是從高維圖像空間找到它在低維空間的表示,這樣分類變得很簡單。低維子空間低維是使用主元分析(Principal Component Analysis,PCA)找到的,它可以找擁有最大方差的那個軸。雖然這樣的轉換是從最佳重建角度考慮的,但是他沒有把標簽問題考慮進去。[gm:讀懂這段需要一些機器學習知識]。想像一個情況,如果變化是基於外部來源,比如光照。軸的最大方差不一定包含任何有鑒別性的信息,因此此時的分類是不可能的。因此,一個使用線性鑒別(Linear Discriminant Analysis,LDA)的特定類投影方法被提出來解決人臉識別問題[BHK97]。其中一個基本的想法就是,使類內方差最小的同時,使類外方差最大。
近年來,各種局部特徵提取方法出現。為了避免輸入的圖像的高維數據,僅僅使用的局部特徵描述圖像的方法被提出,提取的特徵(很有希望的)對於局部遮擋、光照變化、小樣本等情況更強健。有關局部特徵提取的方法有蓋伯小波(Gabor Waelets)([Wiskott97]),離散傅立葉變換(DiscreteCosinus Transform,DCT)([Messer06]),局部二值模式(LocalBinary Patterns,LBP)([AHP04])。使用什麼方法來提取時域空間的局部特徵依舊是一個開放性的研究問題,因為空間信息是潛在有用的信息。
1.3.人臉庫Face Database
我們先獲取一些數據來進行實驗吧。我不想在這里做一個幼稚的例子。我們在研究人臉識別,所以我們需要一個真的人臉圖像!你可以自己創建自己的數據集,也可以從這里(http://face-rec.org/databases/)下載一個。
AT&TFacedatabase又稱ORL人臉資料庫,40個人,每人10張照片。照片在不同時間、不同光照、不同表情(睜眼閉眼、笑或者不笑)、不同人臉細節(戴眼鏡或者不戴眼鏡)下採集。所有的圖像都在一個黑暗均勻的背景下採集的,正面豎直人臉(有些有有輕微旋轉)。

熱點內容
cgxrar解壓密碼 發布:2024-05-05 19:47:24 瀏覽:632
ubuntu編譯linux內核 發布:2024-05-05 19:46:05 瀏覽:7
php靜態方法調用對象 發布:2024-05-05 19:24:30 瀏覽:366
電腦LNS伺服器地址 發布:2024-05-05 19:22:15 瀏覽:376
不屬於編譯程序組成的部分是什麼 發布:2024-05-05 19:05:34 瀏覽:613
壓縮麵食 發布:2024-05-05 18:55:45 瀏覽:804
linux的gz解壓命令 發布:2024-05-05 18:24:13 瀏覽:311
伺服器機櫃屬於什麼輻射 發布:2024-05-05 18:02:10 瀏覽:336
存儲成本計算 發布:2024-05-05 18:02:10 瀏覽:584
如何把手機改安卓10 發布:2024-05-05 17:39:07 瀏覽:498