TDN-M commited on
Commit
1ad0926
·
verified ·
1 Parent(s): 74c50f3

Update avatar.py

Browse files
Files changed (1) hide show
  1. avatar.py +14 -9
avatar.py CHANGED
@@ -10,8 +10,9 @@ import os
10
  import torch
11
  import io
12
  import soundfile as sf
 
 
13
  from Wav2Lip.models import Wav2Lip
14
- from models import Wav2Lip
15
  import face_detection
16
  import audio
17
 
@@ -244,10 +245,12 @@ class Avatar:
244
  # print ("face_detect_img_results[1][1] shape = " +str(self.face_detect_img_results[1][0].shape)) #this is cropped image
245
 
246
 
247
- def datagen(self, full_frames, mels, face_detect_results,progress_bar):
248
  img_batch, mel_batch, frame_batch, coords_batch = [], [], [], []
249
  print(len(full_frames))
250
- progress_bar.progress(0)
 
 
251
  for i, m in enumerate(mels):
252
  idx = i%len(full_frames)
253
  frame_to_save = full_frames[idx].copy()
@@ -272,8 +275,9 @@ class Avatar:
272
  #print(f"len(img_batch)>{self.datagen_batch_size} now len(img_batch)=" + str(len(img_batch)))
273
  yield img_batch, mel_batch, frame_batch, coords_batch
274
  img_batch, mel_batch, frame_batch, coords_batch = [], [], [], []
275
- progress_percentage = (i + 1) / len(mels)
276
- progress_bar.progress(progress_percentage)
 
277
  if len(img_batch) > 0:
278
  img_batch, mel_batch = np.asarray(img_batch), np.asarray(mel_batch)
279
 
@@ -503,8 +507,6 @@ class Avatar:
503
  self.image_frame_num_current=0
504
  def text_to_lip_video_parallel(self, input_text, pre_base_video_frames,pre_face_detect_results):
505
 
506
-
507
-
508
  mel_chunks_from_audio, audio_segment = self.create_mel_from_audio(input_text)
509
  print(str(len(self.face_detect_img_results)))
510
  video_full_frames_copy=self.video_full_frames.copy()
@@ -513,7 +515,7 @@ class Avatar:
513
  self.video_audio_adjust_parallel(mel_chunks_from_audio,video_full_frames_copy,
514
  pre_base_video_frames,pre_face_detect_results))
515
 
516
- gen=self.datagen(video_full_frames_copy,mel_chunks_from_audio,face_detect_results)
517
 
518
  if self.export_video:
519
  video_write_handle = cv2.VideoWriter(self.temp_lip_video_no_voice_path + self.temp_lip_video_no_voice_filename,
@@ -523,13 +525,16 @@ class Avatar:
523
  video_write_handle=0
524
 
525
  self.make_lip_video(gen,video_write_handle, mel_chunks_from_audio,len(mel_chunks_from_audio)>self.datagen_batch_size,audio_segment)
 
 
 
 
526
  if self.export_video:
527
  self.sync_video_audio(self.output_audio_path + self.output_audio_filename,
528
  self.temp_lip_video_no_voice_path + self.temp_lip_video_no_voice_filename,
529
  self.output_video_path + self.split_current_file_name)
530
  image_frame_num_current=0
531
 
532
-
533
  return post_base_video_frames,post_face_detect_results
534
  def delete_files_in_path(self,dir_path):
535
  # Get the list of files in the directory
 
10
  import torch
11
  import io
12
  import soundfile as sf
13
+ from Wav2Lip.face_detection.models import Wav2Lip
14
+ from Wav2Lip.face_detection import FACE_DETECTION
15
  from Wav2Lip.models import Wav2Lip
 
16
  import face_detection
17
  import audio
18
 
 
245
  # print ("face_detect_img_results[1][1] shape = " +str(self.face_detect_img_results[1][0].shape)) #this is cropped image
246
 
247
 
248
+ def datagen(self, full_frames, mels, face_detect_results, progress_bar=None):
249
  img_batch, mel_batch, frame_batch, coords_batch = [], [], [], []
250
  print(len(full_frames))
251
+ if progress_bar:
252
+ progress_bar.progress(0)
253
+
254
  for i, m in enumerate(mels):
255
  idx = i%len(full_frames)
256
  frame_to_save = full_frames[idx].copy()
 
275
  #print(f"len(img_batch)>{self.datagen_batch_size} now len(img_batch)=" + str(len(img_batch)))
276
  yield img_batch, mel_batch, frame_batch, coords_batch
277
  img_batch, mel_batch, frame_batch, coords_batch = [], [], [], []
278
+ if progress_bar:
279
+ progress_percentage = (i + 1) / len(mels)
280
+ progress_bar.progress(progress_percentage)
281
  if len(img_batch) > 0:
282
  img_batch, mel_batch = np.asarray(img_batch), np.asarray(mel_batch)
283
 
 
507
  self.image_frame_num_current=0
508
  def text_to_lip_video_parallel(self, input_text, pre_base_video_frames,pre_face_detect_results):
509
 
 
 
510
  mel_chunks_from_audio, audio_segment = self.create_mel_from_audio(input_text)
511
  print(str(len(self.face_detect_img_results)))
512
  video_full_frames_copy=self.video_full_frames.copy()
 
515
  self.video_audio_adjust_parallel(mel_chunks_from_audio,video_full_frames_copy,
516
  pre_base_video_frames,pre_face_detect_results))
517
 
518
+ gen = self.datagen(video_full_frames_copy, mel_chunks_from_audio, face_detect_results, progress_bar)
519
 
520
  if self.export_video:
521
  video_write_handle = cv2.VideoWriter(self.temp_lip_video_no_voice_path + self.temp_lip_video_no_voice_filename,
 
525
  video_write_handle=0
526
 
527
  self.make_lip_video(gen,video_write_handle, mel_chunks_from_audio,len(mel_chunks_from_audio)>self.datagen_batch_size,audio_segment)
528
+
529
+ if self.export_video and video_write_handle:
530
+ video_write_handle.release()
531
+
532
  if self.export_video:
533
  self.sync_video_audio(self.output_audio_path + self.output_audio_filename,
534
  self.temp_lip_video_no_voice_path + self.temp_lip_video_no_voice_filename,
535
  self.output_video_path + self.split_current_file_name)
536
  image_frame_num_current=0
537
 
 
538
  return post_base_video_frames,post_face_detect_results
539
  def delete_files_in_path(self,dir_path):
540
  # Get the list of files in the directory