Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import matplotlib.animation as animation
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
from scipy.integrate import quad_vec
|
7 |
+
from math import tau
|
8 |
+
import os
|
9 |
+
|
10 |
+
def fourier_transform_drawing(input_image, output_animation, frames, coefficients):
|
11 |
+
# Convert input_image to an OpenCV image
|
12 |
+
input_image = np.array(input_image)
|
13 |
+
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
14 |
+
|
15 |
+
# processing
|
16 |
+
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
17 |
+
blurred = cv2.GaussianBlur(imgray, (7, 7), 0)
|
18 |
+
|
19 |
+
(T, thresh) = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
|
20 |
+
|
21 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
22 |
+
largest_contour_idx = np.argmax([len(c) for c in contours])
|
23 |
+
verts = [tuple(coord) for coord in contours[largest_contour_idx].squeeze()]
|
24 |
+
|
25 |
+
xs, ys = zip(*verts)
|
26 |
+
xs = np.asarray(xs) - np.mean(xs)
|
27 |
+
ys = - np.asarray(ys) + np.mean(ys)
|
28 |
+
t_list = np.linspace(0, tau, len(xs))
|
29 |
+
|
30 |
+
# Compute the Fourier coefficients
|
31 |
+
def f(t, t_list, xs, ys):
|
32 |
+
return np.interp(t, t_list, xs + 1j*ys)
|
33 |
+
|
34 |
+
def compute_cn(f, n):
|
35 |
+
coef = 1/tau*quad_vec(
|
36 |
+
lambda t: f(t, t_list, xs, ys)*np.exp(-n*t*1j),
|
37 |
+
0,
|
38 |
+
tau,
|
39 |
+
limit=100,
|
40 |
+
full_output=False)[0]
|
41 |
+
return coef
|
42 |
+
|
43 |
+
N = coefficients
|
44 |
+
coefs = [(compute_cn(f, 0), 0)] + [(compute_cn(f, j), j) for i in range(1, N+1) for j in (i, -i)]
|
45 |
+
|
46 |
+
# animate the drawings
|
47 |
+
fig, ax = plt.subplots()
|
48 |
+
circles = [ax.plot([], [], 'b-')[0] for _ in range(-N, N+1)]
|
49 |
+
circle_lines = [ax.plot([], [], 'g-')[0] for _ in range(-N, N+1)]
|
50 |
+
drawing, = ax.plot([], [], 'r-', linewidth=2)
|
51 |
+
|
52 |
+
ax.set_xlim(-500, 500)
|
53 |
+
ax.set_ylim(-500, 500)
|
54 |
+
ax.set_axis_off()
|
55 |
+
ax.set_aspect('equal')
|
56 |
+
fig.set_size_inches(15, 15)
|
57 |
+
|
58 |
+
draw_x, draw_y = [], []
|
59 |
+
|
60 |
+
def animate(i, coefs, time):
|
61 |
+
t = time[i]
|
62 |
+
coefs = [(c * np.exp(1j*(fr * tau * t)), fr) for c, fr in coefs]
|
63 |
+
center = (0, 0)
|
64 |
+
|
65 |
+
for c, _ in coefs:
|
66 |
+
r = np.linalg.norm(c)
|
67 |
+
theta = np.linspace(0, tau, 80)
|
68 |
+
x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
|
69 |
+
circle_lines[_].set_data([center[0], center[0]+np.real(c)], [center[1], center[1]+np.imag(c)])
|
70 |
+
circles[_].set_data(x, y)
|
71 |
+
center = (center[0] + np.real(c), center[1] + np.imag(c))
|
72 |
+
|
73 |
+
draw_x.append(center[0])
|
74 |
+
draw_y.append(center[1])
|
75 |
+
drawing.set_data(draw_x, draw_y)
|
76 |
+
|
77 |
+
drawing_time = 1
|
78 |
+
time = np.linspace(0, drawing_time, num=frames)
|
79 |
+
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
|
80 |
+
|
81 |
+
anim.save(output_animation, fps=15)
|
82 |
+
plt.close(fig)
|
83 |
+
|
84 |
+
return output_animation
|
85 |
+
|
86 |
+
# Gradio interface
|
87 |
+
interface = gr.Interface(
|
88 |
+
fn=fourier_transform_drawing,
|
89 |
+
inputs=[
|
90 |
+
gr.inputs.Image(label="Input Image"),
|
91 |
+
gr.inputs.Textbox(default="output.mp4", label="Output Animation Path"),
|
92 |
+
gr.inputs.Slider(minimum=10, maximum=500, default=300, label="Number of Frames"),
|
93 |
+
gr.inputs.Slider(minimum=10, maximum=500, default=300, label="Number of Coefficients")
|
94 |
+
],
|
95 |
+
outputs="file",
|
96 |
+
title="Fourier Transform Drawing",
|
97 |
+
description="Upload an image and generate a Fourier Transform drawing animation."
|
98 |
+
)
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
interface.launch()
|