videoswap.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import cv2
  3. import glob
  4. import torch
  5. import shutil
  6. import numpy as np
  7. from tqdm import tqdm
  8. from util.reverse2original import reverse2wholeimage
  9. import moviepy.editor as mp
  10. from moviepy.editor import AudioFileClip, VideoFileClip
  11. from moviepy.video.io.ImageSequenceClip import ImageSequenceClip
  12. import time
  13. from util.add_watermark import watermark_image
  14. def _totensor(array):
  15. tensor = torch.from_numpy(array)
  16. img = tensor.transpose(0, 1).transpose(0, 2).contiguous()
  17. return img.float().div(255)
  18. def video_swap(video_path, id_vetor, swap_model, detect_model, save_path, temp_results_dir='./temp_results', crop_size=224, no_simswaplogo = False):
  19. video_forcheck = VideoFileClip(video_path)
  20. if video_forcheck.audio is None:
  21. no_audio = True
  22. else:
  23. no_audio = False
  24. del video_forcheck
  25. if not no_audio:
  26. video_audio_clip = AudioFileClip(video_path)
  27. video = cv2.VideoCapture(video_path)
  28. logoclass = watermark_image('./simswaplogo/simswaplogo.png')
  29. ret = True
  30. frame_index = 0
  31. frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
  32. # video_WIDTH = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
  33. # video_HEIGHT = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
  34. fps = video.get(cv2.CAP_PROP_FPS)
  35. if os.path.exists(temp_results_dir):
  36. shutil.rmtree(temp_results_dir)
  37. # while ret:
  38. for frame_index in tqdm(range(frame_count)):
  39. ret, frame = video.read()
  40. if ret:
  41. detect_results = detect_model.get(frame,crop_size)
  42. if detect_results is not None:
  43. # print(frame_index)
  44. if not os.path.exists(temp_results_dir):
  45. os.mkdir(temp_results_dir)
  46. frame_align_crop_list = detect_results[0]
  47. frame_mat_list = detect_results[1]
  48. swap_result_list = []
  49. for frame_align_crop in frame_align_crop_list:
  50. # BGR TO RGB
  51. # frame_align_crop_RGB = frame_align_crop[...,::-1]
  52. frame_align_crop_tenor = _totensor(cv2.cvtColor(frame_align_crop,cv2.COLOR_BGR2RGB))[None,...].cuda()
  53. swap_result = swap_model(None, frame_align_crop_tenor, id_vetor, None, True)[0]
  54. swap_result_list.append(swap_result)
  55. reverse2wholeimage(swap_result_list, frame_mat_list, crop_size, frame, logoclass,os.path.join(temp_results_dir, 'frame_{:0>7d}.jpg'.format(frame_index)),no_simswaplogo)
  56. else:
  57. if not os.path.exists(temp_results_dir):
  58. os.mkdir(temp_results_dir)
  59. frame = frame.astype(np.uint8)
  60. if not no_simswaplogo:
  61. frame = logoclass.apply_frames(frame)
  62. cv2.imwrite(os.path.join(temp_results_dir, 'frame_{:0>7d}.jpg'.format(frame_index)), frame)
  63. else:
  64. break
  65. video.release()
  66. # image_filename_list = []
  67. path = os.path.join(temp_results_dir,'*.jpg')
  68. image_filenames = sorted(glob.glob(path))
  69. clips = ImageSequenceClip(image_filenames,fps = fps)
  70. if not no_audio:
  71. clips = clips.set_audio(video_audio_clip)
  72. clips.write_videofile(save_path)