python dlib瀛︿範锛堜簲锛夛細姣斿浜鸿劯
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python dlib瀛︿範锛堜簲锛夛細姣斿浜鸿劯相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/extract' title='extract'>extract final 宸ヤ綔 _for mod create set ott 绋嬪簭
鍓嶈█
鍦ㄥ墠闈㈢殑鍗氬涓粙缁嶄簡锛屽浣曚娇鐢╠lib鏍囧畾浜鸿劯锛坧ython dlib瀛︿範锛堜竴锛夛細浜鸿劯妫€娴嬶級锛屾彁鍙?8涓壒寰佺偣锛坧ython dlib瀛︿範锛堜簩锛夛細浜鸿劯鐗瑰緛鐐规爣瀹氾級銆傝繖娆¤鍦ㄨ繖涓や釜宸ヤ綔鐨勫熀纭€涔嬩笂锛屽皢浜鸿劯鐨勪俊鎭彁鍙栨垚涓€涓?28缁寸殑鍚戦噺绌洪棿銆傚湪杩欎釜鍚戦噺绌洪棿涓婏紝鍚屼竴涓汉鑴哥殑鏇存帴杩戯紝涓嶅悓浜鸿劯鐨勮窛绂绘洿杩溿€傚害閲忛噰鐢ㄦ寮忚窛绂伙紝娆ф皬璺濈璁$畻涓嶇畻澶嶆潅銆?br />浜岀淮鎯呭喌涓嬶細
distance=(x1−x2)2+(y1−y2)2−−−−−−−−−−−−−−−−−−√
distance=(x1−x2)2+(y1−y2)2
涓夌淮鎯呭喌涓嬶細
distance=(x1−x2)2+(y1−y2)2+(z1−z2)2−−−−−−−−−−−−−−−−−−−−−−−−−−−−√
distance=(x1−x2)2+(y1−y2)2+(z1−z2)2
灏嗗叾鎵╁睍鍒?28缁寸殑鎯呭喌涓嬪嵆鍙€?br />閫氬父浣跨敤鐨勫垽鍒槇鍊兼槸0.6锛屽嵆濡傛灉涓や釜浜鸿劯鐨勫悜閲忕┖闂寸殑娆у紡璺濈瓒呰繃浜?.6锛屽嵆璁ゅ畾涓嶆槸鍚屼竴涓汉锛涘鏋滄姘忚窛绂诲皬浜?.6锛屽垯璁や负鏄悓涓€涓汉銆傝繖涓窛绂讳篃鍙互鐢辫嚜宸卞畾锛屽彧瑕佹晥鏋滆兘鏇村ソ銆?br />瀹為獙涓娇鐢ㄤ簡涓や釜妯″瀷锛?/p>
shape_predictor_68_face_landmarks.dat锛?br />http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
dlib_face_recognition_resnet_model_v1.dat锛?br />http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
鏂囦欢澶圭洰褰曪細
涓や釜妯″瀷鏀惧湪model鏂囦欢澶逛腑锛屾祴璇曞浘鐗囨斁鍦╢aces涓紝鍥剧墖鑷繁闅忎究涓嬪嚑寮犲氨琛屻€?/p>
瀹屾暣宸ョ▼涓嬭浇閾炬帴锛?br />http://pan.baidu.com/s/1boCDZ7T
绋嬪簭1
涓嶈搴熻瘽浜嗭紝鐩存帴涓婁唬鐮併€?/p>
# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob
current_path = os.getcwd() # 鑾峰彇褰撳墠璺緞
# 妯″瀷璺緞
predictor_path = current_path + "\model\shape_predictor_68_face_landmarks.dat"
face_rec_model_path = current_path + "\model\dlib_face_recognition_resnet_model_v1.dat"
#娴嬭瘯鍥剧墖璺緞
faces_folder_path = current_path + "\faces\"
# 璇诲叆妯″瀷
detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor(predictor_path)
face_rec_model = dlib.face_recognition_model_v1(face_rec_model_path)
for img_path in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(img_path))
# opencv 璇诲彇鍥剧墖锛屽苟鏄剧ず
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
# opencv鐨刡gr鏍煎紡鍥剧墖杞崲鎴恟gb鏍煎紡
b, g, r = cv2.split(img)
img2 = cv2.merge([r, g, b])
dets = detector(img, 1) # 浜鸿劯鏍囧畾
print("Number of faces detected: {}".format(len(dets)))
for index, face in enumerate(dets):
print(鈥榝ace {}; left {}; top {}; right {}; bottom {}鈥?format(index, face.left(), face.top(), face.right(), face.bottom()))
shape = shape_predictor(img2, face) # 鎻愬彇68涓壒寰佺偣
for i, pt in enumerate(shape.parts()):
#print(鈥楶art {}: {}鈥?format(i, pt))
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)
#print(type(pt))
#print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
cv2.namedWindow(img_path+str(index), cv2.WINDOW_AUTOSIZE)
cv2.imshow(img_path+str(index), img)
face_descriptor = face_rec_model.compute_face_descriptor(img2, shape) # 璁$畻浜鸿劯鐨?28缁寸殑鍚戦噺
print(face_descriptor)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
绋嬪簭1缁撴灉
閮ㄥ垎鎵撳嵃缁撴灉锛?/p>
F:Pythonmy_dlib_codesface_recognition>python my_face_recogniton.py
Processing file: F:Pythonmy_dlib_codesface_recognitionfacesjobs.jpg
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
-0.179784
0.15487
0.10509
-0.0973604
-0.19153
0.000418252
-0.0357536
-0.0206766
0.129741
-0.0628359
....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
鍚庨潰鐨勯偅涓€鍫嗘暟瀛楀氨鏄汉鑴稿湪128缁村悜閲忕┖闂翠笂鐨勫€笺€?/p>
绋嬪簭2
鍓嶉潰鍙槸娴嬭瘯浜嗕竴涓嬶紝鎶婅鐢ㄧ殑鍊肩粰姹傚埌浜嗐€傝繖閲屾垜灏佽浜嗕竴涓嬶紝鎶婃瘮瀵瑰姛鑳藉疄鐜颁簡銆傛病鍔犲灏戜笢瑗匡紝鎵€浠ヤ笉鍋氳禈杩颁簡銆?/p>
# -*- coding: utf-8 -*-
import sys
import dlib
import cv2
import os
import glob
import numpy as np
def comparePersonData(data1, data2):
diff = 0
# for v1, v2 in data1, data2:
# diff += (v1 - v2)**2
for i in xrange(len(data1)):
diff += (data1[i] - data2[i])**2
diff = np.sqrt(diff)
print diff
if(diff < 0.6):
print "It鈥榮 the same person"
else:
print "It鈥榮 not the same person"
def savePersonData(face_rec_class, face_descriptor):
if face_rec_class.name == None or face_descriptor == None:
return
filePath = face_rec_class.dataPath + face_rec_class.name + 鈥?npy鈥?br /> vectors = np.array([])
for i, num in enumerate(face_descriptor):
vectors = np.append(vectors, num)
# print(num)
print(鈥楽aving files to :鈥?filePath)
np.save(filePath, vectors)
return vectors
def loadPersonData(face_rec_class, personName):
if personName == None:
return
filePath = face_rec_class.dataPath + personName + 鈥?npy鈥?br /> vectors = np.load(filePath)
print(vectors)
return vectors
class face_recognition(object):
def __init__(self):
self.current_path = os.getcwd() # 鑾峰彇褰撳墠璺緞
self.predictor_path = self.current_path + "\model\shape_predictor_68_face_landmarks.dat"
self.face_rec_model_path = self.current_path + "\model\dlib_face_recognition_resnet_model_v1.dat"
self.faces_folder_path = self.current_path + "\faces\"
self.dataPath = self.current_path + "\data\"
self.detector = dlib.get_frontal_face_detector()
self.shape_predictor = dlib.shape_predictor(self.predictor_path)
self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)
self.name = None
self.img_bgr = None
self.img_rgb = None
self.detector = dlib.get_frontal_face_detector()
self.shape_predictor = dlib.shape_predictor(self.predictor_path)
self.face_rec_model = dlib.face_recognition_model_v1(self.face_rec_model_path)
def inputPerson(self, name=鈥榩eople鈥? img_path=None):
if img_path == None:
print(鈥楴o file!
鈥?
return
# img_name += self.faces_folder_path + img_name
self.name = name
self.img_bgr = cv2.imread(self.current_path+img_path)
# opencv鐨刡gr鏍煎紡鍥剧墖杞崲鎴恟gb鏍煎紡
b, g, r = cv2.split(self.img_bgr)
self.img_rgb = cv2.merge([r, g, b])
def create128DVectorSpace(self):
dets = self.detector(self.img_rgb, 1)
print("Number of faces detected: {}".format(len(dets)))
for index, face in enumerate(dets):
print(鈥榝ace {}; left {}; top {}; right {}; bottom {}鈥?format(index, face.left(), face.top(), face.right(), face.bottom()))
shape = self.shape_predictor(self.img_rgb, face)
face_descriptor = self.face_rec_model.compute_face_descriptor(self.img_rgb, shape)
# print(face_descriptor)
# for i, num in enumerate(face_descriptor):
# print(num)
# print(type(num))
return face_descriptor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
绋嬪簭2缁撴灉
娴嬭瘯浠g爜1锛?/p>
import face_rec as fc
face_rec = fc.face_recognition() # 鍒涘缓瀵硅薄
face_rec.inputPerson(name=鈥榡obs鈥? img_path=鈥榎faces\jobs.jpg鈥? # name涓啓绗竴涓汉鍚嶅瓧锛宨mg_name涓哄浘鐗囧悕瀛楋紝娉ㄦ剰瑕佹斁鍦╢aces鏂囦欢澶逛腑
vector = face_rec.create128DVectorSpace() # 鎻愬彇128缁村悜閲忥紝鏄痙lib.vector绫荤殑瀵硅薄
person_data1 = fc.savePersonData(face_rec, vector ) # 灏嗘彁鍙栧嚭鐨勬暟鎹繚瀛樺埌data鏂囦欢澶癸紝涓轰究浜庢搷浣滆繑鍥瀗umpy鏁扮粍锛屽唴瀹硅繕鏄竴鏍风殑
# 瀵煎叆绗簩寮犲浘鐗囷紝骞舵彁鍙栫壒寰佸悜閲?br />face_rec.inputPerson(name=鈥榡obs2鈥? img_path=鈥榎faces\jobs2.jpg鈥?
vector = face_rec.create128DVectorSpace() # 鎻愬彇128缁村悜閲忥紝鏄痙lib.vector绫荤殑瀵硅薄
person_data2 = fc.savePersonData(face_rec, vector )
# 璁$畻娆у紡璺濈锛屽垽鏂槸鍚︽槸鍚屼竴涓汉
fc.comparePersonData(person_data1, person_data2)
1
2
3
4
5
6
7
8
9
10
11
12
13
濡傛灉data鏂囦欢澶逛腑宸茬粡鏈変簡妯″瀷鏂囦欢锛屽彲浠ョ洿鎺ュ鍏ワ細
import face_rec as fc
face_rec = fc.face_recognition() # 鍒涘缓瀵硅薄
person_data1 = fc.loadPersonData(face_rec , 鈥榡obs鈥? # 鍒涘缓涓€涓被淇濆瓨鐩稿叧淇℃伅锛屽悗闈㈣繕瑕佽窡涓婁汉鍚嶏紝绋嬪簭浼氬湪data鏂囦欢涓煡鎵惧搴攏py鏂囦欢锛屾瘮濡傝繖閲屽氨鏄€榡obs.npy鈥?br />person_data2 = fc.loadPersonData(face_rec , 鈥榡obs2鈥? # 瀵煎叆绗簩寮犲浘鐗?br />fc.comparePersonData(person_data1, person_data2) # 璁$畻娆у紡璺濈锛屽垽鏂槸鍚︽槸鍚屼竴涓汉
1
2
3
4
5
绋嬪簭2缁撴灉
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import face_rec as fc
>>> face_rec = fc.face_recognition()
>>> face_rec.inputPerson(name=鈥榡obs鈥? img_path=鈥榎faces\jobs.jpg鈥?
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 184; top 64; right 339; bottom 219
>>> person_data1 = fc.savePersonData(face_rec, vector )
Saving files to :F:Pythonmy_dlib_codesface_recognitiondatajobs.npy
>>> face_rec.inputPerson(name=鈥榡obs2鈥? img_path=鈥榎faces\jobs2.jpg鈥?
>>> vector = face_rec.create128DVectorSpace()
Number of faces detected: 1
face 0; left 124; top 39; right 253; bottom 168
>>> person_data2 = fc.savePersonData(face_rec, vector )
Saving files to :F:Pythonmy_dlib_codesface_recognitiondatajobs2.npy
>>> fc.comparePersonData(person_data1, person_data2)
0.490491048429
It鈥榮 the same person
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
瀹樻柟渚嬬▼
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example shows how to use dlib鈥榮 face recognition tool. This tool maps
# an image of a human face to a 128 dimensional vector space where images of
# the same person are near to each other and images from different people are
# far apart. Therefore, you can perform face recognition by mapping faces to
# the 128D space and then checking if their Euclidean distance is small
# enough.
#
# When using a distance threshold of 0.6, the dlib model obtains an accuracy
# of 99.38% on the standard LFW face recognition benchmark, which is
# comparable to other state-of-the-art methods for face recognition as of
# February 2017. This accuracy means that, when presented with a pair of face
# images, the tool will correctly identify if the pair belongs to the same
# person or is from different people 99.38% of the time.
#
# Finally, for an in-depth discussion of how dlib鈥榮 tool works you should
# refer to the C++ example program dnn_face_recognition_ex.cpp and the
# attendant documentation referenced therein.
#
#
#
#
# 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. This code will also use CUDA if you have CUDA and cuDNN
# installed.
#
# 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 io
if len(sys.argv) != 4:
print(
"Call this program like this:
"
" ./face_recognition.py shape_predictor_68_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces
"
"You can download a trained facial shape predictor and recognition model from:
"
" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
"
" http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2")
exit()
predictor_path = sys.argv[1]
face_rec_model_path = sys.argv[2]
faces_folder_path = sys.argv[3]
# Load all the models we need: a detector to find the faces, a shape predictor
# to find face landmarks so we can precisely localize the face, and finally the
# face recognition model.
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
win = dlib.image_window()
# Now process all the images
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)))
# Now process each face we found.
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 = sp(img, d)
# Draw the face landmarks on the screen so we can see what face is currently being processed.
win.clear_overlay()
win.add_overlay(d)
win.add_overlay(shape)
# Compute the 128D vector that describes the face in img identified by
# shape. In general, if two face descriptor vectors have a Euclidean
# distance between them less than 0.6 then they are from the same
# person, otherwise they are from different people. Here we just print
# the vector to the screen.
face_descriptor = facerec.compute_face_descriptor(img, shape)
print(face_descriptor)
# It should also be noted that you can also call this function like this:
# face_descriptor = facerec.compute_face_descriptor(img, shape, 100)
# The version of the call without the 100 gets 99.13% accuracy on LFW
# while the version with 100 gets 99.38%. However, the 100 makes the
# call 100x slower to execute, so choose whatever version you like. To
# explain a little, the 3rd argument tells the code how many times to
# jitter/resample the image. When you set it to 100 it executes the
# face descriptor extraction 100 times on slightly modified versions of
# the face and returns the average result. You could also pick a more
# middle value, such as 10, which is only 10x slower but still gets an
# LFW accuracy of 99.3%.
dlib.hit_enter_to_continue()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
鍚愭Ы锛?br />dlib鐨勭‘寰堟柟渚匡紝涓嶇敤鑺卞灏戞椂闂村氨鑳借嚜宸卞仛鍒颁竴浜涚洰鏍囧姛鑳姐€傚畼鏂规枃妗h鐨勫緢璇︾粏锛屽緢瀹规槗鍏ラ棬銆傜湅杩欎釜鏂囨。锛坉lib python api锛夊樊涓嶅灏辫兘瀛︿細鐢ㄤ簡銆傚甯堝凡缁忓畨鎺掍簡鐮旂┒鐢熼樁娈电殑瀛︿範浠诲姟浜嗭紝鍚庨潰涔熻蹇欒捣鏉ヤ簡銆俤lib鐨勫涔犺櫧鐒舵槸鎴?0鏈堜唤鎵嶅紑鐨勫潙锛屼负浜嗗杽濮嬪杽缁堟垜涔熻灏藉揩鏁寸悊瀹岃繖浜涗笢瑗裤€備互鍚庤鍥炲埌”娉¢”鐢熸椿浜嗐€?br />
鍘熸枃閾炬帴锛歨ttps://blog.csdn.net/hongbin_xu/article/details/78390982
以上是关于python dlib瀛︿範锛堜簲锛夛細姣斿浜鸿劯的主要内容,如果未能解决你的问题,请参考以下文章
濡備綍浣跨敤Nmap杩涜绔彛鎵弿锛堜簲锛夊浣曡繘琛屾壂鎻忔妧鏈殑閫夋嫨
绋嬪簭鎬ц兘浼樺寲涔嬬綉缁滀紶杈撲笌鏁版嵁瀛樺偍浼樺寲锛堜簲锛変笅
寰湇鍔¤縼绉昏锛堜笁锛夛細閰嶇疆涓績SpringCloud Config鎼缓