Face Varification, Detection 등의 프로젝트를 진행하다 보면
face_recognition package를 사용할 일이 무조건 생기게 된다.
def get_face_embedding(face):
return face_recognition.face_encodings(face)
embedding = get_face_embedding(face)
embedding
>>> []
위와같이 인코딩 결과가 [ ] 로 빈리스트가 나오는 경우가 있다.
물론 이는 인코딩이 제대로 이루어지지 않았다는 뜻
import face_recognition
import os
# 1. 인풋 이미지를 확실하게 crop한다.
def get_cropped_face(image_file):
image = face_recognition.load_image_file(image_file)
face_locations = face_recognition.face_locations(image,
model="cnn",
number_of_times_to_upsample=0) # 옵션은 설정하기 나름입니다.
a, b, c, d = face_locations[0]
cropped_face = image[a:c,d:b,:]
return cropped_face
a, b, c, d = face_locations[0]
cropped_face = image[a:c,d:b,:]
return cropped_face
def get_face_embedding(face):
width = face.shape[1]
height = face.shape[0]
return face_recognition.face_encodings(face,
known_face_locations=[(0, width, height, 0)])
# 2. crop된 이미지 형태를 known_face_locations 옵션에 적용한다.
# 3. encoding을 한다.
embedding = get_face_embedding(face)
embedding
>>>[array([-0.0603382 , 0.08556256, 0.0259272 , -0.0852174 , -0.120474 ,
-0.04559196, -0.02482977, -0.04379142, 0.11300017, -0.02566482,
0.1368414 , 0.01160678, -0.27919298, 0.01683448, -0.02009312,
0.10119183, -0.18841152, -0.04788169, -0.13122849, -0.09714682,
-0.01510313, 0.1327755 , 0.01203356, 0.02821851, -0.0656175 ,
-0.29238069, -0.10144626, -0.08709388, 0.03876785, -0.08957299,
0.02157175, -0.01933426, -0.14223967, -0.0765765 , 0.04637664,
0.03653152, -0.10078526, -0.09714238, 0.2532303 , 0.13860542,
-0.12767646, -0.01342493, 0.02979014, 0.32913166, 0.19005337,
-0.00349848, 0.07072091, -0.0177896 , 0.11243887, -0.2751283 ,
0.03709012, 0.14938839, 0.12012978, 0.0452648 , 0.13796806,
-0.16417755, 0.03775452, 0.13102669, -0.16911776, 0.10792019,
0.10121979, -0.11002138, 0.01480763, 0.02215616, 0.21359196,
0.0680555 , -0.07252643, -0.07897869, 0.22430924, -0.14174683,
-0.06722899, 0.01969626, -0.06575602, -0.12909298, -0.25594592,
-0.00673575, 0.38750848, 0.12689053, -0.18602556, -0.00670487,
-0.11342654, -0.01023668, 0.13944599, 0.03272397, -0.09379099,
-0.00525879, -0.11844102, 0.04566789, 0.22337908, -0.02743023,
-0.03680295, 0.19553161, 0.06839585, -0.0074615 , 0.13481197,
0.00787758, -0.08261447, -0.0710373 , -0.11588717, 0.05834041,
0.05416309, -0.21532783, 0.01468534, 0.09035382, -0.17504165,
0.12986799, 0.03529223, -0.09361142, -0.08592534, -0.01479616,
-0.13949044, 0.02852916, 0.22393201, -0.24963461, 0.23736025,
0.1814633 , -0.0871958 , 0.1407748 , 0.01992018, 0.05439388,
-0.06209055, -0.06841236, -0.08576148, -0.1553738 , -0.04115767,
0.0435479 , 0.07126591, 0.05229743])]
위와같이 진행하면 확실하게 embedding벡터를 return할 수 있습니다