Jonny001 commited on
Commit
af24524
1 Parent(s): 1a975b3

Update roop/face_analyser.py

Browse files
Files changed (1) hide show
  1. roop/face_analyser.py +41 -10
roop/face_analyser.py CHANGED
@@ -1,5 +1,5 @@
1
  import threading
2
- from typing import Any
3
  import insightface
4
 
5
  import roop.globals
@@ -10,25 +10,56 @@ THREAD_LOCK = threading.Lock()
10
 
11
 
12
  def get_face_analyser() -> Any:
 
 
 
 
 
 
 
 
 
13
  global FACE_ANALYSER
14
 
15
  with THREAD_LOCK:
16
  if FACE_ANALYSER is None:
17
- FACE_ANALYSER = insightface.app.FaceAnalysis(name='buffalo_l', providers=roop.globals.execution_providers)
 
 
 
18
  FACE_ANALYSER.prepare(ctx_id=0, det_size=(640, 640))
19
  return FACE_ANALYSER
20
 
21
 
22
- def get_one_face(frame: Frame) -> Any:
23
- face = get_face_analyser().get(frame)
24
- try:
25
- return min(face, key=lambda x: x.bbox[0])
26
- except ValueError:
 
 
 
 
 
 
 
27
  return None
28
 
 
29
 
30
- def get_many_faces(frame: Frame) -> Any:
 
 
 
 
 
 
 
 
 
 
31
  try:
32
  return get_face_analyser().get(frame)
33
- except IndexError:
34
- return None
 
 
1
  import threading
2
+ from typing import Any, List, Optional
3
  import insightface
4
 
5
  import roop.globals
 
10
 
11
 
12
  def get_face_analyser() -> Any:
13
+ """
14
+ Initialize and return the face analyser.
15
+
16
+ This function ensures that the face analyser is initialized only once,
17
+ using a thread-safe mechanism.
18
+
19
+ Returns:
20
+ Any: An instance of the FaceAnalysis class from insightface.
21
+ """
22
  global FACE_ANALYSER
23
 
24
  with THREAD_LOCK:
25
  if FACE_ANALYSER is None:
26
+ FACE_ANALYSER = insightface.app.FaceAnalysis(
27
+ name='buffalo_l',
28
+ providers=roop.globals.execution_providers
29
+ )
30
  FACE_ANALYSER.prepare(ctx_id=0, det_size=(640, 640))
31
  return FACE_ANALYSER
32
 
33
 
34
+ def get_one_face(frame: Frame) -> Optional[Any]:
35
+ """
36
+ Extract the face with the smallest bounding box from the given frame.
37
+
38
+ Args:
39
+ frame (Frame): The image or video frame to analyze.
40
+
41
+ Returns:
42
+ Optional[Any]: The face with the smallest bounding box or None if no face is found.
43
+ """
44
+ faces = get_face_analyser().get(frame)
45
+ if not faces:
46
  return None
47
 
48
+ return min(faces, key=lambda x: x.bbox[0])
49
 
50
+
51
+ def get_many_faces(frame: Frame) -> List[Any]:
52
+ """
53
+ Extract all detected faces from the given frame.
54
+
55
+ Args:
56
+ frame (Frame): The image or video frame to analyze.
57
+
58
+ Returns:
59
+ List[Any]: A list of detected faces or an empty list if no faces are found.
60
+ """
61
  try:
62
  return get_face_analyser().get(frame)
63
+ except Exception as e:
64
+ print(f"Error getting faces: {e}")
65
+ return []