-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmining.py
More file actions
362 lines (306 loc) · 13.8 KB
/
mining.py
File metadata and controls
362 lines (306 loc) · 13.8 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import cv2
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import os
import csv
from PIL import Image
MIN_NODE_AREA = 50 # Adjust the value according to your specific requirements
EDGE_THRESHOLD = 50 # Adjust the value according to your specific requirements
# Function to perform image processing and detect nodes and edges
def image_processing(image):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform edge detection
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
# Check OpenCV version
(major, minor) = cv2.__version__.split('.')[:2]
if int(major) < 4: # For OpenCV version 3.x
_, contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else: # For OpenCV version 4.x or later
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Identify nodes based on contours
nodes = []
for contour in contours:
# Check if contour has at least 3 points
if len(contour) >= 3:
# Calculate area of contour
area = cv2.contourArea(contour)
# Check if contour area is large enough to be considered a node
if area > MIN_NODE_AREA:
# Calculate centroid of contour as node
M = cv2.moments(contour)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
nodes.append((cX, cY))
# Identify edges based on proximity of nodes
edges = []
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
node1 = nodes[i]
node2 = nodes[j]
# Calculate Euclidean distance between nodes
distance = np.sqrt((node1[0] - node2[0])**2 + (node1[1] - node2[1])**2)
# If distance is below a threshold, consider nodes connected by an edge
if distance < EDGE_THRESHOLD:
edges.append((node1, node2))
return nodes, edges
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform edge detection
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
# Check OpenCV version
(major, minor) = cv2.__version__.split('.')[:2]
if int(major) < 4: # For OpenCV version 3.x
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else: # For OpenCV version 4.x or later
_, contours = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Identify nodes based on contours
nodes = []
for contour in contours:
# Check if contour is valid and has enough points
if len(contour) >= 3:
# Calculate area of contour
area = cv2.contourArea(contour)
# Check if contour area is large enough to be considered a node
if area > MIN_NODE_AREA:
# Calculate centroid of contour as node
M = cv2.moments(contour)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
nodes.append((cX, cY))
# Identify edges based on proximity of nodes
edges = []
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
node1 = nodes[i]
node2 = nodes[j]
# Calculate Euclidean distance between nodes
distance = np.sqrt((node1[0] - node2[0])**2 + (node1[1] - node2[1])**2)
# If distance is below a threshold, consider nodes connected by an edge
if distance < EDGE_THRESHOLD:
edges.append((node1, node2))
return nodes, edges
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform edge detection
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
# Check OpenCV version
(major, minor) = cv2.__version__.split('.')[:2]
if int(major) < 4: # For OpenCV version 3.x
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else: # For OpenCV version 4.x or later
_, contours = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Identify nodes based on contours
nodes = []
for contour in contours:
# Check if contour is valid
if len(contour) >= 3: # Ensure contour has at least 3 points
# Calculate area of contour
area = cv2.contourArea(contour)
# Check if contour area is large enough to be considered a node
if area > MIN_NODE_AREA:
# Calculate centroid of contour as node
M = cv2.moments(contour)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
nodes.append((cX, cY))
# Identify edges based on proximity of nodes
edges = []
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
node1 = nodes[i]
node2 = nodes[j]
# Calculate Euclidean distance between nodes
distance = np.sqrt((node1[0] - node2[0])**2 + (node1[1] - node2[1])**2)
# If distance is below a threshold, consider nodes connected by an edge
if distance < EDGE_THRESHOLD:
edges.append((node1, node2))
return nodes, edges
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform edge detection
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
# Check OpenCV version
(major, minor) = cv2.__version__.split('.')[:2]
if int(major) < 4: # For OpenCV version 3.x
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else: # For OpenCV version 4.x or later
_, contours = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Identify nodes based on contours
nodes = []
for contour in contours:
# Calculate area of contour
area = cv2.contourArea(contour)
# Check if contour area is large enough to be considered a node
if area > MIN_NODE_AREA:
# Calculate centroid of contour as node
M = cv2.moments(contour)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
nodes.append((cX, cY))
# Identify edges based on proximity of nodes
edges = []
for i in range(len(nodes)):
for j in range(i + 1, len(nodes)):
node1 = nodes[i]
node2 = nodes[j]
# Calculate Euclidean distance between nodes
distance = np.sqrt((node1[0] - node2[0])**2 + (node1[1] - node2[1])**2)
# If distance is below a threshold, consider nodes connected by an edge
if distance < EDGE_THRESHOLD:
edges.append((node1, node2))
return nodes, edges
def reconstruct_graph(nodes, edges):
# Create an empty graph
graph = nx.Graph()
# Add nodes to the graph
for node in nodes:
graph.add_node(node)
# Add edges to the graph
for edge in edges:
node1, node2 = edge # Assuming edge is a tuple of nodes
graph.add_edge(node1, node2)
return graph
def frequent_subgraph_mining(graphs, output_folder):
os.makedirs(output_folder, exist_ok=True)
num_subgraphs = len(graphs)
for i, subgraph in enumerate(graphs, start=1):
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
plt.close()
print(f"Created subgraph {i}/{num_subgraphs}")
combined_graph = nx.compose_all(graphs)
common_subgraphs = []
for subgraph_nodes in nx.enumerate_all_cliques(combined_graph):
if len(subgraph_nodes) == 3:
subgraph = combined_graph.subgraph(subgraph_nodes)
common_subgraphs.append(subgraph)
num_common_subgraphs = len(common_subgraphs)
for i, subgraph in enumerate(common_subgraphs, start=num_subgraphs + 1):
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
plt.close()
print(f"Created subgraph {i}/{num_common_subgraphs}")
return common_subgraphs
# Create the output folder if it does not exist
os.makedirs(output_folder, exist_ok=True)
# Save each subgraph as a separate PNG image
num_subgraphs = len(graphs)
for i, subgraph in enumerate(graphs, start=1):
# Create a new figure for each subgraph
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
# Save the figure as a PNG image
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
# Close the figure to release memory
plt.close()
# Print indicator
print(f"Created subgraph {i}/{num_subgraphs}")
# Combine all graphs into a single graph
combined_graph = nx.compose_all(graphs)
# Apply frequent subgraph mining algorithm (e.g., subgraph isomorphism)
# For simplicity, let's assume we are looking for all subgraphs of size 3
common_subgraphs = []
for subgraph in nx.enumerate_all_cliques(combined_graph):
if len(subgraph) == 3: # Assuming we are interested in subgraphs of size 3
common_subgraphs.append(subgraph)
# Save each subgraph as a separate PNG image
num_common_subgraphs = len(common_subgraphs)
for i, subgraph in enumerate(common_subgraphs, start=num_subgraphs + 1):
# Create a new figure for each subgraph
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
# Save the figure as a PNG image
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
# Close the figure to release memory
plt.close()
# Print indicator
print(f"Created subgraph {i}/{num_common_subgraphs}")
return common_subgraphs
# Create the output folder if it does not exist
os.makedirs(output_folder, exist_ok=True)
# Save each subgraph as a separate PNG image
for i, subgraph in enumerate(graphs, start=1):
# Create a new figure for each subgraph
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
# Save the figure as a PNG image
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
# Close the figure to release memory
plt.close()
return graphs
# Combine all graphs into a single graph
combined_graph = nx.compose_all(graphs)
# Apply frequent subgraph mining algorithm (e.g., subgraph isomorphism)
# For simplicity, let's assume we are looking for all subgraphs of size 3
common_subgraphs = []
for subgraph in nx.enumerate_all_cliques(combined_graph):
if len(subgraph) == 3: # Assuming we are interested in subgraphs of size 3
common_subgraphs.append(subgraph)
# Save each subgraph as a separate PNG image
for i, subgraph in enumerate(common_subgraphs, start=1):
# Create a new figure for each subgraph
plt.figure()
nx.draw(subgraph, with_labels=True)
plt.title(f"Subgraph {i}")
# Save the figure as a PNG image
subgraph_name = f"subgraph_{i}.png"
subgraph_path = os.path.join(output_folder, subgraph_name)
plt.savefig(subgraph_path)
# Close the figure to release memory
plt.close()
return common_subgraphs
def extract_features_to_csv(common_subgraphs, output_file):
with open(output_file, 'w', newline='') as csvfile:
fieldnames = ['subgraph_id', 'node_count', 'edge_count']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for i, subgraph in enumerate(common_subgraphs, start=1):
node_count = len(subgraph.nodes())
edge_count = len(subgraph.edges())
writer.writerow({
'subgraph_id': f'Subgraph {i}',
'node_count': node_count,
'edge_count': edge_count
})
# Load PNG images of the graphs
graph_images = []
num_graphs = 30 # Update with the total number of graph images
for i in range(1, num_graphs + 1):
image_path = f"E:/Github Repos/Classification-of-Documents-Using-Graph-Based-Features-and-KNN/prepped graphs/graph ({i}).png"
graph_images.append(cv2.imread(image_path))
# Process each graph image
for image in graph_images:
# Perform image processing and detect nodes and edges
nodes, edges = image_processing(image)
# Reconstruct graph from node and edge data
graph = reconstruct_graph(nodes, edges)
# Apply frequent subgraph mining on the reconstructed graph
output_folder = r"E:/Github Repos/Classification-of-Documents-Using-Graph-Based-Features-and-KNN/generated_subgraphs"
common_subgraphs = frequent_subgraph_mining([graph], output_folder)
# Extract features from identified common subgraphs
output_file = os.path.abspath("E:/Github Repos/Classification-of-Documents-Using-Graph-Based-Features-and-KNN/extracted_features.csv")
extract_features_to_csv(common_subgraphs, output_file)
print(f"Features have been saved to {output_file}")