Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,17 @@
|
|
1 |
-
import
|
|
|
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 |
from PIL import Image
|
10 |
-
|
11 |
|
12 |
def fourier_transform_drawing(input_image, frames, coefficients):
|
13 |
-
# Convert
|
14 |
input_image = np.array(input_image)
|
15 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
16 |
|
@@ -18,26 +19,30 @@ def fourier_transform_drawing(input_image, frames, coefficients):
|
|
18 |
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
19 |
blurred = cv2.GaussianBlur(imgray, (7, 7), 0)
|
20 |
|
21 |
-
|
|
|
22 |
|
|
|
23 |
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
|
24 |
largest_contour_idx = np.argmax([len(c) for c in contours])
|
25 |
verts = [tuple(coord) for coord in contours[largest_contour_idx].squeeze()]
|
26 |
|
27 |
xs, ys = zip(*verts)
|
28 |
-
xs = np.asarray(xs) - np.mean(xs)
|
29 |
-
ys = - np.asarray(ys) + np.mean(ys)
|
30 |
|
31 |
-
#
|
32 |
x_range = np.max(xs) - np.min(xs)
|
33 |
y_range = np.max(ys) - np.min(ys)
|
34 |
|
35 |
-
#
|
36 |
-
desired_range =
|
37 |
scale_factor_x = desired_range / x_range
|
38 |
scale_factor_y = desired_range / y_range
|
39 |
|
40 |
-
#
|
41 |
xs = (xs - np.mean(xs)) * scale_factor_x
|
42 |
ys = (ys - np.mean(ys)) * scale_factor_y
|
43 |
|
@@ -65,11 +70,11 @@ def fourier_transform_drawing(input_image, frames, coefficients):
|
|
65 |
circle_lines = [ax.plot([], [], 'g-')[0] for _ in range(-N, N+1)]
|
66 |
drawing, = ax.plot([], [], 'r-', linewidth=2)
|
67 |
|
68 |
-
ax.set_xlim(-
|
69 |
-
ax.set_ylim(-
|
70 |
ax.set_axis_off()
|
71 |
-
ax.set_aspect('equal')
|
72 |
-
fig.set_size_inches(15, 15)
|
73 |
|
74 |
draw_x, draw_y = [], []
|
75 |
|
@@ -82,7 +87,7 @@ def fourier_transform_drawing(input_image, frames, coefficients):
|
|
82 |
r = np.linalg.norm(c)
|
83 |
theta = np.linspace(0, tau, 80)
|
84 |
x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
|
85 |
-
circle_lines[_].set_data([center[0], center[0]+np.real(c)], [center[1], center[1]+np.imag(c)])
|
86 |
circles[_].set_data(x, y)
|
87 |
center = (center[0] + np.real(c), center[1] + np.imag(c))
|
88 |
|
@@ -94,12 +99,12 @@ def fourier_transform_drawing(input_image, frames, coefficients):
|
|
94 |
time = np.linspace(0, drawing_time, num=frames)
|
95 |
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
|
96 |
|
97 |
-
#
|
98 |
output_animation = "output.mp4"
|
99 |
anim.save(output_animation, fps=15)
|
100 |
plt.close(fig)
|
101 |
|
102 |
-
#
|
103 |
return output_animation
|
104 |
|
105 |
# Gradio interface
|
|
|
1 |
+
import os
|
2 |
+
import io
|
3 |
import cv2
|
4 |
import matplotlib.animation as animation
|
5 |
import matplotlib.pyplot as plt
|
6 |
import numpy as np
|
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):
|
14 |
+
# Convert PIL to OpenCV image(array)
|
15 |
input_image = np.array(input_image)
|
16 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
17 |
|
|
|
19 |
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
20 |
blurred = cv2.GaussianBlur(imgray, (7, 7), 0)
|
21 |
|
22 |
+
# apply Otsu threshold
|
23 |
+
(_, thresh) = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
|
24 |
|
25 |
+
# find contours of the binary image
|
26 |
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
27 |
+
|
28 |
+
# take only the largest contour
|
29 |
largest_contour_idx = np.argmax([len(c) for c in contours])
|
30 |
verts = [tuple(coord) for coord in contours[largest_contour_idx].squeeze()]
|
31 |
|
32 |
xs, ys = zip(*verts)
|
33 |
+
#xs = np.asarray(xs) - np.mean(xs)
|
34 |
+
#ys = - np.asarray(ys) + np.mean(ys)
|
35 |
|
36 |
+
# calculate the range of xs and ys
|
37 |
x_range = np.max(xs) - np.min(xs)
|
38 |
y_range = np.max(ys) - np.min(ys)
|
39 |
|
40 |
+
# determine the scale factors
|
41 |
+
desired_range = 400
|
42 |
scale_factor_x = desired_range / x_range
|
43 |
scale_factor_y = desired_range / y_range
|
44 |
|
45 |
+
# apply scaling
|
46 |
xs = (xs - np.mean(xs)) * scale_factor_x
|
47 |
ys = (ys - np.mean(ys)) * scale_factor_y
|
48 |
|
|
|
70 |
circle_lines = [ax.plot([], [], 'g-')[0] for _ in range(-N, N+1)]
|
71 |
drawing, = ax.plot([], [], 'r-', linewidth=2)
|
72 |
|
73 |
+
ax.set_xlim(-desired_range, desired_range)
|
74 |
+
ax.set_ylim(-desired_range, desired_range)
|
75 |
ax.set_axis_off()
|
76 |
+
#ax.set_aspect('equal')
|
77 |
+
#fig.set_size_inches(15, 15)
|
78 |
|
79 |
draw_x, draw_y = [], []
|
80 |
|
|
|
87 |
r = np.linalg.norm(c)
|
88 |
theta = np.linspace(0, tau, 80)
|
89 |
x, y = center[0] + r * np.cos(theta), center[1] + r * np.sin(theta)
|
90 |
+
circle_lines[_].set_data([center[0], center[0 ]+ np.real(c)], [center[1], center[1] + np.imag(c)])
|
91 |
circles[_].set_data(x, y)
|
92 |
center = (center[0] + np.real(c), center[1] + np.imag(c))
|
93 |
|
|
|
99 |
time = np.linspace(0, drawing_time, num=frames)
|
100 |
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
|
101 |
|
102 |
+
# save the animation as an MP4 file
|
103 |
output_animation = "output.mp4"
|
104 |
anim.save(output_animation, fps=15)
|
105 |
plt.close(fig)
|
106 |
|
107 |
+
# return the path to the MP4 file
|
108 |
return output_animation
|
109 |
|
110 |
# Gradio interface
|