Spaces:
Runtime error
Runtime error
Schrodingers
commited on
Commit
•
3673b9e
1
Parent(s):
881b68c
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,72 @@
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
-
import gradio as gr
|
4 |
import heapq
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
deltas = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
|
18 |
-
neigh = [(x + dx, y + dy) for dx, dy in deltas]
|
19 |
-
valid = [(x, y) for x, y in neigh if 0 <= x < rows and 0 <= y < cols]
|
20 |
-
return valid
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
current_distance, current_point = heapq.heappop(priority_queue)
|
31 |
-
if current_point == end:
|
32 |
-
break
|
33 |
-
for neighbor in neighbors(current_point, cost_map):
|
34 |
-
alt_distance = current_distance + cost_map[neighbor]
|
35 |
-
if alt_distance < distance[neighbor]:
|
36 |
-
distance[neighbor] = alt_distance
|
37 |
-
prev[neighbor] = current_point
|
38 |
-
heapq.heappush(priority_queue, (alt_distance, neighbor))
|
39 |
|
|
|
40 |
path = []
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
return path
|
48 |
|
49 |
-
def
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
inputs=[gr.inputs.Image(shape=(
|
64 |
-
|
65 |
-
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import cv2
|
3 |
import numpy as np
|
|
|
4 |
import heapq
|
5 |
|
6 |
+
def compute_edge_costs(image):
|
7 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
8 |
+
gradient = cv2.Laplacian(gray, cv2.CV_64F)
|
9 |
+
edge_costs = np.abs(gradient)
|
10 |
+
return edge_costs
|
11 |
|
12 |
+
def dijkstra(edge_costs, start_point, end_point):
|
13 |
+
h, w = edge_costs.shape
|
14 |
+
visited = np.zeros((h, w), dtype=bool)
|
15 |
+
distances = np.full((h, w), float('inf'))
|
16 |
+
distances[start_point[1], start_point[0]] = 0
|
17 |
+
pq = [(0, start_point)]
|
18 |
+
|
19 |
+
while pq:
|
20 |
+
curr_dist, (x, y) = heapq.heappop(pq)
|
21 |
|
22 |
+
if visited[y, x]:
|
23 |
+
continue
|
24 |
+
visited[y, x] = True
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
for dx in [-1, 0, 1]:
|
27 |
+
for dy in [-1, 0, 1]:
|
28 |
+
nx, ny = x + dx, y + dy
|
29 |
+
if 0 <= nx < w and 0 <= ny < h:
|
30 |
+
new_dist = curr_dist + edge_costs[ny, nx]
|
31 |
+
if new_dist < distances[ny, nx]:
|
32 |
+
distances[ny, nx] = new_dist
|
33 |
+
heapq.heappush(pq, (new_dist, (nx, ny)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Backtrack to find path from end to start
|
36 |
path = []
|
37 |
+
curr_point = end_point
|
38 |
+
while curr_point != start_point:
|
39 |
+
x, y = curr_point
|
40 |
+
path.append(curr_point)
|
41 |
+
min_dist = distances[y, x]
|
42 |
+
for dx in [-1, 0, 1]:
|
43 |
+
for dy in [-1, 0, 1]:
|
44 |
+
nx, ny = x + dx, y + dy
|
45 |
+
if 0 <= nx < w and 0 <= ny < h and distances[ny, nx] < min_dist:
|
46 |
+
min_dist = distances[ny, nx]
|
47 |
+
curr_point = (nx, ny)
|
48 |
+
path.append(start_point)
|
49 |
+
|
50 |
return path
|
51 |
|
52 |
+
def segment_image(image, points):
|
53 |
+
edge_costs = compute_edge_costs(image)
|
54 |
+
|
55 |
+
start_point = tuple(map(int, points[0]))
|
56 |
+
end_point = tuple(map(int, points[-1]))
|
57 |
+
|
58 |
+
path = dijkstra(edge_costs, start_point, end_point)
|
59 |
+
|
60 |
+
mask = np.zeros_like(image)
|
61 |
+
for x, y in path:
|
62 |
+
cv2.circle(mask, (x, y), 2, (0, 255, 0), -1)
|
63 |
+
return mask
|
64 |
|
65 |
+
interface = gr.Interface(
|
66 |
+
segment_image,
|
67 |
+
inputs=[gr.inputs.Image(shape=(200, 200)), gr.inputs.Sketchpad()],
|
68 |
+
outputs=gr.outputs.Image(shape=(200, 200)),
|
69 |
+
live=True
|
70 |
)
|
71 |
|
72 |
+
interface.launch()
|
|
|
|