Mấy năm trở lại đây, từ khóa Deepfake không còn xa lạ gì với mọi người chúng ta. Sự ra đời của rất nhiều ứng dụng giải trí sử dụng công nghệ này để ghép ảnh chân dung của một người khác vào khuôn mặt chuyển động của mình khiến mọi người thích thú. Hay những video giả các nhà lãnh đạo nổi tiếng thế giới phát biểu, các video “nóng” ghép khuôn mặt của nghệ sỹ nổi tiếng.
Hôm nay chúng ta thử vọc vạch công nghệ Deepfake với bài viết mang tên Thử giả Sơn Tùng MTP bằng Deepfake.
Trước tiên chúng ta tìm hiểu sơ qua về công nghệ Deepfake là gì nhé.
Deepfake là gì?
Deepfake là cụm từ được kết hợp giữa 2 từ “deep learning” và “fake”, là phương thức tạo ra các sản phẩm công nghệ giả dưới dạng âm thanh, hình ảnh hay video, bằng việc sử dụng trí tuệ nhân tạo (AI), cụ thể là học sâu (Deep Learning).
Hình ảnh khuôn mặt của một đối tượng A nhất định được thay thế hoàn toàn bằng khuôn mặt của một đối tượng B khác. Deepfake sẽ mã hóa và tìm học tất cả những điểm tương đồng giữa hai đối tượng này và loại bỏ những điểm khác biệt đi, sau đó nén những bức ảnh đó lại. Ảnh nén của đối tượng A được đưa vào bộ giải mã của đối tượng B. Bộ giải mã sau đó tái tạo lại khuôn mặt của B với biểu cảm và hướng khuôn mặt của A. Quá trình này được thực hiện liên tục cho đến khi ra sản phẩm chân thật nhất.
Thử giả Sơn Tùng MTP bằng Deepfake
Hiện tại có rất nhiều model được đào tạo dùng cho công nghệ Deepfake, trong bài này mình sử dụng First Order Motion Model.
Chúng ta sẽ tạo 1 Notebook trên Google Colab để thực hiện nhé.
Đầu tiên, chúng ta sẽ clone model và mount Google Drive với Colab.
1 2 3 4 5 6 |
!git clone https://github.com/AliaksandrSiarohin/first-order-model cd first-order-model from google.colab import drive drive.mount('/content/gdrive') |
Kế đến là load driving video và source image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import imageio import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from skimage.transform import resize from IPython.display import HTML import warnings warnings.filterwarnings("ignore") source_image = imageio.imread('/content/gdrive/My Drive/first-order-motion-model/sontung-256.jpg') driving_video = imageio.mimread('/content/gdrive/My Drive/first-order-motion-model/video-256.mp4') #Resize image and video to 256x256 source_image = resize(source_image, (256, 256))[..., :3] driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video] def display(source, driving, generated=None): fig = plt.figure(figsize=(8 + 4 * (generated is not None), 6)) ims = [] for i in range(len(driving)): cols = [source] cols.append(driving[i]) if generated is not None: cols.append(generated[i]) im = plt.imshow(np.concatenate(cols, axis=1), animated=True) plt.axis('off') ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=1000) plt.close() return ani HTML(display(source_image, driving_video).to_html5_video()) |
Các bạn nhớ up 1 tấm hình của Sơn Tùng MTP và video của bạn lên Drive nhé. Ở đây mình đặt tên cho 2 file này là “sontung-256.jpg” và “video-256.mp4“.
Bây giờ, chúng ta tạo model và load checkpoints
1 2 3 |
from demo import load_checkpoints generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml', checkpoint_path='/content/gdrive/My Drive/first-order-motion-model/vox-cpk.pth.tar') |
Tiếp theo, tạo chuyển động cho hình ảnh
1 2 3 4 5 6 7 8 9 10 |
from demo import make_animation from skimage import img_as_ubyte predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True) #save resulting video imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions]) #video can be downloaded from /content folder HTML(display(source_image, driving_video, predictions).to_html5_video()) |
Cuối cùng, chúng ta chuyển video đã tạo về Drive.
1 |
!ffmpeg -i /content/gdrive/My\ Drive/first-order-motion-model/sontung.mp4 -ss 00:00:00 -t 00:00:09 -filter:v "crop=600:600:760:50" -async 1 sontung.mp4 |
Ở đây, mình dùng ffmpeg để cắt ra video 9s với kích thước 600 x 600 và lưu lại với tên “sontung.mp4“.
Xong, chúng ta thử chạy lên xem sao nhé!
1 2 3 4 5 6 7 8 9 10 11 12 13 |
source_image = imageio.imread('/content/gdrive/My Drive/first-order-motion-model/sontung-256.jpg') driving_video = imageio.mimread('sontung.mp4', memtest=False) #Resize image and video to 256x256 source_image = resize(source_image, (256, 256))[..., :3] driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video] predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True, adapt_movement_scale=True) HTML(display(source_image, driving_video, predictions).to_html5_video()) |
Video
Leave a Reply