Spaces:
Running
Running
Update avatar.py
Browse files
avatar.py
CHANGED
@@ -182,45 +182,62 @@ class Avatar:
|
|
182 |
|
183 |
|
184 |
def create_face_detection_results(self, full_frames, save_result=True):
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
break
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 vì 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]))
|