박선진

add test code for face recognition api

1 +import base64
2 +import face_recognition
3 +import numpy as np
4 +from cv2 import cv2
5 +import os
6 +
7 +known_path = './images/' # directory path of known faces
8 +image_format = 'jpg'
9 +
10 +# image to base64 encoding
11 +# you have to set 'encoded_image' to frame code
12 +with open("./images/obama.jpg", "rb") as img_file:
13 + encoded_image = base64.b64encode(img_file.read())
14 +
15 +#base64 to image(uint8) decoding
16 +img64_decode = base64.decodebytes(encoded_image)
17 +im_arr = np.frombuffer(img64_decode, dtype=np.uint8)
18 +decoded_img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
19 +
20 +#encoding frame
21 +unknown_face_encoding = face_recognition.face_encodings(decoded_img)[0]
22 +
23 +# Load & encode all images from known_path
24 +known_list = os.listdir(known_path)
25 +known_faces = []
26 +
27 +
28 +for f in known_list :
29 + if f.split('.')[-1] != image_format : continue
30 + known_img = face_recognition.load_image_file(known_path+f)
31 + known_img_encoding = face_recognition.face_encodings(known_img)[0]
32 + known_faces.append(known_img_encoding)
33 +
34 +# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
35 +results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
36 +print(results)
37 +#print(not True in results)
...\ No newline at end of file ...\ No newline at end of file