|
|
|
@ -1,3 +1,20 @@
|
|
|
|
|
###############################################################################
|
|
|
|
|
# Copyright (C) 2024 LiveTalking@lipku https://github.com/lipku/LiveTalking
|
|
|
|
|
# email: lipku@foxmail.com
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
import torch
|
|
|
|
|
import numpy as np
|
|
|
|
@ -15,14 +32,13 @@ import copy
|
|
|
|
|
import queue
|
|
|
|
|
from queue import Queue
|
|
|
|
|
from threading import Thread, Event
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
import multiprocessing as mp
|
|
|
|
|
import torch.multiprocessing as mp
|
|
|
|
|
|
|
|
|
|
from musetalk.utils.utils import get_file_type,get_video_fps,datagen
|
|
|
|
|
#from musetalk.utils.preprocessing import get_landmark_and_bbox,read_imgs,coord_placeholder
|
|
|
|
|
from musetalk.utils.blending import get_image,get_image_prepare_material,get_image_blending
|
|
|
|
|
from musetalk.utils.utils import load_all_model,load_diffusion_model,load_audio_model
|
|
|
|
|
from ttsreal import EdgeTTS,VoitsTTS,XTTS
|
|
|
|
|
from musetalk.whisper.audio2feature import Audio2Feature
|
|
|
|
|
|
|
|
|
|
from museasr import MuseASR
|
|
|
|
|
import asyncio
|
|
|
|
@ -46,25 +62,24 @@ def __mirror_index(size, index):
|
|
|
|
|
return res
|
|
|
|
|
else:
|
|
|
|
|
return size - res - 1
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def inference(render_event,batch_size,latents_out_path,audio_feat_queue,audio_out_queue,res_frame_queue,
|
|
|
|
|
): #vae, unet, pe,timesteps
|
|
|
|
|
def inference(render_event,batch_size,input_latent_list_cycle,audio_feat_queue,audio_out_queue,res_frame_queue,
|
|
|
|
|
vae, unet, pe,timesteps): #vae, unet, pe,timesteps
|
|
|
|
|
|
|
|
|
|
vae, unet, pe = load_diffusion_model()
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
timesteps = torch.tensor([0], device=device)
|
|
|
|
|
pe = pe.half()
|
|
|
|
|
vae.vae = vae.vae.half()
|
|
|
|
|
unet.model = unet.model.half()
|
|
|
|
|
# vae, unet, pe = load_diffusion_model()
|
|
|
|
|
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
# timesteps = torch.tensor([0], device=device)
|
|
|
|
|
# pe = pe.half()
|
|
|
|
|
# vae.vae = vae.vae.half()
|
|
|
|
|
# unet.model = unet.model.half()
|
|
|
|
|
|
|
|
|
|
input_latent_list_cycle = torch.load(latents_out_path)
|
|
|
|
|
length = len(input_latent_list_cycle)
|
|
|
|
|
index = 0
|
|
|
|
|
count=0
|
|
|
|
|
counttime=0
|
|
|
|
|
print('start inference')
|
|
|
|
|
while True:
|
|
|
|
|
if render_event.is_set():
|
|
|
|
|
while render_event.is_set():
|
|
|
|
|
starttime=time.perf_counter()
|
|
|
|
|
try:
|
|
|
|
|
whisper_chunks = audio_feat_queue.get(block=True, timeout=1)
|
|
|
|
@ -107,6 +122,11 @@ def inference(render_event,batch_size,latents_out_path,audio_feat_queue,audio_ou
|
|
|
|
|
# print('unet time:',time.perf_counter()-t)
|
|
|
|
|
# t=time.perf_counter()
|
|
|
|
|
recon = vae.decode_latents(pred_latents)
|
|
|
|
|
# infer_inqueue.put((whisper_batch,latent_batch,sessionid))
|
|
|
|
|
# recon,outsessionid = infer_outqueue.get()
|
|
|
|
|
# if outsessionid != sessionid:
|
|
|
|
|
# print('outsessionid:',outsessionid,' mysessionid:',sessionid)
|
|
|
|
|
|
|
|
|
|
# print('vae time:',time.perf_counter()-t)
|
|
|
|
|
#print('diffusion len=',len(recon))
|
|
|
|
|
counttime += (time.perf_counter() - t)
|
|
|
|
@ -121,13 +141,11 @@ def inference(render_event,batch_size,latents_out_path,audio_feat_queue,audio_ou
|
|
|
|
|
res_frame_queue.put((res_frame,__mirror_index(length,index),audio_frames[i*2:i*2+2]))
|
|
|
|
|
index = index + 1
|
|
|
|
|
#print('total batch time:',time.perf_counter()-starttime)
|
|
|
|
|
else:
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
print('musereal inference processor stop')
|
|
|
|
|
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
class MuseReal(BaseReal):
|
|
|
|
|
def __init__(self, opt):
|
|
|
|
|
@torch.no_grad()
|
|
|
|
|
def __init__(self, opt, audio_processor:Audio2Feature,vae, unet, pe,timesteps):
|
|
|
|
|
super().__init__(opt)
|
|
|
|
|
#self.opt = opt # shared with the trainer's opt to support in-place modification of rendering parameters.
|
|
|
|
|
self.W = opt.W
|
|
|
|
@ -155,7 +173,8 @@ class MuseReal(BaseReal):
|
|
|
|
|
self.batch_size = opt.batch_size
|
|
|
|
|
self.idx = 0
|
|
|
|
|
self.res_frame_queue = mp.Queue(self.batch_size*2)
|
|
|
|
|
self.__loadmodels()
|
|
|
|
|
#self.__loadmodels()
|
|
|
|
|
self.audio_processor= audio_processor
|
|
|
|
|
self.__loadavatar()
|
|
|
|
|
|
|
|
|
|
self.asr = MuseASR(opt,self,self.audio_processor)
|
|
|
|
@ -163,13 +182,15 @@ class MuseReal(BaseReal):
|
|
|
|
|
#self.__warm_up()
|
|
|
|
|
|
|
|
|
|
self.render_event = mp.Event()
|
|
|
|
|
mp.Process(target=inference, args=(self.render_event,self.batch_size,self.latents_out_path,
|
|
|
|
|
self.asr.feat_queue,self.asr.output_queue,self.res_frame_queue,
|
|
|
|
|
)).start() #self.vae, self.unet, self.pe,self.timesteps
|
|
|
|
|
self.vae = vae
|
|
|
|
|
self.unet = unet
|
|
|
|
|
self.pe = pe
|
|
|
|
|
self.timesteps = timesteps
|
|
|
|
|
|
|
|
|
|
def __loadmodels(self):
|
|
|
|
|
# load model weights
|
|
|
|
|
self.audio_processor= load_audio_model()
|
|
|
|
|
|
|
|
|
|
# def __loadmodels(self):
|
|
|
|
|
# # load model weights
|
|
|
|
|
# self.audio_processor= load_audio_model()
|
|
|
|
|
# self.audio_processor, self.vae, self.unet, self.pe = load_all_model()
|
|
|
|
|
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
# self.timesteps = torch.tensor([0], device=device)
|
|
|
|
@ -178,7 +199,7 @@ class MuseReal(BaseReal):
|
|
|
|
|
# self.unet.model = self.unet.model.half()
|
|
|
|
|
|
|
|
|
|
def __loadavatar(self):
|
|
|
|
|
#self.input_latent_list_cycle = torch.load(self.latents_out_path)
|
|
|
|
|
self.input_latent_list_cycle = torch.load(self.latents_out_path,weights_only=True)
|
|
|
|
|
with open(self.coords_path, 'rb') as f:
|
|
|
|
|
self.coord_list_cycle = pickle.load(f)
|
|
|
|
|
input_img_list = glob.glob(os.path.join(self.full_imgs_path, '*.[jpJP][pnPN]*[gG]'))
|
|
|
|
@ -287,6 +308,9 @@ class MuseReal(BaseReal):
|
|
|
|
|
process_thread.start()
|
|
|
|
|
|
|
|
|
|
self.render_event.set() #start infer process render
|
|
|
|
|
Thread(target=inference, args=(self.render_event,self.batch_size,self.input_latent_list_cycle,
|
|
|
|
|
self.asr.feat_queue,self.asr.output_queue,self.res_frame_queue,
|
|
|
|
|
self.vae, self.unet, self.pe,self.timesteps)).start() #mp.Process
|
|
|
|
|
count=0
|
|
|
|
|
totaltime=0
|
|
|
|
|
_starttime=time.perf_counter()
|
|
|
|
|