Advertisement
Guest User

Untitled

a guest
May 19th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import cv2 as cv
  2. import torch
  3. import torch.nn as nn
  4. from torchvision import transforms
  5. import numpy as np
  6. from Model import DeePixBiS
  7. from Loss import PixWiseBCELoss
  8. from Metrics import predict, test_accuracy, test_loss
  9. from rknnlite.api import RKNNLite
  10. rknn_lite = RKNNLite()
  11. ret = rknn_lite.load_rknn('/home/orangepi/Desktop/test-face-id/cpp/test_py/DeePixBiS.rknn')
  12. ret = rknn_lite.init_runtime(core_mask=RKNNLite.NPU_CORE_0_1_2)
  13. tfms = transforms.Compose([
  14.     transforms.ToPILImage(),
  15.     transforms.Resize((224, 224)),
  16.     transforms.ToTensor(),
  17.     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
  18. ])
  19.  
  20. faceClassifier = cv.CascadeClassifier('/home/orangepi/Desktop/test-face-id/cpp/test_py/Classifiers/haarface.xml')
  21.  
  22. camera = cv.VideoCapture(0)
  23.  
  24. while cv.waitKey(1) & 0xFF != ord('q'):
  25.     _, img = camera.read()
  26.     grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  27.     faces = faceClassifier.detectMultiScale(
  28.         grey, scaleFactor=1.1, minNeighbors=4)
  29.  
  30.     for x, y, w, h in faces:
  31.         faceRegion = img[y:y + h, x:x + w]
  32.         faceRegion = cv.cvtColor(faceRegion, cv.COLOR_BGR2RGB)
  33.         faceRegion = tfms(faceRegion)
  34.         faceRegion = faceRegion.unsqueeze(0).detach().cpu().numpy()
  35.  
  36.         mask, binary = rknn_lite.inference([faceRegion.astype(np.float16)])
  37.         res = np.mean(mask.flatten())
  38.         print(res)
  39.         cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
  40.  
  41.         if res < 0.5:
  42.             cv.putText(img, 'Fake', (x, y + h + 30),
  43.                        cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
  44.         else:
  45.             cv.putText(img, 'Real', (x, y + h + 30),
  46.                        cv.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
  47.  
  48.     cv.imshow('Deep Pixel-wise Binary Supervision Anti-Spoofing', img)
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement