박선진

seperate process class for readability

1 -import base64 1 +import process
2 -import face_recognition
3 -import numpy as np
4 -from cv2 import cv2
5 -import os
6 2
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
3 +if __name__ == '__main__':
4 + p = process.Process()
5 + p.ProcessImage()
...\ No newline at end of file ...\ No newline at end of file
......
1 +import base64
2 +import face_recognition
3 +import numpy as np
4 +from cv2 import cv2
5 +import os
6 +
7 +class Process:
8 + def __init__(self):
9 + known_path = './images/' # directory path of known faces
10 + image_format = 'jpg'
11 +
12 + known_list = os.listdir(known_path)
13 + self.known_faces = []
14 +
15 + # Load & encode all images from known_path
16 + for f in known_list :
17 + if f.split('.')[-1] != image_format : continue
18 + known_img = face_recognition.load_image_file(known_path+f)
19 + known_img_encoding = face_recognition.face_encodings(known_img)[0]
20 + self.known_faces.append(known_img_encoding)
21 + def ProcessImage(self):
22 + # image to base64 encoding
23 + # you have to set 'encoded_image' to frame code
24 + with open("./images/obama.jpg", "rb") as img_file:
25 + encoded_image = base64.b64encode(img_file.read())
26 +
27 + #base64 to image(uint8) decoding
28 + img64_decode = base64.b64decode(encoded_image)
29 + im_arr = np.frombuffer(img64_decode, dtype=np.uint8)
30 + decoded_img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
31 +
32 + face = self.FaceRecognition(decoded_img) #True면 침입자 발생
33 +
34 + result= {"unknown_person" : face}
35 + print(result)
36 + return result
37 + def FaceRecognition(self, decoded_img):
38 + #encoding frame
39 + try :
40 + unknown_face_encoding = face_recognition.face_encodings(decoded_img)[0]
41 + # results is an array of True/False telling if the unknown face matched anyone in the known_faces array
42 + # 아는 얼굴이면 False, 모르는 얼굴이면 True
43 + results = face_recognition.compare_faces(self.known_faces, unknown_face_encoding)
44 + return not True in results
45 + except IndexError:
46 + print("얼굴없음")
47 + return False
...\ No newline at end of file ...\ No newline at end of file