Schrodingers commited on
Commit
3673b9e
1 Parent(s): 881b68c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -56
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 compute_cost_map(image):
8
- grayscale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
9
- sobelx = cv2.Sobel(grayscale, cv2.CV_64F, 1, 0, ksize=5)
10
- sobely = cv2.Sobel(grayscale, cv2.CV_64F, 0, 1, ksize=5)
11
- magnitude = cv2.magnitude(sobelx, sobely)
12
- return np.max(magnitude) - magnitude
 
 
 
13
 
14
- def neighbors(point, cost_map):
15
- x, y = point
16
- rows, cols = cost_map.shape
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
- def compute_shortest_path(cost_map, start, end):
23
- rows, cols = cost_map.shape
24
- distance = np.full((rows, cols), np.inf)
25
- distance[start] = 0
26
- prev = {}
27
- priority_queue = [(0, start)]
28
-
29
- while priority_queue:
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
- while current_point in prev:
42
- path.append(current_point)
43
- current_point = prev[current_point]
44
- path.append(start)
45
- path.reverse()
46
-
 
 
 
 
 
 
 
47
  return path
48
 
49
- def live_wire_segmentation(image, points):
50
- cost_map = compute_cost_map(image)
51
- path = []
52
- for i in range(1, len(points)):
53
- segment = compute_shortest_path(cost_map, points[i-1], points[i])
54
- path.extend(segment)
55
-
56
- # Draw the path on the image
57
- for i in range(1, len(path)):
58
- cv2.line(image, tuple(path[i-1]), tuple(path[i]), (0, 255, 0), 2)
59
- return image
 
60
 
61
- iface = gr.Interface(
62
- fn=live_wire_segmentation,
63
- inputs=[gr.inputs.Image(shape=(512, 512), label="Original Image"),
64
- gr.inputs.Sketchpad()],
65
- outputs=gr.outputs.Image()
66
  )
67
 
68
- iface.launch()
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()