Yw22 commited on
Commit
469344d
·
1 Parent(s): 794b567

[fix] donwgrade the gradio version to 4.38.1, so that enable the sam

Browse files
Files changed (2) hide show
  1. README.md +1 -1
  2. 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: 5.21.0
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): 椭圆中心的 x 坐标。
402
- y (float): 椭圆中心的 y 坐标。
403
- a (float): 椭圆的短半轴长度。
404
- b (float): 椭圆的长半轴长度。
405
- theta (float): 椭圆的旋转角度(以弧度为单位), 长半轴逆时针角度。
406
-
407
- 返回:
408
- mean (numpy.ndarray): 高斯分布的均值,形状为 (2,) 的数组,表示 (x, y) 坐标。
409
- cov_matrix (numpy.ndarray): 高斯分布的协方差矩阵,形状为 (2, 2) 的数组。
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
- # 不除以 sqrt(2) 也是可以的。这个转换主要是为了在特定的统计上下文中,
418
- # 使得椭圆的半轴长度对应于高斯分布的一个标准差。
419
- # 这样做的目的是为了使得椭圆的面积包含了高斯分布约68%的概率质量(在一维高斯分布中,一个标准差的范围内包含了约68%的概率质量)。
 
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
- # 归一化 mean
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: 输入的物体图像,形状为 (h, w, 3),值范围为 [0, 255]
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)