Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Harrytsz
face_recognition
提交
8f8c8b15
F
face_recognition
项目概览
Harrytsz
/
face_recognition
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
face_recognition
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8f8c8b15
编写于
4月 04, 2017
作者:
A
Adam Geitgey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Expose face_distance and prevent errors with empty inputs
上级
b7b8b9f6
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
75 addition
and
7 deletion
+75
-7
face_recognition/__init__.py
face_recognition/__init__.py
+1
-1
face_recognition/api.py
face_recognition/api.py
+9
-6
tests/test_face_recognition.py
tests/test_face_recognition.py
+65
-0
未找到文件。
face_recognition/__init__.py
浏览文件 @
8f8c8b15
...
...
@@ -4,4 +4,4 @@ __author__ = """Adam Geitgey"""
__email__
=
'ageitgey@gmail.com'
__version__
=
'0.1.0'
from
.api
import
load_image_file
,
face_locations
,
face_landmarks
,
face_encodings
,
compare_faces
from
.api
import
load_image_file
,
face_locations
,
face_landmarks
,
face_encodings
,
compare_faces
,
face_distance
face_recognition/api.py
浏览文件 @
8f8c8b15
...
...
@@ -52,16 +52,19 @@ def _trim_css_to_bounds(css, image_shape):
return
max
(
css
[
0
],
0
),
min
(
css
[
1
],
image_shape
[
1
]),
min
(
css
[
2
],
image_shape
[
0
]),
max
(
css
[
3
],
0
)
def
_face_distance
(
face
s
,
face_to_compare
):
def
face_distance
(
face_encoding
s
,
face_to_compare
):
"""
Given a list of face encodings, compare
d
them to a known face encoding and get a euclidean distance
for each comparison face.
Given a list of face encodings, compare them to a known face encoding and get a euclidean distance
for each comparison face.
The distance tells you how similar the faces are.
:param faces: List of face encodings to compare
:param face_to_compare: A face encoding to compare against
:return: A
list
with the distance for each face in the same order as the 'faces' array
:return: A
numpy ndarray
with the distance for each face in the same order as the 'faces' array
"""
return
np
.
linalg
.
norm
(
faces
-
face_to_compare
,
axis
=
1
)
if
len
(
face_encodings
)
==
0
:
return
np
.
empty
((
0
))
return
np
.
linalg
.
norm
(
face_encodings
-
face_to_compare
,
axis
=
1
)
def
load_image_file
(
filename
,
mode
=
'RGB'
):
...
...
@@ -154,4 +157,4 @@ def compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6):
:param tolerance: How much distance between faces to consider it a match. Lower is more strict. 0.6 is typical best performance.
:return: A list of True/False values indicating which known_face_encodings match the face encoding to check
"""
return
list
(
_
face_distance
(
known_face_encodings
,
face_encoding_to_check
)
<=
tolerance
)
return
list
(
face_distance
(
known_face_encodings
,
face_encoding_to_check
)
<=
tolerance
)
tests/test_face_recognition.py
浏览文件 @
8f8c8b15
...
...
@@ -11,6 +11,7 @@ Tests for `face_recognition` module.
import
unittest
import
os
import
numpy
as
np
from
click.testing
import
CliRunner
from
face_recognition
import
api
...
...
@@ -94,6 +95,50 @@ class Test_face_recognition(unittest.TestCase):
self
.
assertEqual
(
len
(
encodings
),
1
)
self
.
assertEqual
(
len
(
encodings
[
0
]),
128
)
def
test_face_distance
(
self
):
img_a1
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'obama.jpg'
))
img_a2
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'obama2.jpg'
))
img_a3
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'obama3.jpg'
))
img_b1
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'biden.jpg'
))
face_encoding_a1
=
api
.
face_encodings
(
img_a1
)[
0
]
face_encoding_a2
=
api
.
face_encodings
(
img_a2
)[
0
]
face_encoding_a3
=
api
.
face_encodings
(
img_a3
)[
0
]
face_encoding_b1
=
api
.
face_encodings
(
img_b1
)[
0
]
faces_to_compare
=
[
face_encoding_a2
,
face_encoding_a3
,
face_encoding_b1
]
distance_results
=
api
.
face_distance
(
faces_to_compare
,
face_encoding_a1
)
# 0.6 is the default face distance match threshold. So we'll spot-check that the numbers returned
# are above or below that based on if they should match (since the exact numbers could vary).
self
.
assertEqual
(
type
(
distance_results
),
np
.
ndarray
)
self
.
assertLessEqual
(
distance_results
[
0
],
0.6
)
self
.
assertLessEqual
(
distance_results
[
1
],
0.6
)
self
.
assertGreater
(
distance_results
[
2
],
0.6
)
def
test_face_distance_empty_lists
(
self
):
img
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'biden.jpg'
))
face_encoding
=
api
.
face_encodings
(
img
)[
0
]
# empty python list
faces_to_compare
=
[]
distance_results
=
api
.
face_distance
(
faces_to_compare
,
face_encoding
)
self
.
assertEqual
(
type
(
distance_results
),
np
.
ndarray
)
self
.
assertEqual
(
len
(
distance_results
),
0
)
# empty numpy list
faces_to_compare
=
np
.
array
([])
distance_results
=
api
.
face_distance
(
faces_to_compare
,
face_encoding
)
self
.
assertEqual
(
type
(
distance_results
),
np
.
ndarray
)
self
.
assertEqual
(
len
(
distance_results
),
0
)
def
test_compare_faces
(
self
):
img_a1
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'obama.jpg'
))
img_a2
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'obama2.jpg'
))
...
...
@@ -112,10 +157,30 @@ class Test_face_recognition(unittest.TestCase):
face_encoding_b1
]
match_results
=
api
.
compare_faces
(
faces_to_compare
,
face_encoding_a1
)
self
.
assertEqual
(
type
(
match_results
),
list
)
self
.
assertTrue
(
match_results
[
0
])
self
.
assertTrue
(
match_results
[
1
])
self
.
assertFalse
(
match_results
[
2
])
def
test_compare_faces_empty_lists
(
self
):
img
=
api
.
load_image_file
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'test_images'
,
'biden.jpg'
))
face_encoding
=
api
.
face_encodings
(
img
)[
0
]
# empty python list
faces_to_compare
=
[]
match_results
=
api
.
compare_faces
(
faces_to_compare
,
face_encoding
)
self
.
assertEqual
(
type
(
match_results
),
list
)
self
.
assertListEqual
(
match_results
,
[])
# empty numpy list
faces_to_compare
=
np
.
array
([])
match_results
=
api
.
compare_faces
(
faces_to_compare
,
face_encoding
)
self
.
assertEqual
(
type
(
match_results
),
list
)
self
.
assertListEqual
(
match_results
,
[])
def
test_command_line_interface
(
self
):
target_string
=
'--help Show this message and exit.'
runner
=
CliRunner
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录