staghado commited on
Commit
c8a65fb
1 Parent(s): 3ea9a92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -23
app.py CHANGED
@@ -8,6 +8,7 @@ import gradio as gr
8
  from scipy.integrate import quad_vec
9
  from math import tau
10
  from PIL import Image
 
11
 
12
 
13
  def fourier_transform_drawing(input_image, frames, coefficients, img_size):
@@ -55,18 +56,41 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
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_values, 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()
@@ -102,23 +126,6 @@ def fourier_transform_drawing(input_image, frames, coefficients, img_size):
102
  draw_y.append(center[1])
103
  drawing.set_data(draw_x[:i+1], draw_y[:i+1])
104
 
105
- # def animate(i, coefs, time):
106
- # t = time[i]
107
- # center = (0, 0)
108
- # theta = np.linspace(0, tau, 80)
109
- # for _, (c, fr) in enumerate(coefs):
110
- # c = c * np.exp(1j*(fr * tau * t))
111
- # r = np.linalg.norm(c)
112
- # x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
113
- # circle_lines[_].set_data([center[0], center[0] + np.real(c)], [center[1], center[1] + np.imag(c)])
114
- # circles[_].set_data(x, y)
115
- # center = (center[0] + np.real(c), center[1] + np.imag(c))
116
-
117
- # draw_x.append(center[0])
118
- # draw_y.append(center[1])
119
-
120
- # drawing.set_data(draw_x[:i+1], draw_y[:i+1])
121
-
122
  drawing_time = 1
123
  time = np.linspace(0, drawing_time, num=frames)
124
  anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
 
8
  from scipy.integrate import quad_vec
9
  from math import tau
10
  from PIL import Image
11
+ from concurrent.futures import ThreadPoolExecutor
12
 
13
 
14
  def fourier_transform_drawing(input_image, frames, coefficients, img_size):
 
56
  num_points = 1000 # how many points to use for numerical integration
57
  t_values = np.linspace(0, tau, num_points)
58
  t_list = np.linspace(0, tau, len(xs))
59
+
60
+ def compute_cn(f_exp, n, t_values):
61
  """
62
  Integrate the contour along axis (-1) using the composite trapezoidal rule.
63
  https://numpy.org/doc/stable/reference/generated/numpy.trapz.html#r7aa6c77779c0-2
64
  """
65
+ coef = np.trapz(f_exp * np.exp(-n * t_values * 1j), t_values) / tau
 
66
  return coef
67
+
68
+ # Pre-compute the interpolated values
69
+ f_exp_precomputed = np.interp(t_values, t_list, xs + 1j * ys)
70
+
71
  N = coefficients
72
+ indices = [0] + [j for i in range(1, N + 1) for j in (i, -i)]
73
+
74
+ print("Number of threads used:", os.cpu_count())
75
+
76
+ # Parallelize the computation of coefficients
77
+ with ThreadPoolExecutor() as executor:
78
+ coefs = list(executor.map(lambda n: (compute_cn(f_exp_precomputed, n, t_values), n), indices))
79
+
80
+ # Ensure the zeroth coefficient is computed only once
81
+ coefs = [(coefs[0][0], 0)] + coefs[1:]
82
+
83
+ # def compute_cn(n, t_list, xs, ys):
84
+ # """
85
+ # Integrate the contour along axis (-1) using the composite trapezoidal rule.
86
+ # https://numpy.org/doc/stable/reference/generated/numpy.trapz.html#r7aa6c77779c0-2
87
+ # """
88
+ # f_exp = np.interp(t_values, t_list, xs + 1j * ys) * np.exp(-n * t_values * 1j)
89
+ # coef = np.trapz(f_exp, t_values) / tau
90
+ # return coef
91
+
92
+ # N = coefficients
93
+ # 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)]
94
 
95
  # animate the drawings
96
  fig, ax = plt.subplots()
 
126
  draw_y.append(center[1])
127
  drawing.set_data(draw_x[:i+1], draw_y[:i+1])
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  drawing_time = 1
130
  time = np.linspace(0, drawing_time, num=frames)
131
  anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))