staghado commited on
Commit
bacbeef
1 Parent(s): 1663e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -59,12 +59,12 @@ def setup_animation_env(img_size, desired_range, coefficients):
59
 
60
  return fig, ax, background, circles, circle_lines, drawing
61
 
62
- def animate(frame, coefs, time, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta):
63
  fig.canvas.restore_region(background)
64
 
65
  center = (0, 0)
66
  for idx, (r, fr) in enumerate(coefs_static):
67
- c_dynamic = coefs[idx][0] * np.exp(1j * (fr * tau * time[frame]))
68
  x, y = center[0] + r * np.cos(theta[frame]), center[1] + r * np.sin(theta[frame])
69
  circle_lines[idx].set_data([center[0], center[0] + np.real(c_dynamic)], [center[1], center[1] + np.imag(c_dynamic)])
70
  circles[idx].set_data(x, y)
@@ -89,29 +89,29 @@ def animate(frame, coefs, time, fig, ax, background, circles, circle_lines, draw
89
 
90
  return (pil_image, None)
91
 
92
- def generate_animation(frames, coefs, img_size, desired_range, theta_points, coefficients):
93
  fig, ax, background, circles, circle_lines, drawing = setup_animation_env(img_size, desired_range, coefficients)
94
  print(coefs)
95
  coefs_static = [(np.linalg.norm(c), fr) for c, fr in coefs]
96
- time = np.linspace(0, 1, num=frames)
97
- theta = np.linspace(0, tau, theta_points)
98
  draw_x, draw_y = [], []
99
 
100
- anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta))
101
 
102
- return anim
103
 
104
- def fourier_transform_drawing(input_image, frames, coefficients, img_size, blur_kernel_size, desired_range, num_points, theta_points):
105
  xs, ys = process_image(input_image, img_size, blur_kernel_size, desired_range)
106
  coefs = calculate_fourier_coefficients(xs, ys, num_points, coefficients)
107
- anim = generate_animation(frames, coefs, img_size, desired_range, theta_points, coefficients)
108
 
109
  # Saving the animation
110
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
111
  anim.save(temp_file.name, fps=15)
112
 
113
  for frame in range(frames):
114
- yield (animate(frame, coefs, time, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta), temp_file.name)
115
 
116
  def setup_gradio_interface():
117
  interface = gr.Interface(
@@ -124,7 +124,6 @@ def setup_gradio_interface():
124
  gr.Slider(minimum=3, maximum=11, step=2, value=5, label="Blur Kernel Size (odd number)"),
125
  gr.Number(value=400, label="Desired Range for Scaling", precision=0),
126
  gr.Number(value=1000, label="Number of Points for Integration", precision=0),
127
- gr.Slider(minimum=50, maximum=500, value=80, label="Theta Points for Animation")
128
  ],
129
  outputs=["image", gr.Video()],
130
  title="Fourier Transform Drawing",
 
59
 
60
  return fig, ax, background, circles, circle_lines, drawing
61
 
62
+ def animate(frame, coefs, frame_times, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta):
63
  fig.canvas.restore_region(background)
64
 
65
  center = (0, 0)
66
  for idx, (r, fr) in enumerate(coefs_static):
67
+ c_dynamic = coefs[idx][0] * np.exp(1j * (fr * tau * frame_times[frame]))
68
  x, y = center[0] + r * np.cos(theta[frame]), center[1] + r * np.sin(theta[frame])
69
  circle_lines[idx].set_data([center[0], center[0] + np.real(c_dynamic)], [center[1], center[1] + np.imag(c_dynamic)])
70
  circles[idx].set_data(x, y)
 
89
 
90
  return (pil_image, None)
91
 
92
+ def generate_animation(frames, coefs, img_size, desired_range, coefficients):
93
  fig, ax, background, circles, circle_lines, drawing = setup_animation_env(img_size, desired_range, coefficients)
94
  print(coefs)
95
  coefs_static = [(np.linalg.norm(c), fr) for c, fr in coefs]
96
+ frame_times = np.linspace(0, 1, num=frames)
97
+ theta = np.linspace(0, tau, num=frames)
98
  draw_x, draw_y = [], []
99
 
100
+ anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, frame_times, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta))
101
 
102
+ return anim, frame_times
103
 
104
+ def fourier_transform_drawing(input_image, frames, coefficients, img_size, blur_kernel_size, desired_range, num_points):
105
  xs, ys = process_image(input_image, img_size, blur_kernel_size, desired_range)
106
  coefs = calculate_fourier_coefficients(xs, ys, num_points, coefficients)
107
+ anim, frame_times = generate_animation(frames, coefs, img_size, desired_range, coefficients)
108
 
109
  # Saving the animation
110
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
111
  anim.save(temp_file.name, fps=15)
112
 
113
  for frame in range(frames):
114
+ yield (animate(frame, coefs, frame_times, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta), temp_file.name)
115
 
116
  def setup_gradio_interface():
117
  interface = gr.Interface(
 
124
  gr.Slider(minimum=3, maximum=11, step=2, value=5, label="Blur Kernel Size (odd number)"),
125
  gr.Number(value=400, label="Desired Range for Scaling", precision=0),
126
  gr.Number(value=1000, label="Number of Points for Integration", precision=0),
 
127
  ],
128
  outputs=["image", gr.Video()],
129
  title="Fourier Transform Drawing",