Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit test.test. Update requirements. Relative paths in anti_spoof_pre… #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions src/anti_spoof_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# @Software : PyCharm

import os
import traceback

import cv2
import math
import torch
Expand All @@ -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

Expand Down Expand Up @@ -101,3 +107,4 @@ def predict(self, img, model_path):




28 changes: 4 additions & 24 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__":
Expand Down