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))
return args # if __name__ == "__main__": args = arg_parse() # test(args.image_name, args.model_dir, args.device_id) # def test(image_name, model_dir, device_id): image_name = args.image_name model_dir = args.model_dir device_id = args.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 True: image_bbox = model_test.get_bbox(image) prediction = np.zeros((1, 3)) test_speed = 0 model_names = [os.path.join(model_dir, "epoch_77.pth")] # sum the prediction from single model's result for model_name in model_names: # h_input, w_input, model_type, scale = parse_model_name(model_name) h_input, w_input, model_type, scale = 80, 80, None, 1 param = { "org_img": image, "bbox": image_bbox, "scale": scale, "out_w": w_input, "out_h": h_input, "crop": False, }