zejunyang commited on
Commit ·
ac336de
1
Parent(s): 4f3b622
update
Browse filesFiles changed (3)
98 unmodified lines9910010110210310410510610713 unmodified lines1211221231241251261271281291301311 unmodified line1331341351361371381391401411421431441451461471481491501511521531541 unmodified line1561571581591601611621638 unmodified lines1721731741751761771781792 unmodified lines1821831841851861871883 unmodified lines19219319419519619719819931 unmodified lines23123223323423523623723843 unmodified lines282283284285286287288119 unmodified lines40840941041141241341421 unmodified lines43643743843944044144298 unmodified linesframe_inter_model = init_frame_interpolation_model()@spaces.GPUdef audio2video(input_audio, ref_img, headpose_video=None, size=512, steps=25, length=60, seed=42):print('=====Start processing======')fps = 30cfg = 3.5fi_step = 313 unmodified lineswhile os.path.exists(save_dir):save_dir = Path(f"a2v_output/{date_str}/{save_dir_name}_{np.random.randint(10000):04d}")save_dir.mkdir(exist_ok=True, parents=True)print('=====1======')ref_image_np = cv2.cvtColor(ref_img, cv2.COLOR_RGB2BGR)print('=====1======', ref_img.shape, ref_image_np.shape)ref_image_np = crop_face(ref_image_np, lmk_extractor)if ref_image_np is None:return None, Image.fromarray(ref_img)1 unmodified lineref_image_np = cv2.resize(ref_image_np, (size, size))ref_image_pil = Image.fromarray(cv2.cvtColor(ref_image_np, cv2.COLOR_BGR2RGB))print('=====2======')face_result = lmk_extractor(ref_image_np)if face_result is None:return None, ref_image_pilprint('=====3======')lmks = face_result['lmks'].astype(np.float32)ref_pose = vis.draw_landmarks((ref_image_np.shape[1], ref_image_np.shape[0]), lmks, normed=True)sample = prepare_audio_feature(input_audio, wav2vec_model_path=audio_infer_config['a2m_model']['model_path'])sample['audio_feature'] = torch.from_numpy(sample['audio_feature']).float().cuda()sample['audio_feature'] = sample['audio_feature'].unsqueeze(0)print('=====4======')# inferencepred = a2m_model.infer(sample['audio_feature'], sample['seq_len'])1 unmodified linepred = pred.reshape(pred.shape[0], -1, 3)pred = pred + face_result['lmks3d']print('=====5======')if headpose_video is not None:pose_seq = get_headpose_temp(headpose_video)else:8 unmodified linesfor i, verts in enumerate(projected_vertices):lmk_img = vis.draw_landmarks((width, height), verts, normed=False)pose_images.append(lmk_img)print('=====6======')pose_list = []# pose_tensor_list = []2 unmodified lines# [transforms.Resize((height, width)), transforms.ToTensor()]# )args_L = len(pose_images) if length==0 or length > len(pose_images) else lengthargs_L = min(args_L, 60)for pose_image_np in pose_images[: args_L : fi_step]:# pose_image_pil = Image.fromarray(cv2.cvtColor(pose_image_np, cv2.COLOR_BGR2RGB))# pose_tensor_list.append(pose_transform(pose_image_pil))3 unmodified linespose_list = np.array(pose_list)video_length = len(pose_list)print('=====7======')video = pipe(ref_image_pil,31 unmodified lines@spaces.GPUdef video2video(ref_img, source_video, size=512, steps=25, length=60, seed=42):print('=====Start processing======')cfg = 3.5fi_step = 343 unmodified linesverts_list = []bs_list = []args_L = len(source_images) if length==0 or length*step > len(source_images) else length*stepargs_L = min(args_L, 60*step)for src_image_pil in source_images[: args_L : step*fi_step]:src_img_np = cv2.cvtColor(np.array(src_image_pil), cv2.COLOR_RGB2BGR)frame_height, frame_width, _ = src_img_np.shape119 unmodified linesa2v_step_slider = gr.Slider(minimum=5, maximum=20, step=1, value=15, label="Steps (--steps)")with gr.Row():a2v_length = gr.Slider(minimum=0, maximum=60, step=1, value=30, label="Length (-L)")a2v_seed = gr.Number(value=42, label="Seed (--seed)")a2v_botton = gr.Button("Generate", variant="primary")21 unmodified linesv2v_step_slider = gr.Slider(minimum=5, maximum=20, step=1, value=15, label="Steps (--steps)")with gr.Row():v2v_length = gr.Slider(minimum=0, maximum=60, step=1, value=30, label="Length (-L)")v2v_seed = gr.Number(value=42, label="Seed (--seed)")v2v_botton = gr.Button("Generate", variant="primary")98 unmodified lines9910010110210310410513 unmodified lines1191201211221231241251261 unmodified line1281291301311321331341351361371381391401411421431 unmodified line1451461471481491508 unmodified lines1591601611621631642 unmodified lines1671681691701711721733 unmodified lines17717817918018118231 unmodified lines21421521621721821943 unmodified lines263264265266267268269119 unmodified lines38939039139239339439521 unmodified lines41741841942042142242398 unmodified linesframe_inter_model = init_frame_interpolation_model()@spaces.GPUdef audio2video(input_audio, ref_img, headpose_video=None, size=512, steps=25, length=60, seed=42):fps = 30cfg = 3.5fi_step = 313 unmodified lineswhile os.path.exists(save_dir):save_dir = Path(f"a2v_output/{date_str}/{save_dir_name}_{np.random.randint(10000):04d}")save_dir.mkdir(exist_ok=True, parents=True)ref_image_np = cv2.cvtColor(ref_img, cv2.COLOR_RGB2BGR)ref_image_np = crop_face(ref_image_np, lmk_extractor)if ref_image_np is None:return None, Image.fromarray(ref_img)1 unmodified lineref_image_np = cv2.resize(ref_image_np, (size, size))ref_image_pil = Image.fromarray(cv2.cvtColor(ref_image_np, cv2.COLOR_BGR2RGB))face_result = lmk_extractor(ref_image_np)if face_result is None:return None, ref_image_pillmks = face_result['lmks'].astype(np.float32)ref_pose = vis.draw_landmarks((ref_image_np.shape[1], ref_image_np.shape[0]), lmks, normed=True)sample = prepare_audio_feature(input_audio, wav2vec_model_path=audio_infer_config['a2m_model']['model_path'])sample['audio_feature'] = torch.from_numpy(sample['audio_feature']).float().cuda()sample['audio_feature'] = sample['audio_feature'].unsqueeze(0)# inferencepred = a2m_model.infer(sample['audio_feature'], sample['seq_len'])1 unmodified linepred = pred.reshape(pred.shape[0], -1, 3)pred = pred + face_result['lmks3d']if headpose_video is not None:pose_seq = get_headpose_temp(headpose_video)else:8 unmodified linesfor i, verts in enumerate(projected_vertices):lmk_img = vis.draw_landmarks((width, height), verts, normed=False)pose_images.append(lmk_img)pose_list = []# pose_tensor_list = []2 unmodified lines# [transforms.Resize((height, width)), transforms.ToTensor()]# )args_L = len(pose_images) if length==0 or length > len(pose_images) else lengthargs_L = min(args_L, 90)for pose_image_np in pose_images[: args_L : fi_step]:# pose_image_pil = Image.fromarray(cv2.cvtColor(pose_image_np, cv2.COLOR_BGR2RGB))# pose_tensor_list.append(pose_transform(pose_image_pil))3 unmodified linespose_list = np.array(pose_list)video_length = len(pose_list)video = pipe(ref_image_pil,31 unmodified lines@spaces.GPUdef video2video(ref_img, source_video, size=512, steps=25, length=60, seed=42):cfg = 3.5fi_step = 343 unmodified linesverts_list = []bs_list = []args_L = len(source_images) if length==0 or length*step > len(source_images) else length*stepargs_L = min(args_L, 90*step)for src_image_pil in source_images[: args_L : step*fi_step]:src_img_np = cv2.cvtColor(np.array(src_image_pil), cv2.COLOR_RGB2BGR)frame_height, frame_width, _ = src_img_np.shape119 unmodified linesa2v_step_slider = gr.Slider(minimum=5, maximum=20, step=1, value=15, label="Steps (--steps)")with gr.Row():a2v_length = gr.Slider(minimum=0, maximum=90, step=1, value=30, label="Length (-L)")a2v_seed = gr.Number(value=42, label="Seed (--seed)")a2v_botton = gr.Button("Generate", variant="primary")21 unmodified linesv2v_step_slider = gr.Slider(minimum=5, maximum=20, step=1, value=15, label="Steps (--steps)")with gr.Row():v2v_length = gr.Slider(minimum=0, maximum=90, step=1, value=30, label="Length (-L)")v2v_seed = gr.Number(value=42, label="Seed (--seed)")v2v_botton = gr.Button("Generate", variant="primary")
2 unmodified lines34567891011122 unmodified linesdef crop_face(img, lmk_extractor, expand=1.5):print('****=====1======')result = lmk_extractor(img) # cv2 BGRprint('****=====2======')if result is None:return None2 unmodified lines34567892 unmodified linesdef crop_face(img, lmk_extractor, expand=1.5):result = lmk_extractor(img) # cv2 BGRif result is None:return None
37 unmodified lines3839404142434416 unmodified lines6162636465666737 unmodified linesdef __call__(self, img):print('///=====1======')frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)# t0 = time.time()16 unmodified linesexcept:return Noneprint('///=====2======')bs_list = detection_result.face_blendshapesif len(bs_list) == 1:bs = bs_list[0]37 unmodified lines38394041424316 unmodified lines60616263646537 unmodified linesdef __call__(self, img):frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)# t0 = time.time()16 unmodified linesexcept:return Nonebs_list = detection_result.face_blendshapesif len(bs_list) == 1:bs = bs_list[0]
app.py+6-25
src/utils/crop_face_single.py-3
src/utils/mp_utils.py-2