def dl_face_spoof_detect(image, model_dir, model_test, image_cropper, img_heights, exact_thresh): temp = image image, image_bbox = faceboxes_detect(temp, img_heights, exact_thresh) # image, image_bbox = model_test.get_bbox(temp) if image is None: # image, image_bbox = model_test.get_bbox(temp) # if image is None: return False, -1, image, image_bbox prediction = np.zeros((1, 3)) test_speed = 0 # sum the prediction from single model's result for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name(model_name) param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) start = time.time() prediction += model_test.predict(img, os.path.join(model_dir, model_name)) test_speed += time.time() - start print("Prediction cost {:.2f} s".format(test_speed)) label = np.argmax(prediction) value = prediction[0][label] / 2 print("confidence=", value) if label == 1: return False, value, image, image_bbox return True, value, image, image_bbox
def _load_model(self, model_path): # define model model_name = os.path.basename(model_path) h_input, w_input, model_type, _ = parse_model_name(model_name) self.kernel_size = get_kernel( h_input, w_input, ) self.model = MODEL_MAPPING[model_type]( conv6_kernel=self.kernel_size).to(self.device) # load model weight state_dict = torch.load(model_path, map_location=self.device) keys = iter(state_dict) first_layer_name = keys.__next__() if first_layer_name.find('module.') >= 0: from collections import OrderedDict new_state_dict = OrderedDict() for key, value in state_dict.items(): name_key = key[7:] new_state_dict[name_key] = value self.model.load_state_dict(new_state_dict) else: self.model.load_state_dict(state_dict) return None
def liveness_detector(frame): image_cropper = CropImage() model_dir = './resources/liveness_model' image_bbox = model_test.get_bbox(frame) if image_bbox[0] == 0 and image_bbox[1] == 0 and image_bbox[2] == 1 and image_bbox[3] == 1: return False prediction = np.zeros((1, 3)) test_speed = 0 # sum the prediction from single model's result for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name(model_name) param = { "org_img": frame, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) prediction += model_test.predict(img, os.path.join(model_dir, model_name)) # label: face is true or fake label = np.argmax(prediction) # value: the score of prediction value = prediction[0][label] if label == 1 and value > 0.7: return True else: return False
def preprocessing(model_dir, device_id, num_classes, src_dir, dst_dir, threshold): face_model = FaceModel() model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() onlyfiles = [ os.path.join(path, name) for path, subdirs, files in os.walk(src_dir) for name in files ] for file_path in onlyfiles: file_name = os.path.basename(file_path) image = cv2.imread(file_path) image_bbox = face_model.get_bbox(image) if image_bbox == [0, 0, 1, 1]: dst_path_image = join(dst_dir, "not_detect_face") if not exists(dst_path_image): os.makedirs(dst_path_image) cv2.imwrite(join(dst_path_image, file_name), image) else: image_cropped = [] prediction = np.zeros((1, num_classes)) count_model = 0 for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name( model_name) param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) image_cropped.append({"scale": str(scale), "image": img}) if threshold > 0: prediction += model_test.predict( img, os.path.join(model_dir, model_name)) count_model = count_model + 1 directory = dst_dir if threshold > 0: label = np.argmax(prediction) value = prediction[0][label] / count_model directory = join(dst_dir, str(label)) if (threshold > 0 and value >= threshold) or (threshold == 0): for cropped in image_cropped: dst_path_image = join(directory, cropped["scale"]) if not exists(dst_path_image): os.makedirs(dst_path_image) cv2.imwrite(join(dst_path_image, file_name), cropped["image"])
def test(image_name, model_dir, device_id): model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() image = cv2.imread(SAMPLE_IMAGE_PATH + image_name) result = check_image(image) if result is False: return image_bbox = model_test.get_bbox(image) prediction = np.zeros((1, 3)) test_speed = 0 # sum the prediction from single model's result for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name(model_name) param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) start = time.time() prediction += model_test.predict(img, os.path.join(model_dir, model_name)) test_speed += time.time()-start # draw result of prediction label = np.argmax(prediction) value = prediction[0][label]/2 if label == 1: print("Image '{}' is Real Face. Score: {:.2f}.".format(image_name, value)) result_text = "RealFace Score: {:.2f}".format(value) color = (255, 0, 0) else: print("Image '{}' is Fake Face. Score: {:.2f}.".format(image_name, value)) result_text = "FakeFace Score: {:.2f}".format(value) color = (0, 0, 255) print("Prediction cost {:.2f} ms".format(test_speed)) cv2.rectangle( image, (image_bbox[0], image_bbox[1]), (image_bbox[0] + image_bbox[2], image_bbox[1] + image_bbox[3]), color, 2) cv2.putText( image, result_text, (image_bbox[0], image_bbox[1] - 5), cv2.FONT_HERSHEY_COMPLEX, 0.5*image.shape[0]/1024, color) format_ = os.path.splitext(image_name)[-1] result_image_name = image_name.replace(format_, "_result" + format_) cv2.imwrite(SAMPLE_IMAGE_PATH + result_image_name, image)
def test(image_name, model_dir, device_id): ## If input is an image from a folder ## # image_name = cv2.imread(image_name) model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() image_bbox = model_test.get_bbox(image_name) prediction = np.zeros((1, 3)) test_speed = 0 for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name(model_name) param = { "org_img": image_name, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) start = time.time() prediction += model_test.predict(img, os.path.join(model_dir, model_name)) test_speed += time.time() - start # draw result of prediction label = np.argmax(prediction) value = prediction[0][label] / 2 if label == 1 and value >= 0.6: print("Real Face.") result_text = "RealFace" color = (255, 0, 0) else: print("Fake Face.") result_text = "FakeFace" color = (0, 0, 255) print("Prediction speed {:.2f} s".format(test_speed))
def __init__(self): super(AntiSpoofPredict, self).__init__() self.num_class = 2 self.model_path = "/home/ubuntu/Silent-Face-Anti-Spoofing-master/saved_logs/snapshot/Anti_Spoofing_1_80-80/2021-01-11-13-19_Anti_Spoofing_1_80-80_model_iter-39500.pth" model_name = os.path.basename(self.model_path) h_input, w_input, model_type, _ = parse_model_name(model_name) self.kernel_size = get_kernel( h_input, w_input, ) self.model = MODEL_MAPPING["MiniFASNetV2SE"]( conv6_kernel=self.kernel_size, num_classes=self.num_class) checkpoint = torch.load(self.model_path, map_location="cuda:3") pretrain(self.model, checkpoint) self.new_width = self.new_height = 224 self.transform = torchvision.transforms.Compose([ torchvision.transforms.Resize((self.new_width, self.new_height)), torchvision.transforms.ToTensor(), ]) device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu") self.model.to(device) self.model.eval()
def _load_model(self, model_path): # define model model_name = os.path.basename(model_path) h_input, w_input, model_type, _ = parse_model_name(model_name) self.kernel_size = get_kernel( h_input, w_input, ) if model_type not in MODEL_MAPPING: params = { 'embedding_size': 128, 'conv6_kernel': (5, 5), 'num_classes': 2, 'img_channel': 3, 'training': False } self.model = MultiFTNet(**params).to(self.device) else: self.model = MODEL_MAPPING[model_type]( conv6_kernel=self.kernel_size).to(self.device) # load model weight state_dict = torch.load(model_path, map_location=self.device) keys = iter(state_dict) first_layer_name = keys.__next__() if first_layer_name.find('module.') >= 0: from collections import OrderedDict new_state_dict = OrderedDict() for key, value in state_dict.items(): name_key = key[7:] new_state_dict[name_key] = value self.model.load_state_dict(new_state_dict) else: self.model.load_state_dict(state_dict) return None
def webcam(model_dir, device_id): model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() color = (255, 0, 0) cap = cv2.VideoCapture(0) print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe('deploy.prototxt.txt', 'res10_300x300_ssd_iter_140000.caffemodel') print("[INFO] starting video stream...") max_num_faces = 20 prev_frame_time = 0 while True: ret, frame = cap.read() if ret is None: break (h, w) = frame.shape[:2] frame = imutils.resize(frame, width=640) new_frame_time = time.time() ### Use mobileFacenet # image_bbox = model_test.get_bbox(frame) # print(image_bbox) blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) net.setInput(blob) detections = net.forward() model_name = '2.7_80x80_MiniFASNetV2.pth' for i in range(0, max_num_faces): confidence = detections[0, 0, i, 2] if confidence < 0.5: continue prediction = np.zeros((1, 3)) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") image_bbox = (startX, startY, endX - startX, endY - startY) start_time = time.time() for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name( model_name) param = { "org_img": frame, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) prediction += model_test.predict( img, os.path.join(model_dir, model_name)) label = np.argmax(prediction) value = prediction[0][label] / 2 # print(prediction[0][label]) if label == 1: # print("Image '{}' is Real Face. Score: {:.2f}.".format(image_name, value)) result_text = "RealFace Score: {:.2f}".format(value) color = (255, 0, 0) else: # print("Image '{}' is Fake Face. Score: {:.2f}.".format(image_name, value)) result_text = "FakeFace Score: {:.2f}".format(value) color = (0, 0, 255) end_time = time.time() cv2.rectangle( frame, (image_bbox[0], image_bbox[1]), (image_bbox[0] + image_bbox[2], image_bbox[1] + image_bbox[3]), color, 2) cv2.putText(frame, result_text, (image_bbox[0], image_bbox[1] - 5), cv2.FONT_HERSHEY_COMPLEX, 1 * frame.shape[0] / 1024, color) fps = 1 / (end_time - start_time) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(frame, str(fps), (7, 70), font, 3, (100, 255, 0), 3, cv2.LINE_AA) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break
description='face anti spoofing proto code') parser.add_argument('video', help='video file name.') parser.add_argument('--gpu', default=0, type=int, help='gpu id.') parser.add_argument('--rotate', action='store_true', help='rotate frame.') args = parser.parse_args() # RetinaFace Detector detector = insightface.model_zoo.get_model( 'retinaface_mnet025_v2') # can replace with your own face detector # self.detector = insightface.model_zoo.get_model('retinaface_r50_v1') detector.prepare(ctx_id=args.gpu) # Anti Spoof Classifier model_test = AntiSpoofPredict(args.gpu) image_cropper = CropImage() h_input, w_input, model_type, scale = parse_model_name( '2.7_80x80_MiniFASNetV2.pth') # Start pipeline video_capture = cv2.VideoCapture(args.video) fps = video_capture.get(cv2.CAP_PROP_FPS) width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) total = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT)) fourcc = 'MJPG' size = (height, width) if args.rotate else (width, height) video_writer = cv2.VideoWriter( args.video.replace('mp4', 'avi').replace('mov', 'avi'), cv2.VideoWriter_fourcc(*fourcc), fps, size) while True: success, frame = video_capture.read() if not success:
def func(): data = request.json urls = list(data["urls"]) unknown_url = data["unknown_url"] faces = 0 label = '' resp = urllib.request.urlopen(unknown_url) image = np.asarray(bytearray(resp.read()), dtype="uint8") frame = cv2.imdecode(image, cv2.IMREAD_COLOR) image = frame image_bbox, faces = model_test.get_bbox(image) if (faces == 1): prediction = np.zeros((1, 3)) for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name(model_name) param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) prediction += model_test.predict( img, os.path.join(model_dir, model_name)) l = np.argmax(prediction) value = prediction[0][l] / 2 if l == 1: label = 'true' else: label = 'fake' error = -1 body = "False" message = "" if (faces == 0): error = 1 body = "Error" message = "No Face Detected" elif (faces > 1): error = 2 body = "Error" message = "Multiple Faces Detected" elif (faces == 1 and label == 'fake'): error = 3 body = "Error" message = "Only 1 Face Detected but is fake" elif (faces == 1 and label == 'true'): boxes = face_recognition.face_locations(image) if (len(boxes) == 0): message = "No Face Detected" error = 1 body = "Error" # return False, error, msg else: unknown = face_recognition.face_encodings(image, boxes)[0] for im_path in urls: resp = urllib.request.urlopen(im_path) image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) boxes = face_recognition.face_locations(image) if (len(boxes) == 0): continue known = face_recognition.face_encodings(image, boxes)[0] matches = face_recognition.compare_faces([unknown], known) if (matches[0] == True): error = -1 message = "Validated" body = "True" break if (body != "True"): error = 4 message = "Face not Validated" body = "Error" return jsonify(statusCode=200, body=body, error=error, message=message)
def test(model_dir, device_id, num_classes, src_dir, dst_dir, draw_bbox): face_model = FaceModel() model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() onlyfiles = [ os.path.join(path, name) for path, subdirs, files in os.walk(src_dir) for name in files ] for file_path in onlyfiles: image_name = os.path.basename(file_path) image = cv2.imread(file_path) image_bbox = face_model.get_bbox(image) print(image_bbox) if image_bbox == [0, 0, 1, 1]: dst_path_image = join(dst_dir, "not_detect_face") if not exists(dst_path_image): os.makedirs(dst_path_image) cv2.imwrite(join(dst_path_image, image_name), image) else: # if you have n clasees => prediction = np.zeros((1, n)) prediction = np.zeros((1, num_classes)) test_speed = 0 # sum the prediction from single model's result count_model = 0 for model_name in os.listdir(model_dir): h_input, w_input, model_type, scale = parse_model_name( model_name) param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": True, } if scale is None: param["crop"] = False img = image_cropper.crop(**param) start = time.time() prediction += model_test.predict( img, os.path.join(model_dir, model_name)) count_model = count_model + 1 test_speed += time.time() - start # draw result of prediction label = np.argmax(prediction) value = prediction[0][label] / count_model if label == 1: label_text = "Image '{}' is Real Face. Score: {:.2f}.".format( image_name, value) result_text = "RealFace Score: {:.2f}".format(value) color = (255, 0, 0) else: label_text = "Image '{}' is Fake Face. Score: {:.2f}.".format( image_name, value) result_text = "FakeFace Score: {:.2f}".format(value) color = (0, 0, 255) print(label_text) print("Prediction cost {:.2f} s".format(test_speed)) if draw_bbox == True: cv2.rectangle(image, (image_bbox[0], image_bbox[1]), (image_bbox[0] + image_bbox[2], image_bbox[1] + image_bbox[3]), color, 2) cv2.putText(image, result_text, (image_bbox[0], image_bbox[1] - 5), cv2.FONT_HERSHEY_COMPLEX, 0.5 * image.shape[0] / 1024, color) dst_path_image = join(dst_dir, str(label)) if not exists(dst_path_image): os.makedirs(dst_path_image) format_ = os.path.splitext(image_name)[-1] result_image_name = image_name.replace(format_, "_result" + format_) cv2.imwrite(os.path.join(dst_path_image, result_image_name), image)