| import os |
| os.system("pip install gradio==2.9.4") |
| import gradio as gr |
| import numpy as np |
| import cv2 as cv |
| import sys |
| import math |
|
|
| """ |
| The following rotate function is only included for reference and is not called. |
| This shows how the recursion works, but does not provide any sort of interesting animation. |
| """ |
| def rotate(img, x, y, width): |
|
|
| width = width // 2 |
| |
| |
| temp = np.copy(img[y:y + width, x:x + width]) |
| |
| img[y:y + width, x:x + width] = img[y + width:y + 2 * width, x:x + width] |
| img[y + width:y + 2 * width, x:x + width] = img[y + width:y + 2 * width, x + width:x + 2 * width] |
| img[y + width:y + 2 * width, x + width:x + 2 * width] = img[y:y + width, x + width:x + 2 * width] |
| img[y:y + width, x + width:x + 2 * width] = temp |
| |
| if width > 1: |
| |
| rotate(img, x, y, width) |
| rotate(img, x + width, y, width) |
| rotate(img, x + width, y + width, width) |
| rotate(img, x, y + width, width) |
|
|
| def rotate_with_steps(img, output, x, y, width, shift): |
| |
| temp = np.copy(img[y:y + width, x:x + width]) |
| |
| output[y + width - shift:y + 2 * width - shift, x:x + width] = img[y + width:y + 2 * width, x:x + width] |
| output[y + width:y + 2 * width, x + width - shift:x + 2 * width - shift] = img[y + width:y + 2 * width, x + width:x + 2 * width] |
| output[y + shift:y + width + shift, x + width:x + 2 * width] = img[y:y + width, x + width:x + 2 * width] |
| output[y:y + width, x + shift:x + width + shift] = temp |
|
|
| def is_power_of_two(n): |
| return (n != 0) and (n & (n-1) == 0) |
|
|
|
|
|
|
| |
| def run(image): |
| image = cv.imread(image) |
| |
| |
| if not (is_power_of_two(image.shape[0]) and image.shape[0] == image.shape[1]): |
| print("The image you have provided does not have dimensions N x N where N is a power of 2.") |
| closest_valid_dimension = 2**round(math.log(min(image.shape[0], image.shape[1]), 2)) |
| |
| |
| image = cv.resize(image, (closest_valid_dimension, closest_valid_dimension)) |
| |
| |
| img_dim = image.shape[0] |
| |
| |
| out_file_name = "out.mp4" |
| out = cv.VideoWriter(out_file_name, cv.VideoWriter_fourcc('m','p','4','v'), 30, (img_dim, img_dim)) |
| |
| |
| for rotations in range(4): |
| |
| width = img_dim // 2 |
| number_of_frames = 2 * int(math.log2(img_dim)) |
| |
| while width > 0: |
| |
| number_of_frames -= 2 |
| number_of_frames = max(1, number_of_frames) |
| |
| |
| for i in range(0, number_of_frames): |
| |
| output_canvas = np.copy(image) |
| shift = (width * (i + 1)) // number_of_frames |
| |
| for x in range(0, img_dim, 2 * width): |
| for y in range(0, img_dim, 2 * width): |
| rotate_with_steps(image, output_canvas, x, y, width, shift) |
| |
| |
| |
| out.write(output_canvas) |
| |
| image = np.copy(output_canvas) |
| width = width // 2 |
| |
| out.release() |
| return "out.mp4" |
| |
| gr.Interface(run,gr.inputs.Image(type="filepath"),"playable_video").launch() |
|
|