Schrodingers commited on
Commit
a6a05ca
1 Parent(s): 19e8bfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -69
app.py CHANGED
@@ -1,72 +1,28 @@
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.Drawing(label="Draw Seed Points or Paths")],
68
- outputs=gr.outputs.Image(shape=(200, 200)),
69
- live=True
70
- )
71
-
72
- interface.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ from flask import Flask, render_template
5
+
6
+ # A placeholder function; Actual LiveWire code would be much more involved
7
+ def live_wire_segmentation(image, points):
8
+ # This is a placeholder. Implement actual live wire segmentation here.
9
+ return image
10
+
11
+ def main_app():
12
+ interface = gr.Interface(
13
+ fn=live_wire_segmentation,
14
+ inputs=["image", gr.inputs.Sketchpad()], # You may have to adjust this for latest version of Gradio
15
+ outputs="image",
16
+ live=True
17
+ )
18
+ interface.launch()
19
+
20
+ app = Flask(__name__)
21
+
22
+ @app.route('/')
23
+ def index():
24
+ main_app()
25
+ return "Gradio App Running!"
26
+
27
+ if __name__ == "__main__":
28
+ app.run(debug=True)