From c654f66e6d1f05b83771b275ff1fba292a053374 Mon Sep 17 00:00:00 2001 From: computervisiondeveloper Date: Wed, 21 Dec 2022 11:08:09 -0300 Subject: [PATCH] Edit test.test. Update requirements. Relative paths in anti_spoof_predict.py. --- requirements.txt | 16 ++++++++-------- src/anti_spoof_predict.py | 11 +++++++++-- test.py | 28 ++++------------------------ 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/requirements.txt b/requirements.txt index 6e1adf5130..c1175ab6fc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -easydict==1.9 -numpy==1.17.0 -tqdm==4.31.1 -torchvision==0.4.0 -torch==1.2.0 -opencv_python==4.2.0.34 -Pillow==7.1.2 -tensorboardX==2.0 +easydict==1.10 +numpy==1.23.5 +tqdm==4.64.1 +torchvision==0.14.1 +torch==1.13.1 +opencv-python==4.6.0.66 +Pillow==9.3.0 +tensorboardX==2.5.1 diff --git a/src/anti_spoof_predict.py b/src/anti_spoof_predict.py index ead08e0df8..4275cac0a5 100644 --- a/src/anti_spoof_predict.py +++ b/src/anti_spoof_predict.py @@ -6,6 +6,8 @@ # @Software : PyCharm import os +import traceback + import cv2 import math import torch @@ -27,8 +29,12 @@ class Detection: def __init__(self): - caffemodel = "./resources/detection_model/Widerface-RetinaFace.caffemodel" - deploy = "./resources/detection_model/deploy.prototxt" + stack = traceback.extract_stack() + dirname = os.path.dirname(stack[-2].filename) + + caffemodel = os.path.join(dirname, '..', 'resources', 'detection_model', 'Widerface-RetinaFace.caffemodel') + deploy = os.path.join(dirname, '..', 'resources', 'detection_model', 'deploy.prototxt') + self.detector = cv2.dnn.readNetFromCaffe(deploy, caffemodel) self.detector_confidence = 0.6 @@ -101,3 +107,4 @@ def predict(self, img, model_path): + diff --git a/test.py b/test.py index 5c48eb1a53..3cb8a3fbc7 100644 --- a/test.py +++ b/test.py @@ -31,10 +31,11 @@ def check_image(image): return True -def test(image_name, model_dir, device_id): +def test(image, model_dir, device_id): model_test = AntiSpoofPredict(device_id) image_cropper = CropImage() - image = cv2.imread(SAMPLE_IMAGE_PATH + image_name) + # image = cv2.imread(SAMPLE_IMAGE_PATH + image_name) + image = cv2.resize(image, (int(image.shape[0] * 3 / 4), image.shape[0])) result = check_image(image) if result is False: return @@ -62,29 +63,8 @@ def test(image_name, model_dir, device_id): # 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} s".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) + return label if __name__ == "__main__":