hytian2@gmail.com commited on
Commit ·
eae1cca
1
Parent(s): 48798aa
update
Browse filesFiles changed (11)
34 unmodified lines353637383940414243446 unmodified lines5152535455565722 unmodified lines8081828334 unmodified linesavailable_audios = [os.path.basename(x) for x in available_audios]with gr.Blocks() as demo:gr.HTML("""<h1 style="text-align: center; font-size: 40px; font-family: 'Times New Roman', Times, serif;">Free-View Expressive Talking Head Video Editing</h1>6 unmodified lines<img src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-sm.svg#center" alt="Duplicate Space"></a></p>""")with gr.Column(elem_id="col-container"):with gr.Row():with gr.Column():22 unmodified linesaudio_input.select(lambda x: "./assets/audios/" + x, audio_input, audio_preview_output)submit_btn.click(process, inputs, outputs)demo.queue(max_size=12).launch()34 unmodified lines3536373839404142436 unmodified lines505152535455565722 unmodified lines8081828334 unmodified linesavailable_audios = [os.path.basename(x) for x in available_audios]with gr.Blocks() as demo:gr.HTML("""<h1 style="text-align: center; font-size: 40px; font-family: 'Times New Roman', Times, serif;">Free-View Expressive Talking Head Video Editing</h1>6 unmodified lines<img src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-sm.svg#center" alt="Duplicate Space"></a></p>""")with gr.Column(elem_id="col-container"):with gr.Row():with gr.Column():22 unmodified linesaudio_input.select(lambda x: "./assets/audios/" + x, audio_input, audio_preview_output)submit_btn.click(process, inputs, outputs)demo.queue(max_size=10).launch()
123version https://git-lfs.github.com/spec/v1oid sha256:5b200f395b09505d61f3efb67feaacbbd5bb358e75b476c4da083e4a7cef58afsize 525123version https://git-lfs.github.com/spec/v1oid sha256:05569fbc982413520a7f81636cba156e6f67344c1cd13f4831ccd95cbb1bf0adsize 454
123version https://git-lfs.github.com/spec/v1oid sha256:3ac70dd3972f406d9e8195283d11395a7b1e2528bdbdec4a3420eeac919489c9size 909123version https://git-lfs.github.com/spec/v1oid sha256:37790e74ae602e20aa3be2811f60cda21905cc1a88d6efc53f99ec9a73f7e1dfsize 810
123version https://git-lfs.github.com/spec/v1oid sha256:246e4910d5ae9937f2d692beb6d6267dcb2f09bf7b7e0bd75d373a167289cf08size 598123version https://git-lfs.github.com/spec/v1oid sha256:20b7d61ba04d8743c5d0a26b235897df9664b5dc095d96a17be3b9cdbfb06142size 528
123version https://git-lfs.github.com/spec/v1oid sha256:497b14d4185a447327fac69602b66997dc791ff333ead12680c36e3e27d20195size 656123version https://git-lfs.github.com/spec/v1oid sha256:79b016ed5c7934c7e667dc6999457726175b17d4ef0fb67a755c5d038f0b75ecsize 567
123version https://git-lfs.github.com/spec/v1oid sha256:5db52100e8db15ec88b4b3e4d0229fcbdcf96f623b6f4eb8890f7915b430b914size 6554 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940414243444546474 unmodified linesdef input_pose(pose_select="front"):step = 1if pose_select == "front":pose = [[0.0, 0.0, 0.0] for i in range(0, 10, step)]#-20 to 20elif pose_select == "left_right_shaking":pose = [[-i, 0.0, 0.0] for i in range(0, 20, step)]#0 to -20pose += [[i - 20.0, 0.0, 0.0] for i in range(0, 40, step)] # -20 to 20pose += [[20.0 - i, 0.0, 0.0] for i in range(0, 20, step)] # 20 to 0pose = pose + posepose = pose + posepose = pose + pose# pose = pose + pose[::-1]else:raise ValueError("pose_select Error")return poseEMOTIONS = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']def input_emotion(emotion_select="neutral"):sacle_factor = 2if emotion_select == "neutral":emotion = [[0.0,0.0,0.0,0.0,0.0,0.0,1.0] for _ in range(2)]#((i%50))*0.04elif emotion_select == "happy":emotion = [[0.0,0.0,0.0,1.0,0.0,0.0,0.0] for _ in range(2)]#((i%50))*0.04elif emotion_select == "angry":emotion = [[1.0,0.0,0.0,0.0,0.0,0.0,0.0] for _ in range(2)]elif emotion_select == "surprised":emotion = [[0.0,0.0,0.0,0.0,0.0,1.0,0.0] for _ in range(2)]else:raise ValueError("emotion_select Error")return emotion * sacle_factordef input_blink(blink_select="yes"):if blink_select == "yes":blink = [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [0.8], [0.6], [0.0], [0.0], [1.0]]blink = blink + blink + blinkelse:blink = [[1.0] for _ in range(2)]return blink4 unmodified lines56789101112131415161718192021222324252627282930313233343536373839404142434445464748494 unmodified linesdef input_pose(pose_select="front"):step = 1if pose_select == "front":pose = [[0.0, 0.0, 0.0] for i in range(0, 10, step)] # -20 to 20elif pose_select == "left_right_shaking":pose = [[-i, 0.0, 0.0] for i in range(0, 20, step)] # 0 to -20pose += [[i - 20.0, 0.0, 0.0] for i in range(0, 40, step)] # -20 to 20pose += [[20.0 - i, 0.0, 0.0] for i in range(0, 20, step)] # 20 to 0pose = pose + posepose = pose + posepose = pose + poseelse:raise ValueError("pose_select Error")return poseEMOTIONS = ["angry", "disgust", "fear", "happy", "sad", "surprise", "neutral"]def input_emotion(emotion_select="neutral"):sacle_factor = 2if emotion_select == "neutral":emotion = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0] for _ in range(2)] # ((i%50))*0.04elif emotion_select == "happy":emotion = [[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0] for _ in range(2)] # ((i%50))*0.04elif emotion_select == "angry":emotion = [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] for _ in range(2)]elif emotion_select == "surprised":emotion = [[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0] for _ in range(2)]else:raise ValueError("emotion_select Error")return emotion * sacle_factordef input_blink(blink_select="yes"):if blink_select == "yes":blink = [[1.0] for _ in range(25)]blink += [[0.8], [0.6], [0.0], [0.0]]blink += [[1.0] for _ in range(5)]blink = blink + blink + blinkelse:blink = [[1.0] for _ in range(2)]return blink
205 unmodified lines20620720820921021121221321421521644 unmodified lines26126226326426526626726826927015 unmodified lines286287288289290291292293294295296205 unmodified lines# face_sequences = torch.cat([face_sequences[:, :, i] for i in range(face_sequences.size(2))], dim=0)# print(audio_sequences.size(), face_sequences.size(), pose_sequences.size(), emotion_sequences.size())audio_embedding = self.audio_encoder(audio_sequences) # B, 512, 1, 1pose_embedding = self.pose_encoder(pose_sequences) # B, 512, 1, 1emotion_embedding = self.emotion_encoder(emotion_sequences) # B, 512, 1, 1blink_embedding = self.blink_encoder(blink_sequences) # B, 512, 1, 1inputs_embedding = torch.cat((audio_embedding, pose_embedding, emotion_embedding, blink_embedding), dim=1) # B, 1536, 1, 1# print(audio_embedding.size(), pose_embedding.size(), emotion_embedding.size(), inputs_embedding.size())feats = []44 unmodified lines"""super(Self_Attention, self).__init__()self.query_conv = nn.Conv2d(in_channels=in_planes_s, out_channels=in_planes_s // 8, kernel_size=1)self.key_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r // 8, kernel_size=1)self.value_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r, kernel_size=1)self.gamma = nn.Parameter(torch.zeros(1))self.softmax = nn.Softmax(dim=-1)def forward(self, source):source = source.float() if isinstance(source, torch.cuda.HalfTensor) else source15 unmodified linesr_batchsize, rC, rH, rW = reference.size()proj_query = self.query_conv(source).view(s_batchsize, -1, sH * sW).permute(0, 2, 1)proj_key = self.key_conv(reference).view(r_batchsize, -1, rW * rH)energy = torch.bmm(proj_query, proj_key)attention = self.softmax(energy)proj_value = self.value_conv(reference).view(r_batchsize, -1, rH * rW)out = torch.bmm(proj_value, attention.permute(0, 2, 1))out = out.view(s_batchsize, sC, sH, sW)out = self.gamma * out + sourcereturn out.half() if isinstance(source, torch.cuda.FloatTensor) else out205 unmodified lines20620720820921021121221321421521644 unmodified lines26126226326426526626726826927015 unmodified lines286287288289290291292293294295296205 unmodified lines# face_sequences = torch.cat([face_sequences[:, :, i] for i in range(face_sequences.size(2))], dim=0)# print(audio_sequences.size(), face_sequences.size(), pose_sequences.size(), emotion_sequences.size())audio_embedding = self.audio_encoder(audio_sequences) # B, 512, 1, 1pose_embedding = self.pose_encoder(pose_sequences) # B, 512, 1, 1emotion_embedding = self.emotion_encoder(emotion_sequences) # B, 512, 1, 1blink_embedding = self.blink_encoder(blink_sequences) # B, 512, 1, 1inputs_embedding = torch.cat((audio_embedding, pose_embedding, emotion_embedding, blink_embedding), dim=1) # B, 1536, 1, 1# print(audio_embedding.size(), pose_embedding.size(), emotion_embedding.size(), inputs_embedding.size())feats = []44 unmodified lines"""super(Self_Attention, self).__init__()self.query_conv = nn.Conv2d(in_channels=in_planes_s, out_channels=in_planes_s // 8, kernel_size=1)self.key_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r // 8, kernel_size=1)self.value_conv = nn.Conv2d(in_channels=in_planes_r, out_channels=in_planes_r, kernel_size=1)self.gamma = nn.Parameter(torch.zeros(1))self.softmax = nn.Softmax(dim=-1)def forward(self, source):source = source.float() if isinstance(source, torch.cuda.HalfTensor) else source15 unmodified linesr_batchsize, rC, rH, rW = reference.size()proj_query = self.query_conv(source).view(s_batchsize, -1, sH * sW).permute(0, 2, 1)proj_key = self.key_conv(reference).view(r_batchsize, -1, rW * rH)energy = torch.bmm(proj_query, proj_key)attention = self.softmax(energy)proj_value = self.value_conv(reference).view(r_batchsize, -1, rH * rW)out = torch.bmm(proj_value, attention.permute(0, 2, 1))out = out.view(s_batchsize, sC, sH, sW)out = self.gamma * out + sourcereturn out.half() if isinstance(source, torch.cuda.FloatTensor) else out
279 unmodified lines280281282283284285286279 unmodified lineselse:outfile = "/tmp/{}.mp4".format(timestamp)tmp_video = "/tmp/temp_{}.mp4".format(timestamp)writer = imageio.get_writer(tmp_video, fps=fps, codec="libx264", quality=10, pixelformat="yuv420p", macro_block_size=1) if not preview else None# print('Generating frames...', outfile, steps)for inputs, frames, coords in tqdm(gen, total=steps):with torch.no_grad():279 unmodified lines280281282283284285286287288289290279 unmodified lineselse:outfile = "/tmp/{}.mp4".format(timestamp)tmp_video = "/tmp/temp_{}.mp4".format(timestamp)writer = (imageio.get_writer(tmp_video, fps=fps, codec="libx264", quality=10, pixelformat="yuv420p", macro_block_size=1)if not previewelse None)# print('Generating frames...', outfile, steps)for inputs, frames, coords in tqdm(gen, total=steps):with torch.no_grad():
8 unmodified lines9101112131448 unmodified lines636465666768699 unmodified lines79808182838485868788899091929394959618 unmodified lines1151161171181198 unmodified linesdevice = "cuda" if torch.cuda.is_available() else "cpu"def get_squre_coords(coords, image, size=None, last_size=None):y1, y2, x1, x2 = coordsw, h = x2 - x1, y2 - y148 unmodified linesx1 = max(0, rect[0] - padx1)x2 = min(image.shape[1], rect[2] + padx2)# y_gap, x_gap = ((y2 - y1) * 2) // 3, ((x2 - x1) * 2) // 3y_gap, x_gap = (y2 - y1)//2, (x2 - x1)//2coords_ = [y1 - y_gap, y2 + y_gap, x1 - x_gap, x2 + x_gap]_, coords = get_squre_coords(coords_, image)9 unmodified linesprint("Number of frames cropped: {}".format(len(results)))print("First coords: {}".format(results[0]))boxes = np.array(results)boxes = get_smoothened_boxes(boxes, T=15)# results = [[image[y1:y2, x1:x2], (y1, y2, x1, x2)] for image, (x1, y1, x2, y2) in zip(images, boxes)]del detectorreturn boxesdef add_black(imgs):for i in range(len(imgs)):imgs[i] = cv2.vconcat([np.zeros((100, imgs[i].shape[1], 3), dtype=np.uint8), imgs[i], np.zeros((20, imgs[i].shape[1], 3), dtype=np.uint8)])return imgsdef preprocess(video_dir="./assets/videos", save_dir="./assets/coords"):all_videos = natsorted(glob.glob(os.path.join(video_dir, "*.mp4")))for video_path in all_videos:18 unmodified linesnpz = np.load(os.path.join(save_dir, video_name + ".npz"))return npz["coords"]if __name__ == "__main__":preprocess()8 unmodified lines910111213141548 unmodified lines646566676869709 unmodified lines808182838485868788899091929394959697989918 unmodified lines1181191201211221238 unmodified linesdevice = "cuda" if torch.cuda.is_available() else "cpu"def get_squre_coords(coords, image, size=None, last_size=None):y1, y2, x1, x2 = coordsw, h = x2 - x1, y2 - y148 unmodified linesx1 = max(0, rect[0] - padx1)x2 = min(image.shape[1], rect[2] + padx2)# y_gap, x_gap = ((y2 - y1) * 2) // 3, ((x2 - x1) * 2) // 3y_gap, x_gap = (y2 - y1) // 2, (x2 - x1) // 2coords_ = [y1 - y_gap, y2 + y_gap, x1 - x_gap, x2 + x_gap]_, coords = get_squre_coords(coords_, image)9 unmodified linesprint("Number of frames cropped: {}".format(len(results)))print("First coords: {}".format(results[0]))boxes = np.array(results)boxes = get_smoothened_boxes(boxes, T=25)# results = [[image[y1:y2, x1:x2], (y1, y2, x1, x2)] for image, (x1, y1, x2, y2) in zip(images, boxes)]del detectorreturn boxesdef add_black(imgs):for i in range(len(imgs)):imgs[i] = cv2.vconcat([np.zeros((100, imgs[i].shape[1], 3), dtype=np.uint8), imgs[i], np.zeros((20, imgs[i].shape[1], 3), dtype=np.uint8)])return imgsdef preprocess(video_dir="./assets/videos", save_dir="./assets/coords"):all_videos = natsorted(glob.glob(os.path.join(video_dir, "*.mp4")))for video_path in all_videos:18 unmodified linesnpz = np.load(os.path.join(save_dir, video_name + ".npz"))return npz["coords"]if __name__ == "__main__":preprocess()
app.py+4-4
assets/coords/sample1.npz+2-2
assets/coords/sample2.npz+2-2
assets/coords/sample3.npz+2-2
assets/coords/sample4.npz+2-2
assets/coords/sample5.npzAdded+3
assets/videos/sample5.mp4Added
attributtes_utils.py+13-11
fete_model.py+13-13
inference_util.py+5-1
preprocess_videos.py+7-3