Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

ERROR: 'list' object cannot be interpreted as an integer; Traceback: File…

  1. User

    ERROR: 'list' object cannot be interpreted as an integer; Traceback: File "/3divi/estimators/test_estimator.py", line 72, in estimator; result = solver.solve(image); File "/tmp/tmpqw0x919d/solver.py", line 11, in solve; result = self.alg.find_corners(image_gray); File "/tmp/tmpqw0x919d/corner_detector/corner_detector.py", line 166, in find_corners; best_lines = self.__get_best_lines(potential_points); File "/tmp/tmpqw0x919d/corner_detector/corner_detector.py", line 118, in __get_best_lines; remaining_points = np.ndarray([[1, 1], [2, 2], [3, 3], [4, 4]]) Что за ошибка?

  2. Assistant

    Ошибка возникла из-за того, что в коде был попытка использовать список вместо целого числа в месте, где ожидалось целое число в аргументе функции или операторе. В данном случае, происходит попытка присвоить списку [[1, 1], [2, 2], [3, 3], [4, 4]] переменной remaining_points, которая, вероятно, должна быть целым числом.

    Для решения этой ошибки, нужно изучить контекст, где возникает эта ошибка и исправить код таким образом, чтобы использовалось целочисленное значение, а не список, в тех местах, где это необходимо.

  3. User

    def __get_best_lines( self, potential_points: np.ndarray ) -> np.ndarray: """ Gets the best combinations of lines by all points. Line y = kx + b. Returns np.ndarray like [[k1, b1], [k2, b2]]. """ remaining_points = 0 best_lines = [] num_lines = 0

        while num_lines < 3:
            if num_lines == 0:
                remaining_points = np.copy(potential_points)
            print(f"remaining points: {len(remaining_points)}")
            best_inlines = []
            best_line = None
    
            if len(remaining_points) == 0:
                remaining_points = np.ndarray([[1, 1], [2, 2], [3, 3], [4, 4]])
    
            for i in range(self.iterations_num):
                sample_indices = np.random.choice(
                    remaining_points.shape[0], size=3, replace=False)
                sample_points = remaining_points[sample_indices]
                sample_x = sample_points[:, 1]
                sample_y = sample_points[:, 0]
                coefficients = np.polyfit(sample_x, sample_y, deg=1)
                distances = np.abs(remaining_points[:, 0] - np.matmul(np.vstack((remaining_points[:, 1], np.ones_like(remaining_points[:, 1]))).T, coefficients)) / np.sqrt(coefficients[0] ** 2 + 1)
                inlines = np.where(distances < self.threshold_distance)[0]
    
                is_similar = False
                for line in best_lines:
                    diff_k = abs(coefficients[0] - line[0])
                    diff_b = abs(coefficients[1] - line[1])
    
                    if diff_k <= self.threshold_k and diff_b <= self.threshold_b:
                        is_similar = True
                        break
    
                if not is_similar and len(inlines) > len(best_inlines):
                    best_line = coefficients
                    best_inlines = inlines
    
            if best_line is not None:
                best_lines.append(best_line)
                remaining_points = np.delete(remaining_points, best_inlines, axis=0)
    
            num_lines += 1
    
        return np.array(best_lines)
    
    def find_corners(self, image: np.ndarray) -> np.ndarray:
        """Finding triangle corners on image process."""
        self.image = image
    
        zero_percentage = ImageService.calculate_zero_pixel_percentage(image)
    
        if zero_percentage > NOISE_LESS:
            self.image_threshold = IMAGE_THRESHOLD_MIN
        else:
            self.image_threshold = IMAGE_THRESHOLD_MAX
    
        coords = self.__get_coords_by_threshold(image)
        neigh_nums = self.__get_neigh_nums_list(coords)
        sorted_neigh_nums = self.__sort_neigh_nums_by_N(neigh_nums)
        potential_points = self.__get_potential_points_coords(sorted_neigh_nums)
        best_lines = self.__get_best_lines(potential_points)
    
        intersection_points = np.array([
            np.linalg.solve(np.array([[best_lines[0][0], -1], [best_lines[1][0], -1]]), np.array([-best_lines[0][1], -best_lines[1][1]])),
            np.linalg.solve(np.array([[best_lines[0][0], -1], [best_lines[2][0], -1]]), np.array([-best_lines[0][1], -best_lines[2][1]])),
            np.linalg.solve(np.array([[best_lines[1][0], -1], [best_lines[2][0], -1]]), np.array([-best_lines[1][1], -best_lines[2][1]]))
        ], dtype=np.float32)
    
        return intersection_points
    

    Вот код этой функции ОШИБКА: ERROR: ‘list’ object cannot be interpreted as an integer; Traceback: File “/3divi/estimators/test_estimator.py”, line 72, in estimator; result = solver.solve(image); File “/tmp/tmpqw0x919d/solver.py”, line 11, in solve; result = self.alg.find_corners(image_gray); File “/tmp/tmpqw0x919d/corner_detector/corner_detector.py”, line 166, in find_corners; best_lines = self.__get_best_lines(potential_points); File “/tmp/tmpqw0x919d/corner_detector/corner_detector.py”, line 118, in __get_best_lines; remaining_points = np.ndarray([[1, 1], [2, 2], [3, 3], [4, 4]])

  4. Assistant

    Ошибкой является восприятие списка в качестве целого числа при попытке объявить переменную remaining_points. Вместо np.ndarray([[1, 1], [2, 2], [3, 3], [4, 4]]) нужно использовать np.array([[1, 1], [2, 2], [3, 3], [4, 4]]).

    Исправленный код выглядит следующим образом:

    if len(remaining_points) == 0:
        remaining_points = np.array([[1, 1], [2, 2], [3, 3], [4, 4]])