TDN-M commited on
Commit
939a8b2
·
verified ·
1 Parent(s): cb87636

Update avatar.py

Browse files
Files changed (1) hide show
  1. avatar.py +54 -37
avatar.py CHANGED
@@ -182,45 +182,62 @@ class Avatar:
182
 
183
 
184
  def create_face_detection_results(self, full_frames, save_result=True):
185
- detector = FaceAlignment(LandmarksType.TWO_D, flip_input=False, device=self.device)
186
- images = full_frames
187
- while 1:
188
- predictions = []
189
- try:
190
- for i in tqdm(range(0, len(images), self.face_detect_batch_size)):
191
- batch_images = np.array(images[i:i + self.face_detect_batch_size])
192
- batch_images = torch.from_numpy(batch_images).permute(0, 3, 1, 2).float().to(self.device)
193
- predictions.extend(detector.face_detector.detect_from_batch(batch_images))
194
- except RuntimeError:
195
- if self.face_detect_batch_size == 1:
196
- raise RuntimeError('Image too big to run face detection on GPU. Please use the --resize_factor argument')
197
- self.face_detect_batch_size //= 2
198
- print('Recovering from OOM error; New batch size: {}'.format(self.face_detect_batch_size))
199
- continue
200
- break
201
 
202
- face_detect_results = []
203
- pady1, pady2, padx1, padx2 = [0, 10, 0, 0]
204
- for rect, image in zip(predictions, images):
205
- # Kiểm tra nếu rect là None hoặc không đủ phần tử
206
- if rect is None or len(rect) < 4:
207
- # Bỏ qua frame này và tiếp tục với frame tiếp theo
208
- face_detect_results.append(None)
209
- continue
210
-
211
- try:
212
- y1 = max(0, rect[1] - pady1)
213
- y2 = min(image.shape[0], rect[3] + pady2)
214
- x1 = max(0, rect[0] - padx1)
215
- x2 = min(image.shape[1], rect[2] + padx2)
 
 
 
 
 
 
 
 
 
216
 
217
- face_detect_results.append([x1, y1, x2, y2])
218
- except (IndexError, TypeError) as e:
219
- print(f"Error processing face detection result: {e}")
220
- face_detect_results.append(None)
221
- continue
222
-
223
- return face_detect_results
 
 
 
 
 
 
 
 
 
224
  # print("\n")
225
  # print("face_detect_results length = " + str(len(face_detect_results)))
226
  # print("face_detect_results[2]="+str(face_detect_results[2]))
 
182
 
183
 
184
  def create_face_detection_results(self, full_frames, save_result=True):
185
+ try:
186
+ from face_alignment import FaceAlignment, LandmarksType
187
+
188
+ # Kiểm tra và thiết lập device
189
+ if not hasattr(self, 'device') or self.device is None:
190
+ self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
191
+
192
+ # Khởi tạo detector với cấu hình đơn giản hơn
193
+ detector = FaceAlignment(
194
+ LandmarksType.TWO_D,
195
+ device=self.device,
196
+ face_detector='sfd' # Sử dụng SFD thay mặc định
197
+ )
198
+
199
+ images = [frame for frame in full_frames]
 
200
 
201
+ # Xử lý batch
202
+ predictions = []
203
+ while True:
204
+ try:
205
+ for i in range(0, len(images), self.face_detect_batch_size):
206
+ batch_images = np.array(images[i:i + self.face_detect_batch_size])
207
+ batch_images = torch.from_numpy(batch_images).permute(0, 3, 1, 2).float().to(self.device)
208
+ predictions.extend(detector.face_detector.detect_from_batch(batch_images))
209
+ break
210
+ except RuntimeError:
211
+ if self.face_detect_batch_size == 1:
212
+ raise RuntimeError('Image too big to run face detection on GPU. Please use the --resize_factor argument')
213
+ self.face_detect_batch_size //= 2
214
+ print(f'Reducing batch size to {self.face_detect_batch_size} due to OOM error')
215
+ continue
216
+
217
+ # Xử lý kết quả
218
+ face_detect_results = []
219
+ pady1, pady2, padx1, padx2 = [0, 10, 0, 0]
220
+ for rect, image in zip(predictions, images):
221
+ if rect is None or len(rect) < 4:
222
+ face_detect_results.append(None)
223
+ continue
224
 
225
+ try:
226
+ y1 = max(0, rect[1] - pady1)
227
+ y2 = min(image.shape[0], rect[3] + pady2)
228
+ x1 = max(0, rect[0] - padx1)
229
+ x2 = min(image.shape[1], rect[2] + padx2)
230
+ face_detect_results.append([x1, y1, x2, y2])
231
+ except (IndexError, TypeError) as e:
232
+ print(f"Error processing face detection result: {e}")
233
+ face_detect_results.append(None)
234
+ continue
235
+
236
+ return face_detect_results
237
+
238
+ except Exception as e:
239
+ print(f"Error in create_face_detection_results: {str(e)}")
240
+ raise
241
  # print("\n")
242
  # print("face_detect_results length = " + str(len(face_detect_results)))
243
  # print("face_detect_results[2]="+str(face_detect_results[2]))