Spaces:
Running
on
Zero
Running
on
Zero
[fix] donwgrade the gradio version to 4.38.1, so that enable the sam
Browse files- README.md +1 -1
- examples/blobctrl/blobctrl_app.py +30 -30
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🦉
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: examples/blobctrl/blobctrl_app.py
|
9 |
pinned: false
|
10 |
python_version: 3.10
|
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.38.1
|
8 |
app_file: examples/blobctrl/blobctrl_app.py
|
9 |
pinned: false
|
10 |
python_version: 3.10
|
examples/blobctrl/blobctrl_app.py
CHANGED
@@ -395,59 +395,59 @@ def _get_ellipse(mask):
|
|
395 |
|
396 |
def ellipse_to_gaussian(x, y, a, b, theta):
|
397 |
"""
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
x (float):
|
402 |
-
y (float):
|
403 |
-
a (float):
|
404 |
-
b (float):
|
405 |
-
theta (float):
|
406 |
-
|
407 |
-
|
408 |
-
mean (numpy.ndarray):
|
409 |
-
cov_matrix (numpy.ndarray):
|
410 |
"""
|
411 |
-
#
|
412 |
mean = np.array([x, y])
|
413 |
|
414 |
-
#
|
415 |
# sigma_x = b / np.sqrt(2)
|
416 |
# sigma_y = a / np.sqrt(2)
|
417 |
-
#
|
418 |
-
#
|
419 |
-
#
|
|
|
420 |
|
421 |
-
#
|
422 |
sigma_x = b
|
423 |
sigma_y = a
|
424 |
-
#
|
425 |
cov_matrix = np.array([[sigma_x**2, 0],
|
426 |
[0, sigma_y**2]])
|
427 |
|
428 |
-
#
|
429 |
R = np.array([[np.cos(theta), -np.sin(theta)],
|
430 |
[np.sin(theta), np.cos(theta)]])
|
431 |
|
432 |
-
#
|
433 |
cov_matrix_rotated = R @ cov_matrix @ R.T
|
434 |
|
435 |
-
cov_matrix_rotated[0, 1] *= -1 #
|
436 |
-
cov_matrix_rotated[1, 0] *= -1 #
|
437 |
|
438 |
# eigenvalues, eigenvectors = np.linalg.eig(cov_matrix_rotated)
|
439 |
|
440 |
return mean, cov_matrix_rotated
|
441 |
|
442 |
-
|
443 |
def normalize_gs(mean, cov_matrix_rotated, width, height):
|
444 |
-
#
|
445 |
normalized_mean = mean / np.array([width, height])
|
446 |
|
447 |
-
#
|
448 |
max_length = np.sqrt(width**2 + height**2)
|
449 |
|
450 |
-
#
|
451 |
normalized_cov_matrix = cov_matrix_rotated / (max_length ** 2)
|
452 |
|
453 |
return normalized_mean, normalized_cov_matrix
|
@@ -694,9 +694,9 @@ def get_object_region_from_mask(mask, original_image):
|
|
694 |
|
695 |
def extract_contours(object_image):
|
696 |
"""
|
697 |
-
|
698 |
-
:param object_image:
|
699 |
-
:return:
|
700 |
"""
|
701 |
# 将图像转换为灰度图
|
702 |
gray_image = cv2.cvtColor(object_image, cv2.COLOR_BGR2GRAY)
|
|
|
395 |
|
396 |
def ellipse_to_gaussian(x, y, a, b, theta):
|
397 |
"""
|
398 |
+
Convert ellipse parameters to mean and covariance matrix of a Gaussian distribution.
|
399 |
+
|
400 |
+
Parameters:
|
401 |
+
x (float): x-coordinate of the ellipse center.
|
402 |
+
y (float): y-coordinate of the ellipse center.
|
403 |
+
a (float): Length of the minor semi-axis of the ellipse.
|
404 |
+
b (float): Length of the major semi-axis of the ellipse.
|
405 |
+
theta (float): Rotation angle of the ellipse (in radians), counterclockwise angle of the major axis.
|
406 |
+
|
407 |
+
Returns:
|
408 |
+
mean (numpy.ndarray): Mean of the Gaussian distribution, an array of shape (2,) representing (x, y) coordinates.
|
409 |
+
cov_matrix (numpy.ndarray): Covariance matrix of the Gaussian distribution, an array of shape (2, 2).
|
410 |
"""
|
411 |
+
# Mean
|
412 |
mean = np.array([x, y])
|
413 |
|
414 |
+
# Diagonal elements of the covariance matrix
|
415 |
# sigma_x = b / np.sqrt(2)
|
416 |
# sigma_y = a / np.sqrt(2)
|
417 |
+
# Not dividing by sqrt(2) is also acceptable. This conversion is mainly for specific statistical contexts,
|
418 |
+
# to make the semi-axis length of the ellipse correspond to one standard deviation of the Gaussian distribution.
|
419 |
+
# The purpose is to make the ellipse area contain about 68% of the probability mass of the Gaussian distribution
|
420 |
+
# (in a one-dimensional Gaussian distribution, one standard deviation contains about 68% of the probability mass).
|
421 |
|
422 |
+
# Diagonal elements of the covariance matrix
|
423 |
sigma_x = b
|
424 |
sigma_y = a
|
425 |
+
# Covariance matrix (before rotation)
|
426 |
cov_matrix = np.array([[sigma_x**2, 0],
|
427 |
[0, sigma_y**2]])
|
428 |
|
429 |
+
# Rotation matrix
|
430 |
R = np.array([[np.cos(theta), -np.sin(theta)],
|
431 |
[np.sin(theta), np.cos(theta)]])
|
432 |
|
433 |
+
# Rotate the covariance matrix
|
434 |
cov_matrix_rotated = R @ cov_matrix @ R.T
|
435 |
|
436 |
+
cov_matrix_rotated[0, 1] *= -1 # Reverse the non-diagonal elements of the covariance matrix
|
437 |
+
cov_matrix_rotated[1, 0] *= -1 # Reverse the non-diagonal elements of the covariance matrix
|
438 |
|
439 |
# eigenvalues, eigenvectors = np.linalg.eig(cov_matrix_rotated)
|
440 |
|
441 |
return mean, cov_matrix_rotated
|
442 |
|
|
|
443 |
def normalize_gs(mean, cov_matrix_rotated, width, height):
|
444 |
+
# Normalize mean
|
445 |
normalized_mean = mean / np.array([width, height])
|
446 |
|
447 |
+
# Calculate maximum length for normalizing the covariance matrix
|
448 |
max_length = np.sqrt(width**2 + height**2)
|
449 |
|
450 |
+
# Normalize covariance matrix
|
451 |
normalized_cov_matrix = cov_matrix_rotated / (max_length ** 2)
|
452 |
|
453 |
return normalized_mean, normalized_cov_matrix
|
|
|
694 |
|
695 |
def extract_contours(object_image):
|
696 |
"""
|
697 |
+
Extract contours from an object image
|
698 |
+
:param object_image: Input object image, shape (h, w, 3), value range [0, 255]
|
699 |
+
:return: Contour image
|
700 |
"""
|
701 |
# 将图像转换为灰度图
|
702 |
gray_image = cv2.cvtColor(object_image, cv2.COLOR_BGR2GRAY)
|