박선진

seperate process class for readability

import base64
import face_recognition
import numpy as np
from cv2 import cv2
import os
import process
known_path = './images/' # directory path of known faces
image_format = 'jpg'
# image to base64 encoding
# you have to set 'encoded_image' to frame code
with open("./images/obama.jpg", "rb") as img_file:
encoded_image = base64.b64encode(img_file.read())
#base64 to image(uint8) decoding
img64_decode = base64.decodebytes(encoded_image)
im_arr = np.frombuffer(img64_decode, dtype=np.uint8)
decoded_img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
#encoding frame
unknown_face_encoding = face_recognition.face_encodings(decoded_img)[0]
# Load & encode all images from known_path
known_list = os.listdir(known_path)
known_faces = []
for f in known_list :
if f.split('.')[-1] != image_format : continue
known_img = face_recognition.load_image_file(known_path+f)
known_img_encoding = face_recognition.face_encodings(known_img)[0]
known_faces.append(known_img_encoding)
# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print(results)
#print(not True in results)
\ No newline at end of file
if __name__ == '__main__':
p = process.Process()
p.ProcessImage()
\ No newline at end of file
......
import base64
import face_recognition
import numpy as np
from cv2 import cv2
import os
class Process:
def __init__(self):
known_path = './images/' # directory path of known faces
image_format = 'jpg'
known_list = os.listdir(known_path)
self.known_faces = []
# Load & encode all images from known_path
for f in known_list :
if f.split('.')[-1] != image_format : continue
known_img = face_recognition.load_image_file(known_path+f)
known_img_encoding = face_recognition.face_encodings(known_img)[0]
self.known_faces.append(known_img_encoding)
def ProcessImage(self):
# image to base64 encoding
# you have to set 'encoded_image' to frame code
with open("./images/obama.jpg", "rb") as img_file:
encoded_image = base64.b64encode(img_file.read())
#base64 to image(uint8) decoding
img64_decode = base64.b64decode(encoded_image)
im_arr = np.frombuffer(img64_decode, dtype=np.uint8)
decoded_img = cv2.imdecode(im_arr, flags=cv2.IMREAD_COLOR)
face = self.FaceRecognition(decoded_img) #True면 침입자 발생
result= {"unknown_person" : face}
print(result)
return result
def FaceRecognition(self, decoded_img):
#encoding frame
try :
unknown_face_encoding = face_recognition.face_encodings(decoded_img)[0]
# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
# 아는 얼굴이면 False, 모르는 얼굴이면 True
results = face_recognition.compare_faces(self.known_faces, unknown_face_encoding)
return not True in results
except IndexError:
print("얼굴없음")
return False
\ No newline at end of file