backend.components.camera_verification.faceid.faceidService

 1import face_recognition
 2
 3from backend.components.camera_verification.qrcode.qrcodeService import MultipleCodesError
 4from backend.database.models import Worker
 5from backend.components.workers.workerService import get_worker_embedding
 6
 7def verify_worker_face(worker: Worker, checked_image) -> list:
 8    '''
 9    Verify if the scanned face indeed belongs to the worker decoded from the QR code.
10
11    **Parameters**:
12    - `worker` (Worker): Worker detected from the QR code.
13    - `checked_image` (ndarray): Image decoded to ndarray.
14    
15    **Returns**:
16    - `List` - indices of faces that match
17
18    **Raises**:
19    - `MultipleWorkersError` - More than one face detected on the image.
20    - `NoFacesFoundError` - No faces found on the image
21    - `FaceNotMatchingError` - The face on the image and the worker face do not
22    - `FaceIDError` - Generic, uncaught error in face recognition library.
23
24    '''
25    # todo: moze encoding powinien być przechowywany w db zamiast obrazka?
26    # TODO: Może być więcej niż 1 zdjęcie! Możemy to wykorzystać do poprawy dokładności
27    original_image_embedding = get_worker_embedding(worker)
28    try: checked_face_embedding = face_recognition.face_encodings(checked_image)
29    except Exception as e:
30        raise FaceIDError(str(e))
31
32    if len(checked_face_embedding) >1:
33        raise MultipleWorkersError("Wykryto więcej niż jednego pracownika.")
34
35    if not checked_face_embedding or len(checked_face_embedding) == 0:
36        raise NoFacesFoundError("Nie znaleziono twarzy.")
37
38    try: faces_match = face_recognition.compare_faces(original_image_embedding, checked_face_embedding)
39    except Exception as e:
40        raise FaceIDError(str(e))
41
42    if not faces_match[0]:
43        raise FaceNotMatchingError("Niezgodność zeskanowanej twarzy")
44
45    return faces_match
46
47
48class FaceIDError(Exception):
49    """
50    Base class for face id errors
51    """
52    pass
53
54class MultipleWorkersError(FaceIDError):
55    """
56    Raised when more than one worker have been detected.
57    """
58    pass
59
60class FaceNotMatchingError(FaceIDError):
61    """
62    Raised when detected face does not match with the one in database.
63    """
64    pass
65
66class NoFacesFoundError(FaceIDError):
67    """
68    Raised when no faces were detected.
69    """
70    pass
def verify_worker_face(worker: backend.database.models.Worker, checked_image) -> list:
 8def verify_worker_face(worker: Worker, checked_image) -> list:
 9    '''
10    Verify if the scanned face indeed belongs to the worker decoded from the QR code.
11
12    **Parameters**:
13    - `worker` (Worker): Worker detected from the QR code.
14    - `checked_image` (ndarray): Image decoded to ndarray.
15    
16    **Returns**:
17    - `List` - indices of faces that match
18
19    **Raises**:
20    - `MultipleWorkersError` - More than one face detected on the image.
21    - `NoFacesFoundError` - No faces found on the image
22    - `FaceNotMatchingError` - The face on the image and the worker face do not
23    - `FaceIDError` - Generic, uncaught error in face recognition library.
24
25    '''
26    # todo: moze encoding powinien być przechowywany w db zamiast obrazka?
27    # TODO: Może być więcej niż 1 zdjęcie! Możemy to wykorzystać do poprawy dokładności
28    original_image_embedding = get_worker_embedding(worker)
29    try: checked_face_embedding = face_recognition.face_encodings(checked_image)
30    except Exception as e:
31        raise FaceIDError(str(e))
32
33    if len(checked_face_embedding) >1:
34        raise MultipleWorkersError("Wykryto więcej niż jednego pracownika.")
35
36    if not checked_face_embedding or len(checked_face_embedding) == 0:
37        raise NoFacesFoundError("Nie znaleziono twarzy.")
38
39    try: faces_match = face_recognition.compare_faces(original_image_embedding, checked_face_embedding)
40    except Exception as e:
41        raise FaceIDError(str(e))
42
43    if not faces_match[0]:
44        raise FaceNotMatchingError("Niezgodność zeskanowanej twarzy")
45
46    return faces_match

Verify if the scanned face indeed belongs to the worker decoded from the QR code.

Parameters:

  • worker (Worker): Worker detected from the QR code.
  • checked_image (ndarray): Image decoded to ndarray.

Returns:

  • List - indices of faces that match

Raises:

class FaceIDError(builtins.Exception):
49class FaceIDError(Exception):
50    """
51    Base class for face id errors
52    """
53    pass

Base class for face id errors

class MultipleWorkersError(FaceIDError):
55class MultipleWorkersError(FaceIDError):
56    """
57    Raised when more than one worker have been detected.
58    """
59    pass

Raised when more than one worker have been detected.

class FaceNotMatchingError(FaceIDError):
61class FaceNotMatchingError(FaceIDError):
62    """
63    Raised when detected face does not match with the one in database.
64    """
65    pass

Raised when detected face does not match with the one in database.

class NoFacesFoundError(FaceIDError):
67class NoFacesFoundError(FaceIDError):
68    """
69    Raised when no faces were detected.
70    """
71    pass

Raised when no faces were detected.