Commit
·
cfb3a93
1
Parent(s):
acc4474
Update README.md
Browse files
README.md
CHANGED
@@ -1,27 +1,48 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
git clone https://github.com/cliport/cliport.git
|
5 |
-
from susie.model import create_sample_fn
|
6 |
-
from susie.jax_utils import initialize_compilation_cache
|
7 |
-
import requests
|
8 |
import numpy as np
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import product, chain
|
2 |
+
import math
|
3 |
+
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
+
import networkx as nx
|
6 |
+
import cv2
|
7 |
+
im = cv2.imread("/kaggle/working/black.png")
|
8 |
+
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
9 |
|
10 |
+
h, w = im.shape # Get height and width of image
|
11 |
|
12 |
+
# Add only those nodes which are black to graph
|
13 |
+
nodes = [(i, j) for (i, j) in product(range(h), range(w)) if im[i, j] == 0]
|
14 |
+
g = nx.Graph(nodes)
|
15 |
|
16 |
+
# For each node there can be 8 neighbours, if you consider diagonal as well.
|
17 |
+
def get_neighbors(node):
|
18 |
+
box_coords = product([-1, 0, 1], [-1, 0, 1])
|
19 |
+
nns = []
|
20 |
+
for coord in box_coords:
|
21 |
+
if coord[0] != coord[1]:
|
22 |
+
nn = (node[0] - coord[0], node[1] - coord[1])
|
23 |
+
nns.append(nn)
|
24 |
+
return nns
|
25 |
|
26 |
+
# A point will be a neighbour if it is black as well and is in image bounds
|
27 |
+
neighbors = list(chain.from_iterable([[(node, ng_node, 1) for ng_node in get_neighbors(node) if (im[node] == 0) and (0 < ng_node[0] < h) and (0 < ng_node[1] , w)] for node in nodes]))
|
28 |
+
|
29 |
+
g.add_weighted_edges_from(neighbors)
|
30 |
+
|
31 |
+
# In image loaded above (0, 0) is top left point. To keep things little more generic. I select point closest to (0, 0) as start and furthest as end.
|
32 |
+
|
33 |
+
min_pt = min(nodes, key=lambda x: math.hypot(x[0], x[1]))
|
34 |
+
max_pt = max(nodes, key=lambda x: math.hypot(x[0], x[1]))
|
35 |
+
|
36 |
+
# Now we can just use networkx to find path between two points
|
37 |
+
path = nx.shortest_path(g, source=min_pt, target=max_pt)
|
38 |
+
|
39 |
+
# Get new image with only shortest path
|
40 |
+
im2 = 255*np.ones_like(im)
|
41 |
+
# Draw the path on the original image before saving
|
42 |
+
# Draw the path on the original image with white color
|
43 |
+
for i in range(len(path)-1):
|
44 |
+
cv2.line(im, path[i], path[i+1], color=(255), thickness=1)
|
45 |
+
|
46 |
+
cv2.imwrite('image_with_path.png', im)
|
47 |
+
plt.figure(figsize=(10, 10))
|
48 |
+
plt.imshow(im, cmap='gray')
|