Makhinur commited on
Commit
a8db7a5
·
verified ·
1 Parent(s): 710b074

Update face_detection.py

Browse files
Files changed (1) hide show
  1. face_detection.py +23 -6
face_detection.py CHANGED
@@ -4,17 +4,29 @@ from PIL import Image, ImageOps
4
  import cv2
5
 
6
  # Load InsightFace models
7
- detector = insightface.model_zoo.get_model('retinaface_r50_v1')
8
- detector.prepare(ctx_id=-1) # Use CPU, set ctx_id=0 for GPU
9
-
10
- landmark_model = insightface.model_zoo.get_model('2d106det')
11
- landmark_model.prepare(ctx_id=-1) # Use CPU, set ctx_id=0 for GPU
 
 
 
 
 
 
 
 
12
 
13
  def align(image_in, face_index=0, output_size=256):
 
 
 
 
14
  try:
15
  image_in = ImageOps.exif_transpose(image_in)
16
  except:
17
- print("exif problem, not rotating")
18
 
19
  landmarks = list(get_landmarks(image_in))
20
  n_faces = len(landmarks)
@@ -29,6 +41,9 @@ def align(image_in, face_index=0, output_size=256):
29
 
30
  def get_landmarks(image):
31
  """Get landmarks from PIL image"""
 
 
 
32
  img = np.array(image)
33
  bboxes, _ = detector.detect(img, threshold=0.5, scale=1.0)
34
 
@@ -37,6 +52,8 @@ def get_landmarks(image):
37
  landmarks = landmark_model.get(img, bbox)
38
  yield landmarks
39
 
 
 
40
  def image_align(src_img, face_landmarks, output_size=512, transform_size=2048, enable_padding=True, x_scale=1, y_scale=1, em_scale=0.1, alpha=False):
41
  # Align function modified from ffhq-dataset
42
  # See https://github.com/NVlabs/ffhq-dataset for license
 
4
  import cv2
5
 
6
  # Load InsightFace models
7
+ try:
8
+ detector = insightface.model_zoo.get_model('retinaface_r50_v1')
9
+ detector.prepare(ctx_id=-1) # Use CPU, set ctx_id=0 for GPU
10
+ except Exception as e:
11
+ print(f"Error loading face detection model: {e}")
12
+ detector = None
13
+
14
+ try:
15
+ landmark_model = insightface.model_zoo.get_model('2d106det')
16
+ landmark_model.prepare(ctx_id=-1) # Use CPU, set ctx_id=0 for GPU
17
+ except Exception as e:
18
+ print(f"Error loading landmark model: {e}")
19
+ landmark_model = None
20
 
21
  def align(image_in, face_index=0, output_size=256):
22
+ if detector is None or landmark_model is None:
23
+ print("Models not loaded properly.")
24
+ return image_in, 0, None
25
+
26
  try:
27
  image_in = ImageOps.exif_transpose(image_in)
28
  except:
29
+ print("Exif problem, not rotating")
30
 
31
  landmarks = list(get_landmarks(image_in))
32
  n_faces = len(landmarks)
 
41
 
42
  def get_landmarks(image):
43
  """Get landmarks from PIL image"""
44
+ if detector is None or landmark_model is None:
45
+ return []
46
+
47
  img = np.array(image)
48
  bboxes, _ = detector.detect(img, threshold=0.5, scale=1.0)
49
 
 
52
  landmarks = landmark_model.get(img, bbox)
53
  yield landmarks
54
 
55
+ # Remaining functions remain unchanged
56
+
57
  def image_align(src_img, face_landmarks, output_size=512, transform_size=2048, enable_padding=True, x_scale=1, y_scale=1, em_scale=0.1, alpha=False):
58
  # Align function modified from ffhq-dataset
59
  # See https://github.com/NVlabs/ffhq-dataset for license