face_morphing / app.py
akopiler's picture
Update app.py
a6d7828 verified
import gradio as gr
import numpy as np
import time
import os
import cv2 as cv
#from cv2 import (VideoCapture, imshow, waitKey, destroyAllWindows,
#CAP_PROP_FRAME_WIDTH, CAP_PROP_FRAME_HEIGHT, CAP_PROP_FPS, COLOR_RGB2BGR, cvtColor, INTER_LINEAR)
cwd = os.getcwd()
print(cwd)
#Hinton = os.path.join(cwd, "\Geoffrey_Hinton.jpeg")
#Lecun = os.path.join(cwd, "\Yann_LeCun.jpeg")
#Morph = os.path.join(cwd, "\video.mp4")
Hinton=cwd + "/Geoffrey_Hinton.jpeg"
Lecun=cwd + "/Yann_LeCun.jpeg"
Morph=cwd + "/video.mp4"
print(Lecun)
print(Hinton)
print(Morph)
# Create video capture object
capture = cv.VideoCapture(Morph)
# Check that a camera connection has been established
if not capture.isOpened():
print("Error opening video file")
else:
# Get video properties and print them
frame_width = capture.get(cv.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv.CAP_PROP_FPS)
print("Image frame width: ", int(frame_width))
print("Image frame height: ", int(frame_height))
print("Frame rate: ", int(fps))
def morphing(inp, target, slider):
capture = cv.VideoCapture(Morph)
for i in range(slider):
print(i)
# time.sleep(0.05)
# Read an image frame
ret, frame = capture.read()
# print(len(frame))
if ret:
image = cv.cvtColor(frame, cv.COLOR_RGB2BGR)
# Set rows and columns
# lets downsize the image using new width and height
down_width = int(frame_width/4)
down_height = int(frame_height/2)
down_points = (down_width, down_height)
# image = cv.resize(image, (720,1280), interpolation= cv.INTER_LINEAR)
yield image
# image = np.ones((240,240,3), np.uint8)
# image[:] = [255, 124, 0]
# yield image
ww= 256 #int(frame_width/2)
hh= 256 #int(frame_height/2)
with gr.Blocks(theme=gr.themes.Default(font=[gr.themes.GoogleFont("Inconsolata"), "Arial", "sans-serif"])) as demo:
gr.Markdown("Start choosing an input and target image for the morphing then click **Submit** to watch the output in slow motion.")
with gr.Row():
inp = gr.Image(label="Input", value=Lecun, width=ww, height=hh) # width=int(frame_width),height=int(frame_height))
target = gr.Image(label="Target", value=Hinton, width=ww, height=hh) # width=int(frame_width),height=int(frame_height))
out = gr.Image(label="Output", width=int(frame_width/2),height=int(frame_height/3)) # width=ww, height=hh) # width=int(frame_width),height=int(frame_height))
slider= gr.Slider(1, 1000, value=300, label="Steps", info="Choose between 1 and 1000")
btn = gr.Button("Submit")
btn.click(fn=morphing, inputs=[inp,target,slider], outputs=out)
gr.ClearButton([inp,target,slider,out])
gr.Examples(
examples=[[Lecun, Hinton, 300], [Lecun, Hinton, 150]],
inputs=[inp, target, slider],
outputs=out,
fn=morphing,
cache_examples=True,
)
if __name__ == "__main__":
demo.queue()
demo.launch()