Skip to content

Instantly share code, notes, and snippets.

@arndom
Last active July 17, 2021 16:11
Show Gist options
  • Save arndom/7a40bfd2a5ea9bbcd2f7076bb79ab638 to your computer and use it in GitHub Desktop.
Save arndom/7a40bfd2a5ea9bbcd2f7076bb79ab638 to your computer and use it in GitHub Desktop.
Flask API version for first-order-motion-model by [AliaksandrSiarohin](https://github.com/AliaksandrSiarohin/first-order-model)
import imageio
# imageio.plugins.ffmpeg.download()
import numpy as np
import matplotlib
matplotlib.use('agg')
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")
from demo import load_checkpoints, make_animation
from skimage import img_as_ubyte
from flask import Flask, request,jsonify,send_file,render_template
from flask_cors import CORS, cross_origin
from PIL import Image
import io
import requests
import urllib.request
app = Flask(__name__)
CORS(app, resources = {
r"/*":{
"origins": "*"
}
}, headers='Content-Type') ##added for origin access
app.config['CORS_HEADERS'] = 'Content-Type'
@app.route("/")
def homepage():
return render_template("index.html", title="JUST WORK")
@app.route('/post', methods=['GET', 'POST'])
@cross_origin(origin='*',headers=['Content-Type','Authorization'])
def post():
if request.method == 'POST':
image = request.files['image']
print('image recieved')
print(" ")
print(type(image))
print(" ")
image = image.read()
print('after image read')
print(" ")
print(type(image))
print(" ")
# image = Image.open(requests.get(image, stream=True).raw)
image = Image.open(io.BytesIO(image))
print('after image open')
print(" ")
print(type(image))
print(" ")
image =np.array(image)
image = resize(image, (256, 256))[..., :3]
print("image resized")
print(" ")
video = request.files['video']
print('video recieved')
print(" ")
print(type(video))
print(" ")
video = video.read()
print('video read')
print(" ")
print(type(video))
print(" ")
video = imageio.get_reader(video, 'mp4')
# video = imageio.get_reader(requests.get(video, allow_redirects=True, stream=True).raw, 'mp4')
print('video url open')
print(" ")
print(type(video))
print(" ")
fps = video.get_meta_data()['fps']
print(fps)
driving_video = []
try:
for im in video:
driving_video.append(im)
except RuntimeError:
pass
video.close()
driving_video = [resize(frame, (256, 256))[..., :3] for frame in driving_video]
print('video resized')
# source_image, driving_video,fps = control(image, video)
generator, kp_detector = load_checkpoints(config_path='config/vox-256.yaml',
checkpoint_path='vox-cpk.pth.tar',
cpu=True
)
print("generator done")
predictions = make_animation(source_image=image,
driving_video=driving_video,
generator=generator,
kp_detector=kp_detector,
relative=True,
cpu=True
) #cpu
imageio.mimsave('generatedVideo.mp4',
[img_as_ubyte(frame) for frame in predictions],
fps=fps)
return send_file('generatedVideo.mp4',
as_attachment=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment