staghado commited on
Commit
2726595
1 Parent(s): 9baccee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -67,7 +67,7 @@ def animate(frame, coefs, frame_times, fig, ax, background, circles, circle_line
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)
71
  center = (center[0] + np.real(c_dynamic), center[1] + np.imag(c_dynamic))
72
 
73
  draw_x.append(center[0])
@@ -91,7 +91,6 @@ def animate(frame, coefs, frame_times, fig, ax, background, circles, circle_line
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
  thetas = np.linspace(0, tau, num=frames)
@@ -100,19 +99,27 @@ def generate_animation(frames, coefs, img_size, desired_range, coefficients):
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, thetas))
101
 
102
  return fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, anim, frame_times, thetas, coefs_static
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
- fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, anim, frame_times, thetas, coefs_static = 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, thetas), temp_file.name)
 
115
 
 
 
 
 
116
  def setup_gradio_interface():
117
  interface = gr.Interface(
118
  fn=fourier_transform_drawing,
 
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])
71
  center = (center[0] + np.real(c_dynamic), center[1] + np.imag(c_dynamic))
72
 
73
  draw_x.append(center[0])
 
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
  coefs_static = [(np.linalg.norm(c), fr) for c, fr in coefs]
95
  frame_times = np.linspace(0, 1, num=frames)
96
  thetas = np.linspace(0, tau, num=frames)
 
99
  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, thetas))
100
 
101
  return fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, anim, frame_times, thetas, coefs_static
102
+
103
  def fourier_transform_drawing(input_image, frames, coefficients, img_size, blur_kernel_size, desired_range, num_points):
104
  xs, ys = process_image(input_image, img_size, blur_kernel_size, desired_range)
105
  coefs = calculate_fourier_coefficients(xs, ys, num_points, coefficients)
 
106
 
107
+ # Setup animation environment
108
+ fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, anim, frame_times, thetas, coefs_static = generate_animation(frames, coefs, img_size, desired_range, coefficients)
109
+
110
+ # Create a temporary file for the video
111
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_file:
112
+ video_path = temp_file.name
113
 
114
+ # Generate and save each frame as a PIL image, and ultimately the video
115
  for frame in range(frames):
116
+ pil_image, _ = animate(frame, coefs, time, fig, ax, background, circles, circle_lines, drawing, draw_x, draw_y, coefs_static, theta)
117
+ yield pil_image, video_path
118
 
119
+ # Save the animation as a video
120
+ 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))
121
+ anim.save(video_path, fps=15)
122
+
123
  def setup_gradio_interface():
124
  interface = gr.Interface(
125
  fn=fourier_transform_drawing,