PULKIT52 commited on
Commit
249be44
·
verified ·
1 Parent(s): bb5f4d1

Update hallo/datasets/image_processor.py

Browse files
Files changed (1) hide show
  1. hallo/datasets/image_processor.py +29 -7
hallo/datasets/image_processor.py CHANGED
@@ -9,13 +9,21 @@ import os
9
  from typing import List
10
 
11
  import cv2
12
- import mediapipe as mp
13
  import numpy as np
14
  import torch
15
- from insightface.app import FaceAnalysis
16
  from PIL import Image
17
  from torchvision import transforms
18
 
 
 
 
 
 
 
 
 
 
 
19
  from ..utils.util import (blur_mask, get_landmark_overframes, get_mask,
20
  get_union_face_mask, get_union_lip_mask)
21
 
@@ -51,6 +59,11 @@ class ImageProcessor:
51
  Exits a runtime context and handles any exceptions that occurred during the processing.
52
  """
53
  def __init__(self, img_size, face_analysis_model_path) -> None:
 
 
 
 
 
54
  self.img_size = img_size
55
 
56
  self.pixel_transform = transforms.Compose(
@@ -242,6 +255,10 @@ class ImageProcessorForDataProcessing():
242
  """
243
  def __init__(self, face_analysis_model_path, landmark_model_path, step) -> None:
244
  if step == 2:
 
 
 
 
245
  self.face_analysis = FaceAnalysis(
246
  name="",
247
  root=face_analysis_model_path,
@@ -250,6 +267,10 @@ class ImageProcessorForDataProcessing():
250
  self.face_analysis.prepare(ctx_id=0, det_size=(640, 640))
251
  self.landmarker = None
252
  else:
 
 
 
 
253
  BaseOptions = mp.tasks.BaseOptions
254
  FaceLandmarker = mp.tasks.vision.FaceLandmarker
255
  FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
@@ -273,7 +294,7 @@ class ImageProcessorForDataProcessing():
273
  Returns:
274
  None
275
  """
276
- # 1. get face embdeding
277
  face_mask, face_emb, sep_pose_mask, sep_face_mask, sep_lip_mask = None, None, None, None, None
278
  if self.face_analysis:
279
  for frame in sorted(os.listdir(source_image_path)):
@@ -325,9 +346,10 @@ class ImageProcessorForDataProcessing():
325
  Returns:
326
  None.
327
  """
328
- for _, model in self.face_analysis.models.items():
329
- if hasattr(model, "Dispose"):
330
- model.Dispose()
 
331
 
332
  def _augmentation(self, images, transform, state=None):
333
  if state is not None:
@@ -343,4 +365,4 @@ class ImageProcessorForDataProcessing():
343
  return self
344
 
345
  def __exit__(self, _exc_type, _exc_val, _exc_tb):
346
- self.close()
 
9
  from typing import List
10
 
11
  import cv2
 
12
  import numpy as np
13
  import torch
 
14
  from PIL import Image
15
  from torchvision import transforms
16
 
17
+ try:
18
+ import mediapipe as mp
19
+ except Exception:
20
+ mp = None
21
+
22
+ try:
23
+ from insightface.app import FaceAnalysis
24
+ except Exception:
25
+ FaceAnalysis = None
26
+
27
  from ..utils.util import (blur_mask, get_landmark_overframes, get_mask,
28
  get_union_face_mask, get_union_lip_mask)
29
 
 
59
  Exits a runtime context and handles any exceptions that occurred during the processing.
60
  """
61
  def __init__(self, img_size, face_analysis_model_path) -> None:
62
+ if FaceAnalysis is None:
63
+ raise ImportError(
64
+ "insightface is not installed. Install it with: pip install insightface"
65
+ )
66
+
67
  self.img_size = img_size
68
 
69
  self.pixel_transform = transforms.Compose(
 
255
  """
256
  def __init__(self, face_analysis_model_path, landmark_model_path, step) -> None:
257
  if step == 2:
258
+ if FaceAnalysis is None:
259
+ raise ImportError(
260
+ "insightface is not installed. Install it with: pip install insightface"
261
+ )
262
  self.face_analysis = FaceAnalysis(
263
  name="",
264
  root=face_analysis_model_path,
 
267
  self.face_analysis.prepare(ctx_id=0, det_size=(640, 640))
268
  self.landmarker = None
269
  else:
270
+ if mp is None:
271
+ raise ImportError(
272
+ "mediapipe is not installed. Install it with: pip install mediapipe"
273
+ )
274
  BaseOptions = mp.tasks.BaseOptions
275
  FaceLandmarker = mp.tasks.vision.FaceLandmarker
276
  FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
 
294
  Returns:
295
  None
296
  """
297
+ # 1. get face embedding
298
  face_mask, face_emb, sep_pose_mask, sep_face_mask, sep_lip_mask = None, None, None, None, None
299
  if self.face_analysis:
300
  for frame in sorted(os.listdir(source_image_path)):
 
346
  Returns:
347
  None.
348
  """
349
+ if self.face_analysis is not None:
350
+ for _, model in self.face_analysis.models.items():
351
+ if hasattr(model, "Dispose"):
352
+ model.Dispose()
353
 
354
  def _augmentation(self, images, transform, state=None):
355
  if state is not None:
 
365
  return self
366
 
367
  def __exit__(self, _exc_type, _exc_val, _exc_tb):
368
+ self.close()