maduvantha's picture
Upload app.py
e1ff00c
import imageio
import imageio.v3 as iio
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 asyncio
import warnings
from demo import load_checkpoints
import torch
from animate import normalize_kp
from skimage import img_as_ubyte
from tqdm import tqdm
from scipy.spatial import ConvexHull
from urllib.request import Request, urlopen
from nextcord import Interaction, SlashOption, ChannelType
from nextcord.abc import GuildChannel
from nextcord.ext import commands
import nextcord
client = commands.Bot(command_prefix='!')
testigaerverid=989256818398203945
#aaaaaaaaaaaa
def find_best_frame(source, driving, cpu=False):
import face_alignment
def normalize_kp(kp):
kp = kp - kp.mean(axis=0, keepdims=True)
area = ConvexHull(kp[:, :2]).volume
area = np.sqrt(area)
kp[:, :2] = kp[:, :2] / area
return kp
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=True,
device='cpu' if cpu else 'cuda')
kp_source = fa.get_landmarks(255 * source)[0]
kp_source = normalize_kp(kp_source)
norm = float('inf')
frame_num = 0
for i, image in tqdm(enumerate(driving)):
kp_driving = fa.get_landmarks(255 * image)[0]
kp_driving = normalize_kp(kp_driving)
new_norm = (np.abs(kp_source - kp_driving) ** 2).sum()
if new_norm < norm:
norm = new_norm
frame_num = i
return frame_num
#sssssssssssss
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
#Resize image and video to 256x256
def dy():
for i in range( 10):
i=i+1
time.sleep(5)
yield str(i)
@client.slash_command(name="repeat", description="whatever doyou want",guild_ids=[testigaerverid])
async def repeat(interaction : Interaction, message:str, message2:str):
channel = interaction.channel
await interaction.response.send_message("jj")
warnings.filterwarnings("ignore")
web_image = message
request_site = Request(web_image, headers={"User-Agent": "Mozilla/5.0"})
source_image =iio.imread(urlopen(request_site).read())
urlforvideo=message2
driving_video = iio.imread(urlforvideo)
source_image = resize(source_image, (256, 256))[..., :3]
driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
eg,cd=load_checkpoints('config//vox-256.yaml','vox-cpk.pth.tar',True)
cpu=True
print("came to this")
with torch.no_grad():
predictions = []
source = torch.tensor(source_image[np.newaxis].astype(np.float32)).permute(0, 3, 1, 2)
if not cpu:
source = source.cuda()
driving = torch.tensor(np.array(driving_video)[np.newaxis].astype(np.float32)).permute(0, 4, 1, 2, 3)
kp_source = cd(source)
kp_driving_initial = cd(driving[:, :, 0])
progress_message = await channel.send("Progress: 0%")
for frame_idx in tqdm(range(driving.shape[2])):
driving_frame = driving[:, :, frame_idx]
if not cpu:
driving_frame = driving_frame.cuda()
kp_driving = cd(driving_frame)
kp_norm = normalize_kp(kp_source=kp_source, kp_driving=kp_driving,
kp_driving_initial=kp_driving_initial, use_relative_movement=True,
use_relative_jacobian=True, adapt_movement_scale=True)
out = eg(source, kp_source=kp_source, kp_driving=kp_norm)
predictions.append(np.transpose(out['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0])
await progress_message.edit(content=f"{frame_idx}")
print("print karandath puluvan bola")
imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions])
@client.event
async def on_ready():
print("Bot is connected")
client.run("MTA1OTcwODIxNTY5MDAwNjU5OA.GjNyS_.QNudUyA7G-gHbMZPQDuPWIQdmldKFJOi5c6AdI")