class LivenessDetection(): def __init__(self, face_detection_model_path, liveness_model_path, threshold, image_size): self.model = None self.liveness_model_path = liveness_model_path self.face_detection_model_path = face_detection_model_path self.threshold = threshold self.image_size = image_size def initial(self): self.model = load_model(self.liveness_model_path) self.model._make_predict_function() self.face_detect = FaceDetection(self.face_detection_model_path) def process(self, image, mode): data = [] x, y, w, h = self.face_detect.process( cv2.cvtColor(image, cv2.COLOR_RGB2BGR)) if w > 0 and h > 0: face_image = image[y:y + h, x:x + w] face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY) face_image = cv2.resize(face_image, (self.image_size, self.image_size)) else: face_image = np.zeros((self.image_size, self.image_size), dtype=int) image = cv2.resize(image, (self.image_size, self.image_size)) image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) image[:, :, 0] = face_image data.append(image) data = np.array(data, dtype="float") / 255.0 preds = self.model.predict(data) score = preds[0][0] predict = True if score > self.threshold else False return score, predict
class LivenessDetection(): def __init__(self, face_detection_model_path, liveness_model_path, threshold, image_size): self.model = None self.liveness_model_path = liveness_model_path self.face_detection_model_path = face_detection_model_path self.threshold = threshold self.image_size = image_size def initial(self): self.model = keras.models.load_model(self.liveness_model_path) self.model._make_predict_function() self.face_detect = FaceDetection(self.face_detection_model_path) def process(self, image, mode): data = [] image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # image = imutils.resize(image, width=800) x, y, w, h = self.face_detect.process(image) if w > 0 and h > 0: # img_face = image[y:y+h, x:x+w] new_pad = np.min( [x, image.shape[1] - (x + w), y, image.shape[0] - (y + h)]) image = image[y - new_pad:y + h + new_pad, x - new_pad:x + w + new_pad, :] image = cv2.resize(image, (self.image_size, self.image_size)) image = image.astype(np.float32) / 127.5 - 1 score = self.model.predict_on_batch(np.expand_dims(image, axis=0))[0] predict = True if score > self.threshold else False else: score = -999 predict = False return score, predict
from face_detection import FaceDetection from util.db import DynamoDBUtils from util import misc import random import shutil import pprint import os OUTPUT_DIR = '../videos' if __name__ == '__main__': BUCKET_NAME = 'smart-cam' ''' if os.path.exists(OUTPUT_DIR): shutil.rmtree(OUTPUT_DIR) os.makedirs(OUTPUT_DIR) ret, local_file = misc.download_from_s3(BUCKET_NAME, 'videos/video_1.avi', OUTPUT_DIR) print ret, local_file ''' #local_file = '../videos/video_100_frames_2.mp4' local_file = '../videos/video_100_frames_1.mp4' #fd = FaceDetectionV1() fd = FaceDetection() report = fd.process(local_file, show_frame=False) pprint.pprint(report)
from face_detection_v1 import FaceDetectionV1 from face_detection import FaceDetection from util.db import DynamoDBUtils from util import misc import random import shutil import pprint import os OUTPUT_DIR = '../videos' if __name__ == '__main__': BUCKET_NAME = 'smart-cam' ''' if os.path.exists(OUTPUT_DIR): shutil.rmtree(OUTPUT_DIR) os.makedirs(OUTPUT_DIR) ret, local_file = misc.download_from_s3(BUCKET_NAME, 'videos/video_1.avi', OUTPUT_DIR) print ret, local_file ''' #local_file = '../videos/video_100_frames_2.mp4' local_file = '../videos/video_100_frames_1.mp4' #fd = FaceDetectionV1() fd = FaceDetection() report = fd.process(local_file, show_frame=False) pprint.pprint(report)