# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import cv2
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
from tqdm import tqdm
from matplotlib import pyplot as plt
import warnings
warnings.filterwarnings("ignore")

# +
source_image = imageio.imread('/home/wintersoldier/Downloads/forDeepFake.jpg')
driving_video = imageio.mimread('/home/wintersoldier/Downloads/my_video3.mp4',memtest=False)
# source_image2 = imageio.imread('/home/wintersoldier/Downloads/chris.jpg')

source_image = resize(source_image, (256, 256))[..., :3]

# source_image2 = resize(source_image2, (256, 256))[..., :3]

source_image = resize(source_image, (256, 256))[..., :3]
driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]


# +
#Resize image and video to 256x256
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())
# -

from demo import load_checkpoints
generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml', 
                            checkpoint_path='/media/wintersoldier/Gaming_Disk/Downloads/vox-cpk.pth.tar')

# +
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

 

# +
from demo import make_animation
# from skimage import img_as_ubyte
cap = cv2.VideoCapture(0)
driving_video=[]

while True:
    if  cv2.waitKey(1) & 0xFF == ord('q'):
        break
    ret, frame = cap.read()
    frame = cv2.resize(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB),(256,256))
    cv2.imshow('the',frame)
    print(len(driving_video))
    driving_video.append(frame)
    if(len(driving_video)==256):
        predictions = make_animation(source_image, driving_video, generator, kp_detector, relative=True)
        try:
            predictions = np.reshape(predictions,(10,256,256,3))
            cv2.imshow('this',predictions[8])
        except:
            pass
        driving_video=[]
    
    
cap.release()
cv2.destroyAllWindows()
    
# -

cap.release()

HTML(display(source_image, driving_video, predictions).to_html5_video())

# +
# main_prediction=np.reshape(main_prediction,(100,256,256,3))

# +
# HTML(display(source_image, driving_video, main_prediction).to_html5_video())
# -

np.shape(temp_driving_video)

temp_driving_video = driving_video[121:122]

# +
from demo import make_animation
from skimage import img_as_ubyte


k=100
for i in range(k, k+1):
    temp_driving_video = driving_video[i:i+15]
    predictions = make_animation(source_image, temp_driving_video, generator, kp_detector, relative=True)
HTML(display(source_image, temp_driving_video, predictions).to_html5_video())

#save resulting video
# imageio.mimsave('../generated.mp4', [img_as_ubyte(frame) for frame in predictions])
#video can be downloaded from /content folder



# +
cap = cv2.VideoCapture(0)
recorded=[]

while True:
    ret, frame = cap.read()
    frame = cv2.resize(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB),(256,256))
    cv2.imshow('the',frame)
    print(len(driving_video))
    recorded.append(frame)
    if  cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
# -

import cv2





plt.imshow(predictions[1])

HTML(display(source_image, driving_video, predictions).to_html5_video())

np.shape(predictions)

for frame in predictions:
    frame = np.array(frame,'uint8')
    cv2.imshow('',frame)
    if  cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()

for frame in predictions:
    frame = np.array(frame,'uint8')
    print(np.shape(frame))

np.shape(predictions)

cv2.destroyAllWindows()

predictions[0][0]


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=[[]]
        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


arr = [img_as_ubyte(frame) for frame in predictions]

# + active=""
#
# -

import time
for frame in arr:
    cv2.imshow('',frame)
    time.sleep(1)
cv2.destroyAllWindows()
    

imageio.mimsave('/home/wintersoldier/Desktop/generated.mp4', [img_as_ubyte(frame) for frame in predictions])

imageio.imsave('/home/wintersoldier/Desktop/generated.png', img_as_ubyte(predictions[4]))

arr=[]
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('', frame)
    if  cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

# +
import urllib
import cv2
import numpy as np
import time
arr=[]

# Replace the URL with your own IPwebcam shot.jpg IP:port
url='http://192.168.1.103:8080/shot.jpg'


while True:
    # Use urllib to get the image from the IP camera
    imgResp = urllib.request.urlopen(url)
    
    # Numpy to convert into a array
    imgNp = np.array(bytearray(imgResp.read()),dtype=np.uint8)
    
    # Finally decode the array to OpenCV usable format ;) 
    img = cv2.imdecode(imgNp,-1)
    img = cv2.resize(img, (256,256))
	# put the image on screen
    cv2.imshow('IPWebcam',img)
    
    arr.append(img)
    predictions = make_animation(source_image, arr, generator, kp_detector, relative=True)
    imageio.imsave('/home/wintersoldier/Desktop/cache.png', img_as_ubyte(predictions[0]))
    readImg= plt.imread('/home/wintersoldier/Desktop/cache.png')
    cv2.imshow('',readImg)
    arr = []
    #To give the processor some less stress
    #time.sleep(0.1) 

    # Quit if q is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cv2.destroyAllWindows()
# -





StarcoderdataJupyterScriptsDedupFiltered