File size: 1,633 Bytes
9067733 03d287b 9067733 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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)
|