improve musetalk quality

This commit is contained in:
lipku 2024-05-26 18:07:22 +08:00
parent 5a4a459ad5
commit 6508a9160c
2 changed files with 63 additions and 46 deletions

View File

@ -1,15 +1,16 @@
A streaming digital human based on the Ernerf model realize audio video synchronous dialogue. It can basically achieve commercial effects. A streaming digital human based on the Ernerf model realize audio video synchronous dialogue. It can basically achieve commercial effects.
基于ernerf模型的流式数字人实现音视频同步对话。基本可以达到商用效果 基于ernerf模型的流式数字人实现音视频同步对话。基本可以达到商用效果
[效果演示](https://www.bilibili.com/video/BV1PM4m1y7Q2/) [ernerf效果](https://www.bilibili.com/video/BV1PM4m1y7Q2/) [musetalk效果](https://www.bilibili.com/video/BV1gm421N7vQ/)
## Features ## Features
1. 支持声音克隆 1. 支持多种数字人模型: ernerf、musetalk
2. 支持大模型对话 2. 支持声音克隆
3. 支持多种音频特征驱动wav2vec、hubert 3. 支持多种音频特征驱动wav2vec、hubert
4. 支持全身视频拼接 4. 支持全身视频拼接
5. 支持rtmp和webrtc 5. 支持rtmp和webrtc
6. 支持视频编排:不说话时播放自定义视频 6. 支持视频编排:不说话时播放自定义视频
7. 支持大模型对话
## 1. Installation ## 1. Installation
@ -205,7 +206,8 @@ docker版本已经不是最新代码可以作为一个空环境把最新
- [x] 添加chatgpt实现数字人对话 - [x] 添加chatgpt实现数字人对话
- [x] 声音克隆 - [x] 声音克隆
- [x] 数字人静音时用一段视频代替 - [x] 数字人静音时用一段视频代替
- [ ] MuseTalk - [x] MuseTalk
- [ ] SyncTalk
如果本项目对你有帮助帮忙点个star。也欢迎感兴趣的朋友一起来完善该项目。 如果本项目对你有帮助帮忙点个star。也欢迎感兴趣的朋友一起来完善该项目。
Email: lipku@foxmail.com Email: lipku@foxmail.com

View File

@ -101,59 +101,73 @@ class MuseReal:
# self.batch_size) # self.batch_size)
self.asr.run_step() self.asr.run_step()
whisper_chunks = self.asr.get_next_feat() whisper_chunks = self.asr.get_next_feat()
whisper_batch = np.stack(whisper_chunks) is_all_silence=True
latent_batch = [] audio_frames = []
for i in range(self.batch_size): for _ in range(self.batch_size*2):
idx = self.__mirror_index(self.idx+i) frame,type = self.asr.get_audio_out()
latent = self.input_latent_list_cycle[idx] audio_frames.append((frame,type))
latent_batch.append(latent) if type==0:
latent_batch = torch.cat(latent_batch, dim=0) is_all_silence=False
if is_all_silence:
# for i, (whisper_batch,latent_batch) in enumerate(gen): for i in range(self.batch_size):
audio_feature_batch = torch.from_numpy(whisper_batch) self.res_frame_queue.put((None,self.__mirror_index(self.idx),audio_frames[i*2:i*2+2]))
audio_feature_batch = audio_feature_batch.to(device=self.unet.device, self.idx = self.idx + 1
dtype=self.unet.model.dtype) else:
audio_feature_batch = self.pe(audio_feature_batch) print('infer=======')
latent_batch = latent_batch.to(dtype=self.unet.model.dtype) whisper_batch = np.stack(whisper_chunks)
latent_batch = []
for i in range(self.batch_size):
idx = self.__mirror_index(self.idx+i)
latent = self.input_latent_list_cycle[idx]
latent_batch.append(latent)
latent_batch = torch.cat(latent_batch, dim=0)
# for i, (whisper_batch,latent_batch) in enumerate(gen):
audio_feature_batch = torch.from_numpy(whisper_batch)
audio_feature_batch = audio_feature_batch.to(device=self.unet.device,
dtype=self.unet.model.dtype)
audio_feature_batch = self.pe(audio_feature_batch)
latent_batch = latent_batch.to(dtype=self.unet.model.dtype)
pred_latents = self.unet.model(latent_batch, pred_latents = self.unet.model(latent_batch,
self.timesteps, self.timesteps,
encoder_hidden_states=audio_feature_batch).sample encoder_hidden_states=audio_feature_batch).sample
recon = self.vae.decode_latents(pred_latents) recon = self.vae.decode_latents(pred_latents)
#print('diffusion len=',len(recon)) #print('diffusion len=',len(recon))
for res_frame in recon: for i,res_frame in enumerate(recon):
#self.__pushmedia(res_frame,loop,audio_track,video_track) #self.__pushmedia(res_frame,loop,audio_track,video_track)
self.res_frame_queue.put((res_frame,self.__mirror_index(self.idx))) self.res_frame_queue.put((res_frame,self.__mirror_index(self.idx),audio_frames[i*2:i*2+2]))
self.idx = self.idx + 1 self.idx = self.idx + 1
def process_frames(self,quit_event,loop=None,audio_track=None,video_track=None): def process_frames(self,quit_event,loop=None,audio_track=None,video_track=None):
while not quit_event.is_set(): while not quit_event.is_set():
try: try:
res_frame,idx = self.res_frame_queue.get(block=True, timeout=1) res_frame,idx,audio_frames = self.res_frame_queue.get(block=True, timeout=1)
except queue.Empty: except queue.Empty:
continue continue
bbox = self.coord_list_cycle[idx] if audio_frames[0][1]==1 and audio_frames[1][1]==1: #全为静音数据只需要取fullimg
ori_frame = copy.deepcopy(self.frame_list_cycle[idx]) combine_frame = self.frame_list_cycle[idx]
x1, y1, x2, y2 = bbox else:
try: bbox = self.coord_list_cycle[idx]
res_frame = cv2.resize(res_frame.astype(np.uint8),(x2-x1,y2-y1)) ori_frame = copy.deepcopy(self.frame_list_cycle[idx])
except: x1, y1, x2, y2 = bbox
continue try:
mask = self.mask_list_cycle[idx] res_frame = cv2.resize(res_frame.astype(np.uint8),(x2-x1,y2-y1))
mask_crop_box = self.mask_coords_list_cycle[idx] except:
#combine_frame = get_image(ori_frame,res_frame,bbox) continue
combine_frame = get_image_blending(ori_frame,res_frame,bbox,mask,mask_crop_box) mask = self.mask_list_cycle[idx]
mask_crop_box = self.mask_coords_list_cycle[idx]
#combine_frame = get_image(ori_frame,res_frame,bbox)
combine_frame = get_image_blending(ori_frame,res_frame,bbox,mask,mask_crop_box)
image = combine_frame #(outputs['image'] * 255).astype(np.uint8) image = combine_frame #(outputs['image'] * 255).astype(np.uint8)
new_frame = VideoFrame.from_ndarray(image, format="bgr24") new_frame = VideoFrame.from_ndarray(image, format="bgr24")
asyncio.run_coroutine_threadsafe(video_track._queue.put(new_frame), loop) asyncio.run_coroutine_threadsafe(video_track._queue.put(new_frame), loop)
audiotype = 0 for audio_frame in audio_frames:
for _ in range(2): frame,type = audio_frame
frame,type = self.asr.get_audio_out()
audiotype += type
frame = (frame * 32767).astype(np.int16) frame = (frame * 32767).astype(np.int16)
new_frame = AudioFrame(format='s16', layout='mono', samples=frame.shape[0]) new_frame = AudioFrame(format='s16', layout='mono', samples=frame.shape[0])
new_frame.planes[0].update(frame.tobytes()) new_frame.planes[0].update(frame.tobytes())
@ -185,9 +199,10 @@ class MuseReal:
print(f"------actual avg infer fps:{count/totaltime:.4f}") print(f"------actual avg infer fps:{count/totaltime:.4f}")
count=0 count=0
totaltime=0 totaltime=0
if self.res_frame_queue.qsize()>2*self.opt.batch_size: if video_track._queue.qsize()>=2*self.opt.batch_size:
time.sleep(0.1) #print('sleep qsize=',video_track._queue.qsize())
#print('sleep') time.sleep(0.04*self.opt.batch_size*1.5)
# delay = _starttime+_totalframe*0.04-time.perf_counter() #40ms # delay = _starttime+_totalframe*0.04-time.perf_counter() #40ms
# if delay > 0: # if delay > 0:
# time.sleep(delay) # time.sleep(delay)