Download DeePixBiS/Test.py from ozyman/fasd: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/ozyman/fasd/resolve/main/DeePixBiS/Test.py
- Command line
-
hf download hf://spaces/ozyman/fasd/DeePixBiS/Test.py
-
curl -L -o Test.py https://huggingface.co/spaces/ozyman/fasd/resolve/main/DeePixBiS/Test.py
1.63 kB
| import cv2 as cv | |
| import torch | |
| import torch.nn as nn | |
| from torchvision import transforms | |
| import numpy as np | |
| from DeePixBis.Model import DeePixBiS | |
| from DeePixBis.Loss import PixWiseBCELoss | |
| from DeePixBis.Metrics import predict, test_accuracy, test_loss | |
| model = DeePixBiS(pretrained=False) | |
| model.load_state_dict(torch.load('./DeePixBiS.pth')) | |
| model.eval() | |
| tfms = transforms.Compose([ | |
| transforms.ToPILImage(), | |
| transforms.Resize((224, 224)), | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) | |
| ]) | |
| faceClassifier = cv.CascadeClassifier('Classifiers/haarface.xml') | |
| camera = cv.VideoCapture(0) | |
| while cv.waitKey(1) & 0xFF != ord('q'): | |
| _, img = camera.read() | |
| grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY) | |
| faces = faceClassifier.detectMultiScale(grey, scaleFactor=1.1, minNeighbors=4) | |
| for x, y, w, h in faces: | |
| faceRegion = img[y:y + h, x:x + w] | |
| faceRegion = cv.cvtColor(faceRegion, cv.COLOR_BGR2RGB) | |
| # cv.imshow('Test', faceRegion) | |
| faceRegion = tfms(faceRegion) | |
| faceRegion = faceRegion.unsqueeze(0) | |
| mask, binary = model.forward(faceRegion) | |
| res = torch.mean(mask).item() | |
| # res = binary.item() | |
| if res < 0.7: | |
| label = f'Spoof {res:.2f}' | |
| color = (0, 0, 255) | |
| else: | |
| label = f'Real {res:.2f}' | |
| color = (0, 255, 0) | |
| print(f'{label}: {res}') | |
| cv.rectangle(img, (x, y), (x + w, y + h), color, 2) | |
| cv.putText(img, label, (x, y + h + 30), cv.FONT_HERSHEY_COMPLEX, 1, color) | |
| cv.imshow('Deep Pixel-wise Binary Supervision Anti-Spoofing', img) | |