/tmp/ipykernel1100758/3723957304.py:41: MatplotlibDeprecationWarning: The getcmap…
- User
/tmp/ipykernel_1100758/3723957304.py:41: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use
matplotlib.colormaps[name]ormatplotlib.colormaps.get_cmap(obj)instead. colors = plt.cm.get_cmap('hsv', len(lines) + 1) - Assistant
The warning message you are encountering indicates that the
get_cmapfunction from Matplotlib is deprecated as of version 3.7 and will be removed in a future release. You should update your code to use the new syntax for fetching colormaps. Here's how you can modify your code accordingly:Change this line:
pythoncolors = plt.cm.get_cmap('hsv', len(lines) + 1)To use one of these options:
python# Option 1: Directly access the colormap by name colors = plt.colormaps['hsv'](range(len(lines) + 1)) # Option 2: Use the get_cmap method from the new location colors = plt.colormaps.get_cmap('hsv', len(lines) + 1)Both of these methods achieve the same goal; they retrieve the colormap and apply it to a specific number of entries. In your specific case, replacing the deprecated function ensures compatibility with future releases of Matplotlib.
- User
TypeError Traceback (most recent call last) Cell In[85], line 141 139 for filename in os.listdir("Sortowanie"): 140 if filename.endswith(".pdf"): --> 141 main(filename[:-4])
Cell In[85], line 127, in main(filename) 125 print(time.time()-time1) 126 text_overlap = reconstruct_text(lines_overlap) --> 127 visualize_with_text_and_image(image, text_overlap, lines_overlap, method='overlap') 130 # 3. Graph-Based Grouping 131 time2 = time.time()
Cell In[84], line 45, in visualize_with_text_and_image(image, reconstructed_text, lines, method) 41 colors = plt.colormaps['hsv'](range(len(lines) + 1)) 44 for idx, line in enumerate(lines): ---> 45 color = tuple([int(255 * c) for c in colors(idx)[:3]]) # Convert to 0-255 scale 46 for elem in line: 47 x1, y1, x2, y2 = elem['x1'], elem['y1'], elem['x2'], elem['y2']
TypeError: 'numpy.ndarray' object is not callable
- Assistant
The error you're encountering,
TypeError: 'numpy.ndarray' object is not callable, arises becausecolorsis an array, but it seems you are trying to call it as if it were a function. This misunderstanding may occur becausecolorsin earlier code could be a function, but with the change, it's now a list or array.To fix the issue, first double-check how you're obtaining and using the colors:
Here's how you can correctly apply colors indexed from a colormap:
python# Utilize the colormap directly colors = plt.colormaps['hsv'](np.linspace(0, 1, len(lines) + 1)) # Use the correct indexing for your loop for idx, line in enumerate(lines): # colors is now an array, and you directly index it color = tuple([int(255 * c) for c in colors[idx][:3]]) # Convert to 0-255 scale for elem in line: x1, y1, x2, y2 = elem['x1'], elem['y1'], elem['x2'], elem['y2'] # Draw or do something with these values and color - User
def cluster_based_grouping(df, distance_threshold=50): """ Group text elements into lines using hierarchical clustering based on y1 and x1 coordinates. Returns a list of lines, each line is a list of dictionaries. """ # Extract coordinates for clustering coordinates = df[['y1', 'x1']].values
code# Apply Agglomerative Clustering clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=distance_threshold, linkage='average') clustering.fit(coordinates) df['cluster'] = clustering.labels_ # Group by cluster and sort each cluster by x1 (left to right) grouped = df.groupby('cluster').apply(lambda x: x.sort_values('x1')) # Convert each group to a list of dictionaries lines = [group.to_dict('records') for name, group in grouped] # Sort lines top to bottom based on the minimum y1 value in each line lines_sorted = sorted(lines, key=lambda line: min(elem['y1'] for elem in line)) return lines_sorted - Assistant
The function
cluster_based_groupingaims to group text elements into lines using hierarchical clustering based on the and coordinates. This process involves a few key steps, including applying agglomerative clustering to identify clusters of elements that likely belong to the same line, sorting these elements within each cluster, and finally organizing the lines from top to bottom. - User
- Assistant
- User
def cluster_based_grouping(df, distance_threshold=50): """ Group text elements into lines using hierarchical clustering based on y1 and x1 coordinates. Returns a list of lines, each line is a list of dictionaries. """ # Extract coordinates for clustering coordinates = df[['y1', 'x1']].values
code# Apply Agglomerative Clustering clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=distance_threshold, linkage='average') clustering.fit(coordinates) df['cluster'] = clustering.labels_ # Group by cluster grouped = df.groupby('cluster') lines = [] for name, group in grouped: # Debug: Print cluster number and number of elements if not isinstance(group, pd.DataFrame): group = group.to_frame().T # Convert Series to DataFrame if necessary # Sort the group by 'x1' (left to right) group_sorted = group.sort_values('x1') # Convert the sorted group to a list of dictionaries group_dict = group_sorted.to_dict('records') # Debug: Print first element of the group to verify structure lines.append(group_dict) # Sort lines top to bottom based on the minimum y1 value in each line lines_sorted = sorted(lines, key=lambda line: min(elem['y1'] for elem in line) if line else 0) return lines_sorted - Assistant