Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,9 @@ from PIL import Image
|
|
11 |
|
12 |
|
13 |
def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
|
|
|
|
|
|
14 |
# Convert PIL to OpenCV image(array)
|
15 |
input_image = np.array(input_image)
|
16 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
@@ -28,10 +31,11 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
|
28 |
# find the contour with the largest area
|
29 |
largest_contour_idx = np.argmax([cv2.contourArea(c) for c in contours])
|
30 |
largest_contour = contours[largest_contour_idx]
|
31 |
-
|
32 |
verts = [tuple(coord) for coord in contours[largest_contour_idx].squeeze()]
|
33 |
|
34 |
xs, ys = zip(*verts)
|
|
|
35 |
|
36 |
# calculate the range of xs and ys
|
37 |
x_range = np.max(xs) - np.min(xs)
|
@@ -39,46 +43,30 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
|
39 |
|
40 |
# determine the scale factors
|
41 |
desired_range = 400
|
42 |
-
|
43 |
-
|
44 |
|
45 |
# apply scaling
|
46 |
# ys needs to be flipped vertically
|
47 |
-
xs = (
|
48 |
-
ys = (-
|
49 |
-
|
50 |
-
t_list = np.linspace(0, tau, len(xs))
|
51 |
|
52 |
# compute the Fourier coefficients
|
53 |
-
|
54 |
-
|
|
|
55 |
|
56 |
-
def compute_cn(
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
coef = np.trapz(integrand, t_values) / tau
|
64 |
return coef
|
65 |
|
66 |
-
|
67 |
-
# # compute the Fourier coefficients
|
68 |
-
# def f(t, t_list, xs, ys):
|
69 |
-
# return np.interp(t, t_list, xs + 1j*ys)
|
70 |
-
|
71 |
-
# def compute_cn(f, n):
|
72 |
-
# coef = 1/tau*quad_vec(
|
73 |
-
# lambda t: f(t, t_list, xs, ys)*np.exp(-n*t*1j),
|
74 |
-
# 0,
|
75 |
-
# tau,
|
76 |
-
# limit=100,
|
77 |
-
# full_output=False)[0]
|
78 |
-
# return coef
|
79 |
-
|
80 |
N = coefficients
|
81 |
-
coefs = [(compute_cn(
|
82 |
|
83 |
# animate the drawings
|
84 |
fig, ax = plt.subplots()
|
@@ -96,12 +84,11 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
|
96 |
|
97 |
def animate(i, coefs, time):
|
98 |
t = time[i]
|
99 |
-
coefs = [(c * np.exp(1j*(fr * tau * t)), fr) for c, fr in coefs]
|
100 |
center = (0, 0)
|
101 |
-
|
102 |
-
for c,
|
|
|
103 |
r = np.linalg.norm(c)
|
104 |
-
theta = np.linspace(0, tau, 80)
|
105 |
x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
|
106 |
circle_lines[_].set_data([center[0], center[0 ]+ np.real(c)], [center[1], center[1] + np.imag(c)])
|
107 |
circles[_].set_data(x, y)
|
@@ -121,7 +108,6 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
|
121 |
anim.save(output_animation, fps=15)
|
122 |
plt.close(fig)
|
123 |
|
124 |
-
# return the path to the MP4 file
|
125 |
return output_animation
|
126 |
|
127 |
# Gradio interface
|
|
|
11 |
|
12 |
|
13 |
def fourier_transform_drawing(input_image, frames, coefficients, img_size):
|
14 |
+
"""
|
15 |
+
|
16 |
+
"""
|
17 |
# Convert PIL to OpenCV image(array)
|
18 |
input_image = np.array(input_image)
|
19 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
|
|
31 |
# find the contour with the largest area
|
32 |
largest_contour_idx = np.argmax([cv2.contourArea(c) for c in contours])
|
33 |
largest_contour = contours[largest_contour_idx]
|
34 |
+
|
35 |
verts = [tuple(coord) for coord in contours[largest_contour_idx].squeeze()]
|
36 |
|
37 |
xs, ys = zip(*verts)
|
38 |
+
xs, ys = np.asarray(xs), np.asarray(ys)
|
39 |
|
40 |
# calculate the range of xs and ys
|
41 |
x_range = np.max(xs) - np.min(xs)
|
|
|
43 |
|
44 |
# determine the scale factors
|
45 |
desired_range = 400
|
46 |
+
scale_x = desired_range / x_range
|
47 |
+
scale_y = desired_range / y_range
|
48 |
|
49 |
# apply scaling
|
50 |
# ys needs to be flipped vertically
|
51 |
+
xs = (xs - np.mean(xs)) * scale_x
|
52 |
+
ys = (-ys + np.mean(ys)) * scale_y
|
|
|
|
|
53 |
|
54 |
# compute the Fourier coefficients
|
55 |
+
num_points = 1000 # how many points to use for numerical integration
|
56 |
+
t_values = np.linspace(0, tau, num_points)
|
57 |
+
t_list = np.linspace(0, tau, len(xs))
|
58 |
|
59 |
+
def compute_cn(n, t_list, xs, ys):
|
60 |
+
"""
|
61 |
+
Integrate the contour along axis (-1) using the composite trapezoidal rule.
|
62 |
+
https://numpy.org/doc/stable/reference/generated/numpy.trapz.html#r7aa6c77779c0-2
|
63 |
+
"""
|
64 |
+
f_exp = np.interp(t, t_list, xs + 1j * ys) * np.exp(-n * t_values * 1j)
|
65 |
+
coef = np.trapz(f_exp, t_values) / tau
|
|
|
66 |
return coef
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
N = coefficients
|
69 |
+
coefs = [(compute_cn(0, t_list, xs, ys), 0)] + [(compute_cn(j, t_list, xs, ys), j) for i in range(1, N+1) for j in (i, -i)]
|
70 |
|
71 |
# animate the drawings
|
72 |
fig, ax = plt.subplots()
|
|
|
84 |
|
85 |
def animate(i, coefs, time):
|
86 |
t = time[i]
|
|
|
87 |
center = (0, 0)
|
88 |
+
theta = np.linspace(0, tau, 80)
|
89 |
+
for c, fr in coefs:
|
90 |
+
c = c * np.exp(1j*(fr * tau * t)
|
91 |
r = np.linalg.norm(c)
|
|
|
92 |
x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
|
93 |
circle_lines[_].set_data([center[0], center[0 ]+ np.real(c)], [center[1], center[1] + np.imag(c)])
|
94 |
circles[_].set_data(x, y)
|
|
|
108 |
anim.save(output_animation, fps=15)
|
109 |
plt.close(fig)
|
110 |
|
|
|
111 |
return output_animation
|
112 |
|
113 |
# Gradio interface
|