@ -24,7 +24,6 @@ import argparse
import shutil
import asyncio
app = Flask ( __name__ )
sockets = Sockets ( app )
global nerfreal
@ -59,6 +58,7 @@ def llm_response(message):
print ( response )
return response
@sockets.route ( ' /humanchat ' )
def chat_socket ( ws ) :
# 获取WebSocket对象
@ -79,9 +79,11 @@ def chat_socket(ws):
res = llm_response ( message )
nerfreal . put_msg_txt ( res )
#####webrtc###############################
pcs = set ( )
# @app.route('/offer', methods=['POST'])
async def offer ( request ) :
params = await request . json ( )
@ -115,6 +117,7 @@ async def offer(request):
) ,
)
async def human ( request ) :
params = await request . json ( )
@ -131,12 +134,14 @@ async def human(request):
) ,
)
async def on_shutdown ( app ) :
# close peer connections
coros = [ pc . close ( ) for pc in pcs ]
await asyncio . gather ( * coros )
pcs . clear ( )
async def post ( url , data ) :
try :
async with aiohttp . ClientSession ( ) as session :
@ -145,6 +150,7 @@ async def post(url,data):
except aiohttp . ClientError as e :
print ( f ' Error: { e } ' )
async def run ( push_url ) :
pc = RTCPeerConnection ( )
pcs . add ( pc )
@ -163,6 +169,8 @@ async def run(push_url):
await pc . setLocalDescription ( await pc . createOffer ( ) )
answer = await post ( push_url , pc . localDescription . sdp )
await pc . setRemoteDescription ( RTCSessionDescription ( sdp = answer , type = ' answer ' ) )
##########################################
# os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'
# os.environ['MULTIPROCESSING_METHOD'] = 'forkserver'
@ -182,13 +190,19 @@ if __name__ == '__main__':
### training options
parser . add_argument ( ' --ckpt ' , type = str , default = ' data/pretrained/ngp_kf.pth ' )
parser . add_argument ( ' --num_rays ' , type = int , default = 4096 * 16 , help = " num rays sampled per image for each training step " )
parser . add_argument ( ' --num_rays ' , type = int , default = 4096 * 16 ,
help = " num rays sampled per image for each training step " )
parser . add_argument ( ' --cuda_ray ' , action = ' store_true ' , help = " use CUDA raymarching instead of pytorch " )
parser . add_argument ( ' --max_steps ' , type = int , default = 16 , help = " max num steps sampled per ray (only valid when using --cuda_ray) " )
parser . add_argument ( ' --num_steps ' , type = int , default = 16 , help = " num steps sampled per ray (only valid when NOT using --cuda_ray) " )
parser . add_argument ( ' --upsample_steps ' , type = int , default = 0 , help = " num steps up-sampled per ray (only valid when NOT using --cuda_ray) " )
parser . add_argument ( ' --update_extra_interval ' , type = int , default = 16 , help = " iter interval to update extra status (only valid when using --cuda_ray) " )
parser . add_argument ( ' --max_ray_batch ' , type = int , default = 4096 , help = " batch size of rays at inference to avoid OOM (only valid when NOT using --cuda_ray) " )
parser . add_argument ( ' --max_steps ' , type = int , default = 16 ,
help = " max num steps sampled per ray (only valid when using --cuda_ray) " )
parser . add_argument ( ' --num_steps ' , type = int , default = 16 ,
help = " num steps sampled per ray (only valid when NOT using --cuda_ray) " )
parser . add_argument ( ' --upsample_steps ' , type = int , default = 0 ,
help = " num steps up-sampled per ray (only valid when NOT using --cuda_ray) " )
parser . add_argument ( ' --update_extra_interval ' , type = int , default = 16 ,
help = " iter interval to update extra status (only valid when using --cuda_ray) " )
parser . add_argument ( ' --max_ray_batch ' , type = int , default = 4096 ,
help = " batch size of rays at inference to avoid OOM (only valid when NOT using --cuda_ray) " )
### loss set
parser . add_argument ( ' --warmup_step ' , type = int , default = 10000 , help = " warm up steps " )
@ -203,23 +217,31 @@ if __name__ == '__main__':
parser . add_argument ( ' --bg_img ' , type = str , default = ' white ' , help = " background image " )
parser . add_argument ( ' --fbg ' , action = ' store_true ' , help = " frame-wise bg " )
parser . add_argument ( ' --exp_eye ' , action = ' store_true ' , help = " explicitly control the eyes " )
parser . add_argument ( ' --fix_eye ' , type = float , default = - 1 , help = " fixed eye area, negative to disable, set to 0-0.3 for a reasonable eye " )
parser . add_argument ( ' --fix_eye ' , type = float , default = - 1 ,
help = " fixed eye area, negative to disable, set to 0-0.3 for a reasonable eye " )
parser . add_argument ( ' --smooth_eye ' , action = ' store_true ' , help = " smooth the eye area sequence " )
parser . add_argument ( ' --torso_shrink ' , type = float , default = 0.8 , help = " shrink bg coords to allow more flexibility in deform " )
parser . add_argument ( ' --torso_shrink ' , type = float , default = 0.8 ,
help = " shrink bg coords to allow more flexibility in deform " )
### dataset options
parser . add_argument ( ' --color_space ' , type = str , default = ' srgb ' , help = " Color space, supports (linear, srgb) " )
parser . add_argument ( ' --preload ' , type = int , default = 0 , help = " 0 means load data from disk on-the-fly, 1 means preload to CPU, 2 means GPU. " )
parser . add_argument ( ' --preload ' , type = int , default = 0 ,
help = " 0 means load data from disk on-the-fly, 1 means preload to CPU, 2 means GPU. " )
# (the default value is for the fox dataset)
parser . add_argument ( ' --bound ' , type = float , default = 1 , help = " assume the scene is bounded in box[-bound, bound]^3, if > 1, will invoke adaptive ray marching. " )
parser . add_argument ( ' --bound ' , type = float , default = 1 ,
help = " assume the scene is bounded in box[-bound, bound]^3, if > 1, will invoke adaptive ray marching. " )
parser . add_argument ( ' --scale ' , type = float , default = 4 , help = " scale camera location into box[-bound, bound]^3 " )
parser . add_argument ( ' --offset ' , type = float , nargs = ' * ' , default = [ 0 , 0 , 0 ] , help = " offset of camera location " )
parser . add_argument ( ' --dt_gamma ' , type = float , default = 1 / 256 , help = " dt_gamma (>=0) for adaptive ray marching. set to 0 to disable, >0 to accelerate rendering (but usually with worse quality) " )
parser . add_argument ( ' --dt_gamma ' , type = float , default = 1 / 256 ,
help = " dt_gamma (>=0) for adaptive ray marching. set to 0 to disable, >0 to accelerate rendering (but usually with worse quality) " )
parser . add_argument ( ' --min_near ' , type = float , default = 0.05 , help = " minimum near distance for camera " )
parser . add_argument ( ' --density_thresh ' , type = float , default = 10 , help = " threshold for density grid to be occupied (sigma) " )
parser . add_argument ( ' --density_thresh_torso ' , type = float , default = 0.01 , help = " threshold for density grid to be occupied (alpha) " )
parser . add_argument ( ' --patch_size ' , type = int , default = 1 , help = " [experimental] render patches in training, so as to apply LPIPS loss. 1 means disabled, use [64, 32, 16] to enable " )
parser . add_argument ( ' --density_thresh ' , type = float , default = 10 ,
help = " threshold for density grid to be occupied (sigma) " )
parser . add_argument ( ' --density_thresh_torso ' , type = float , default = 0.01 ,
help = " threshold for density grid to be occupied (alpha) " )
parser . add_argument ( ' --patch_size ' , type = int , default = 1 ,
help = " [experimental] render patches in training, so as to apply LPIPS loss. 1 means disabled, use [64, 32, 16] to enable " )
parser . add_argument ( ' --init_lips ' , action = ' store_true ' , help = " init lips region " )
parser . add_argument ( ' --finetune_lips ' , action = ' store_true ' , help = " use LPIPS and landmarks to fine tune lips region " )
@ -237,12 +259,15 @@ if __name__ == '__main__':
parser . add_argument ( ' --max_spp ' , type = int , default = 1 , help = " GUI rendering max sample per pixel " )
### else
parser . add_argument ( ' --att ' , type = int , default = 2 , help = " audio attention mode (0 = turn off, 1 = left-direction, 2 = bi-direction) " )
parser . add_argument ( ' --aud ' , type = str , default = ' ' , help = " audio source (empty will load the default, else should be a path to a npy file) " )
parser . add_argument ( ' --att ' , type = int , default = 2 ,
help = " audio attention mode (0 = turn off, 1 = left-direction, 2 = bi-direction) " )
parser . add_argument ( ' --aud ' , type = str , default = ' ' ,
help = " audio source (empty will load the default, else should be a path to a npy file) " )
parser . add_argument ( ' --emb ' , action = ' store_true ' , help = " use audio class + embedding instead of logits " )
parser . add_argument ( ' --ind_dim ' , type = int , default = 4 , help = " individual code dim, 0 to turn off " )
parser . add_argument ( ' --ind_num ' , type = int , default = 10000 , help = " number of individual codes, should be larger than training dataset size " )
parser . add_argument ( ' --ind_num ' , type = int , default = 10000 ,
help = " number of individual codes, should be larger than training dataset size " )
parser . add_argument ( ' --ind_dim_torso ' , type = int , default = 8 , help = " individual code dim, 0 to turn off " )
@ -251,7 +276,8 @@ if __name__ == '__main__':
parser . add_argument ( ' --part2 ' , action = ' store_true ' , help = " use partial training data (first 15s) " )
parser . add_argument ( ' --train_camera ' , action = ' store_true ' , help = " optimize camera pose " )
parser . add_argument ( ' --smooth_path ' , action = ' store_true ' , help = " brute-force smooth camera pose trajectory with a window size " )
parser . add_argument ( ' --smooth_path ' , action = ' store_true ' ,
help = " brute-force smooth camera pose trajectory with a window size " )
parser . add_argument ( ' --smooth_path_window ' , type = int , default = 7 , help = " smoothing window size " )
# asr
@ -299,7 +325,8 @@ if __name__ == '__main__':
parser . add_argument ( ' --model ' , type = str , default = ' ernerf ' ) # musetalk wav2lip
parser . add_argument ( ' --transport ' , type = str , default = ' rtcpush ' ) # rtmp webrtc rtcpush
parser . add_argument ( ' --push_url ' , type = str , default = ' http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream ' ) #rtmp://localhost/live/livestream
parser . add_argument ( ' --push_url ' , type = str ,
default = ' http://localhost:1985/rtc/v1/whip/?app=live&stream=livestream ' ) # rtmp://localhost/live/livestream
parser . add_argument ( ' --listenport ' , type = int , default = 8010 )
@ -312,6 +339,7 @@ if __name__ == '__main__':
from ernerf . nerf_triplane . utils import *
from ernerf . nerf_triplane . network import NeRFNetwork
from nerfreal import NeRFReal
# assert test mode
opt . test = True
opt . test_train = False
@ -346,7 +374,8 @@ if __name__ == '__main__':
criterion = torch . nn . MSELoss ( reduction = ' none ' )
metrics = [ ] # use no metric in GUI for faster initialization...
print ( model )
trainer = Trainer ( ' ngp ' , opt , model , device = device , workspace = opt . workspace , criterion = criterion , fp16 = opt . fp16 , metrics = metrics , use_checkpoint = opt . ckpt )
trainer = Trainer ( ' ngp ' , opt , model , device = device , workspace = opt . workspace , criterion = criterion , fp16 = opt . fp16 ,
metrics = metrics , use_checkpoint = opt . ckpt )
test_loader = NeRFDataset_Test ( opt , device = device ) . dataloader ( )
model . aud_features = test_loader . _data . auds
@ -356,10 +385,12 @@ if __name__ == '__main__':
nerfreal = NeRFReal ( opt , trainer , test_loader )
elif opt . model == ' musetalk ' :
from musereal import MuseReal
print ( opt )
nerfreal = MuseReal ( opt )
elif opt . model == ' wav2lip ' :
from lipreal import LipReal
print ( opt )
nerfreal = LipReal ( opt )
@ -388,6 +419,7 @@ if __name__ == '__main__':
for route in list ( appasync . router . routes ( ) ) :
cors . add ( route )
def run_server ( runner ) :
loop = asyncio . new_event_loop ( )
asyncio . set_event_loop ( loop )
@ -397,6 +429,8 @@ if __name__ == '__main__':
if opt . transport == ' rtcpush ' :
loop . run_until_complete ( run ( opt . push_url ) )
loop . run_forever ( )
Thread ( target = run_server , args = ( web . AppRunner ( appasync ) , ) ) . start ( )
print ( ' start websocket server ' )
@ -404,5 +438,3 @@ if __name__ == '__main__':
# app.router.add_post("/offer", offer)
server = pywsgi . WSGIServer ( ( ' 0.0.0.0 ' , 8000 ) , app , handler_class = WebSocketHandler )
server . serve_forever ( )