VDNT11 commited on
Commit
291ef85
·
verified ·
1 Parent(s): 0b6b784

Update angle_detection_app.py

Browse files
Files changed (1) hide show
  1. angle_detection_app.py +18 -100
angle_detection_app.py CHANGED
@@ -1,10 +1,8 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- import matplotlib.pyplot as plt
5
- from typing import Tuple, List
6
- import tempfile
7
  import os
 
8
 
9
  def detect_bends_and_angles(
10
  image,
@@ -21,32 +19,19 @@ def detect_bends_and_angles(
21
  """
22
  Detect bends and calculate angles relative to horizontal with configurable parameters.
23
  """
24
- # Convert image to grayscale
25
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
26
-
27
- # Step 2: Apply Gaussian blur
28
- # Ensure blur_kernel_size is odd and greater than 0
29
  if blur_kernel_size is None:
30
- blur_kernel_size = 3 # Default to 3 if not provided
31
- blur_kernel_size = max(3, blur_kernel_size | 1) # Ensure it's an odd number
32
  blurred = cv2.GaussianBlur(gray, (blur_kernel_size, blur_kernel_size), 0)
33
-
34
- # Step 3: Perform edge detection
35
  edges = cv2.Canny(blurred, canny_threshold1, canny_threshold2)
36
-
37
- # Step 4: Dilate edges
38
  kernel = np.ones((dilation_kernel_size, dilation_kernel_size), np.uint8)
39
  dilated = cv2.dilate(edges, kernel, iterations=1)
40
 
41
- # Step 5: Detect parallel lines and identify bends
42
  height, width = dilated.shape
43
  lines = cv2.HoughLinesP(
44
- dilated,
45
- rho=1,
46
- theta=np.pi/180,
47
- threshold=hough_threshold,
48
- minLineLength=min_line_length,
49
- maxLineGap=max_line_gap
50
  )
51
 
52
  bend_points = []
@@ -68,7 +53,6 @@ def detect_bends_and_angles(
68
  if abs(x1 - x1_next) < bend_threshold and abs(y1 - y1_next) < bend_threshold:
69
  bend_points.append((x1, y1))
70
 
71
- # Step 6: Calculate angles between bends
72
  angles = []
73
  for i in range(len(bend_points) - 1):
74
  x1, y1 = bend_points[i]
@@ -94,7 +78,6 @@ def process_image(
94
  """
95
  Process the image and return the visualization and angle measurements.
96
  """
97
- # Convert Gradio image to numpy array if needed
98
  if isinstance(image, dict):
99
  image = image['image']
100
  if isinstance(image, str):
@@ -102,18 +85,10 @@ def process_image(
102
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
103
 
104
  bend_points, angles = detect_bends_and_angles(
105
- image,
106
- blur_kernel_size,
107
- canny_threshold1,
108
- canny_threshold2,
109
- dilation_kernel_size,
110
- hough_threshold,
111
- min_line_length,
112
- max_line_gap,
113
- bend_threshold
114
  )
115
 
116
- # Create visualization
117
  result_img = image.copy()
118
  for i, (x, y) in enumerate(bend_points):
119
  cv2.circle(result_img, (x, y), 5, (0, 0, 255), -1)
@@ -127,7 +102,6 @@ def process_image(
127
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1
128
  )
129
 
130
- # Create angle measurements text
131
  measurements = "Angle Measurements:\n"
132
  for i, ((x, y), angle) in enumerate(angles):
133
  measurements += f"Bend {chr(65 + i)} at ({x}, {y}): {angle:.1f}°\n"
@@ -144,38 +118,14 @@ def create_gradio_interface():
144
  input_image = gr.Image(label="Input Image", type="numpy")
145
 
146
  with gr.Accordion("Algorithm Parameters", open=False):
147
- blur_kernel_size = gr.Slider(
148
- minimum=3, maximum=15, step=2,
149
- value=7, label="Blur Kernel Size"
150
- )
151
- canny_threshold1 = gr.Slider(
152
- minimum=0, maximum=100, step=10,
153
- value=30, label="Canny Threshold 1"
154
- )
155
- canny_threshold2 = gr.Slider(
156
- minimum=100, maximum=300, step=10,
157
- value=150, label="Canny Threshold 2"
158
- )
159
- dilation_kernel_size = gr.Slider(
160
- minimum=1, maximum=5, step=1,
161
- value=2, label="Dilation Kernel Size"
162
- )
163
- hough_threshold = gr.Slider(
164
- minimum=10, maximum=100, step=10,
165
- value=50, label="Hough Threshold"
166
- )
167
- min_line_length = gr.Slider(
168
- minimum=5, maximum=50, step=5,
169
- value=10, label="Minimum Line Length"
170
- )
171
- max_line_gap = gr.Slider(
172
- minimum=10, maximum=100, step=10,
173
- value=60, label="Maximum Line Gap"
174
- )
175
- bend_threshold = gr.Slider(
176
- minimum=5, maximum=30, step=5,
177
- value=15, label="Bend Threshold"
178
- )
179
 
180
  process_btn = gr.Button("Process Image", variant="primary")
181
 
@@ -185,45 +135,13 @@ def create_gradio_interface():
185
 
186
  process_btn.click(
187
  fn=process_image,
188
- inputs=[
189
- input_image,
190
- blur_kernel_size,
191
- canny_threshold1,
192
- canny_threshold2,
193
- dilation_kernel_size,
194
- hough_threshold,
195
- min_line_length,
196
- max_line_gap,
197
- bend_threshold
198
- ],
199
  outputs=[output_image, output_text]
200
  )
201
 
202
-
203
-
204
  return interface
205
 
206
  if __name__ == "__main__":
207
  interface = create_gradio_interface()
208
- # Get port from environment variable or use a different default port
209
- port = int(os.environ.get("GRADIO_SERVER_PORT", 7861))
210
- # Get host from environment variable or use default
211
- host = os.environ.get("GRADIO_SERVER_NAME", "0.0.0.0")
212
- # Launch the interface with error handling
213
- try:
214
- interface.launch(
215
- server_name=host,
216
- server_port=port,
217
- share=True # Set to True to create a public URL
218
- )
219
- except OSError as e:
220
- print(f"Port {port} is in use. Trying next port...")
221
- try:
222
- interface.launch(
223
- server_name=host,
224
- server_port=port + 1,
225
- share=True
226
- )
227
- except Exception as e:
228
- print(f"Error launching the interface: {e}")
229
- print("Please try a different port by setting the GRADIO_SERVER_PORT environment variable")
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
 
 
4
  import os
5
+ from typing import Tuple, List
6
 
7
  def detect_bends_and_angles(
8
  image,
 
19
  """
20
  Detect bends and calculate angles relative to horizontal with configurable parameters.
21
  """
 
22
  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
 
 
23
  if blur_kernel_size is None:
24
+ blur_kernel_size = 3
25
+ blur_kernel_size = max(3, blur_kernel_size | 1) # Ensure it's odd
26
  blurred = cv2.GaussianBlur(gray, (blur_kernel_size, blur_kernel_size), 0)
 
 
27
  edges = cv2.Canny(blurred, canny_threshold1, canny_threshold2)
 
 
28
  kernel = np.ones((dilation_kernel_size, dilation_kernel_size), np.uint8)
29
  dilated = cv2.dilate(edges, kernel, iterations=1)
30
 
 
31
  height, width = dilated.shape
32
  lines = cv2.HoughLinesP(
33
+ dilated, rho=1, theta=np.pi/180, threshold=hough_threshold,
34
+ minLineLength=min_line_length, maxLineGap=max_line_gap
 
 
 
 
35
  )
36
 
37
  bend_points = []
 
53
  if abs(x1 - x1_next) < bend_threshold and abs(y1 - y1_next) < bend_threshold:
54
  bend_points.append((x1, y1))
55
 
 
56
  angles = []
57
  for i in range(len(bend_points) - 1):
58
  x1, y1 = bend_points[i]
 
78
  """
79
  Process the image and return the visualization and angle measurements.
80
  """
 
81
  if isinstance(image, dict):
82
  image = image['image']
83
  if isinstance(image, str):
 
85
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
86
 
87
  bend_points, angles = detect_bends_and_angles(
88
+ image, blur_kernel_size, canny_threshold1, canny_threshold2, dilation_kernel_size,
89
+ hough_threshold, min_line_length, max_line_gap, bend_threshold
 
 
 
 
 
 
 
90
  )
91
 
 
92
  result_img = image.copy()
93
  for i, (x, y) in enumerate(bend_points):
94
  cv2.circle(result_img, (x, y), 5, (0, 0, 255), -1)
 
102
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1
103
  )
104
 
 
105
  measurements = "Angle Measurements:\n"
106
  for i, ((x, y), angle) in enumerate(angles):
107
  measurements += f"Bend {chr(65 + i)} at ({x}, {y}): {angle:.1f}°\n"
 
118
  input_image = gr.Image(label="Input Image", type="numpy")
119
 
120
  with gr.Accordion("Algorithm Parameters", open=False):
121
+ blur_kernel_size = gr.Slider(minimum=3, maximum=15, step=2, value=7, label="Blur Kernel Size")
122
+ canny_threshold1 = gr.Slider(minimum=0, maximum=100, step=10, value=30, label="Canny Threshold 1")
123
+ canny_threshold2 = gr.Slider(minimum=100, maximum=300, step=10, value=150, label="Canny Threshold 2")
124
+ dilation_kernel_size = gr.Slider(minimum=1, maximum=5, step=1, value=2, label="Dilation Kernel Size")
125
+ hough_threshold = gr.Slider(minimum=10, maximum=100, step=10, value=50, label="Hough Threshold")
126
+ min_line_length = gr.Slider(minimum=5, maximum=50, step=5, value=10, label="Minimum Line Length")
127
+ max_line_gap = gr.Slider(minimum=10, maximum=100, step=10, value=60, label="Maximum Line Gap")
128
+ bend_threshold = gr.Slider(minimum=5, maximum=30, step=5, value=15, label="Bend Threshold")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  process_btn = gr.Button("Process Image", variant="primary")
131
 
 
135
 
136
  process_btn.click(
137
  fn=process_image,
138
+ inputs=[input_image, blur_kernel_size, canny_threshold1, canny_threshold2, dilation_kernel_size,
139
+ hough_threshold, min_line_length, max_line_gap, bend_threshold],
 
 
 
 
 
 
 
 
 
140
  outputs=[output_image, output_text]
141
  )
142
 
 
 
143
  return interface
144
 
145
  if __name__ == "__main__":
146
  interface = create_gradio_interface()
147
+ interface.launch(share=False) # Set share=False to avoid issues when deploying