-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
77 lines (63 loc) · 2.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import matplotlib.pyplot as plt
import numpy as np
from classification.classify_shape import classify_shape
from regularization.regularize_contour import regularize_contour
# Define some example colors if not already defined
colours = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] # Add or modify as needed
def read_csv(csv_path):
"""
Read CSV file and parse paths with XY coordinates.
"""
np_path_XYs = np.genfromtxt(csv_path, delimiter=',')
path_XYs = []
for i in np.unique(np_path_XYs[:, 0]):
npXYs = np_path_XYs[np_path_XYs[:, 0] == i][:, 1:]
XYs = []
for j in np.unique(npXYs[:, 0]):
XY = npXYs[npXYs[:, 0] == j][:, 1:]
XYs.append(XY)
path_XYs.append(XYs)
return path_XYs
def plot(paths_XYs):
"""
Plot the shapes from the paths_XYs data.
"""
fig, ax = plt.subplots(tight_layout=True, figsize=(8, 8))
for i, XYs in enumerate(paths_XYs):
c = colours[i % len(colours)] # Choose color based on index
for XY in XYs:
contour = np.array(XY, dtype=np.int32).reshape((-1, 1, 2))
regularized_contour = regularize_contour(contour)
shape_type = classify_shape(regularized_contour)
if shape_type == "Straight line":
# Directly plot the line using the start and end points
x0, y0 = XY[0]
x1, y1 = XY[-1]
ax.plot([x0, x1], [y0, y1], c=c, linewidth=2, label=shape_type)
elif shape_type == 'Unknown Shape' or shape_type == 'Not a Triangle':
ax.plot(XY[:, 0], XY[:, 1], c=c, linewidth=2, label=shape_type)
else:
# Plot the regularized contour for other shapes
# regularized_contour = regularize_contour(contour)
contour_points = regularized_contour.reshape(-1, 2)
if shape_type == "Straight Line":
x0, y0 = XY[0]
x1, y1 = XY[-1]
ax.plot([x0, x1], [y0, y1], c=c, linewidth=2, label=shape_type)
# Ensure the contour is closed
else:
if not np.array_equal(contour_points[0], contour_points[-1]):
contour_points = np.vstack([contour_points, contour_points[0]])
ax.plot(contour_points[:, 0], contour_points[:, 1], c=c, linewidth=2, label=shape_type)
# Annotate the shape type
centroid = np.mean(XY, axis=0)
ax.text(centroid[0], centroid[1], shape_type, fontsize=12, ha='center')
ax.set_aspect('equal') # Ensure aspect ratio is equal
ax.legend()
plt.show() # Display the plot
# Example usage
if __name__ == "__main__":
csv_path = r'D:\python_projects\frag0.csv'
paths_XYs = read_csv(csv_path)
# Plot shapes
plot(paths_XYs)