Schrodingers commited on
Commit
d7ff44b
1 Parent(s): a6a05ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -1
app.py CHANGED
@@ -4,8 +4,65 @@ 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():
 
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
+
12
+ import cv2
13
+ import numpy as np
14
+
15
+ def compute_cost(image):
16
+ gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
17
+ gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
18
+ gradient_magnitude = cv2.magnitude(gradient_x, gradient_y)
19
+ return gradient_magnitude
20
+
21
+ def dijkstra(cost, start, end):
22
+ # Dimensions of the image
23
+ h, w = cost.shape
24
+ visited = np.zeros((h, w), dtype=np.bool_)
25
+ distance = np.full((h, w), np.inf)
26
+ parent = np.zeros((h, w, 2), dtype=np.int16) # to store the path
27
+
28
+ distance[start[1], start[0]] = 0
29
+ for _ in range(h * w):
30
+ min_distance = np.inf
31
+ for y in range(h):
32
+ for x in range(w):
33
+ if not visited[y, x] and distance[y, x] < min_distance:
34
+ u = (x, y)
35
+ min_distance = distance[y, x]
36
+
37
+ visited[u[1], u[0]] = True
38
+
39
+ # Check neighbors
40
+ for i in [-1, 0, 1]:
41
+ for j in [-1, 0, 1]:
42
+ if 0 <= u[1] + i < h and 0 <= u[0] + j < w:
43
+ v = (u[0] + j, u[1] + i)
44
+ alt = distance[u[1], u[0]] + cost[v[1], v[0]]
45
+ if alt < distance[v[1], v[0]]:
46
+ distance[v[1], v[0]] = alt
47
+ parent[v[1], v[0]] = u
48
+
49
+ # Reconstruct path from end to start by following parents
50
+ path = []
51
+ while end != start:
52
+ path.append(end)
53
+ end = tuple(parent[end[1], end[0]])
54
+ path.append(start)
55
+
56
+ return path
57
+
58
+ def live_wire_segmentation(image, start, end):
59
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
60
+ cost = compute_cost(gray)
61
+ path = dijkstra(cost, start, end)
62
+
63
+ for point in path:
64
+ cv2.circle(image, point, 1, (0, 255, 0), -1) # Draw path on the image
65
+
66
  return image
67
 
68
  def main_app():