VDNT11 commited on
Commit
6ab6dd8
·
verified ·
1 Parent(s): a042301

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +1 -12
  2. angle_detection_app.py +218 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1 @@
1
- ---
2
- title: Bend Angle Measurement
3
- emoji: 🏆
4
- colorFrom: gray
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.23.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
 
 
 
 
 
 
 
 
 
 
 
angle_detection_app.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,
11
+ blur_kernel_size: int = 7,
12
+ canny_threshold1: int = 30,
13
+ canny_threshold2: int = 150,
14
+ dilation_kernel_size: int = 2,
15
+ hough_threshold: int = 50,
16
+ min_line_length: int = 10,
17
+ max_line_gap: int = 60,
18
+ bend_threshold: int = 15,
19
+ debug: bool = True
20
+ ) -> Tuple[List[Tuple[int, int]], List[Tuple[Tuple[int, int], float]]]:
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
+ blurred = cv2.GaussianBlur(gray, (blur_kernel_size, blur_kernel_size), 0)
29
+
30
+ # Step 3: Perform edge detection
31
+ edges = cv2.Canny(blurred, canny_threshold1, canny_threshold2)
32
+
33
+ # Step 4: Dilate edges
34
+ kernel = np.ones((dilation_kernel_size, dilation_kernel_size), np.uint8)
35
+ dilated = cv2.dilate(edges, kernel, iterations=1)
36
+
37
+ # Step 5: Detect parallel lines and identify bends
38
+ height, width = dilated.shape
39
+ lines = cv2.HoughLinesP(
40
+ dilated,
41
+ rho=1,
42
+ theta=np.pi/180,
43
+ threshold=hough_threshold,
44
+ minLineLength=min_line_length,
45
+ maxLineGap=max_line_gap
46
+ )
47
+
48
+ bend_points = []
49
+ if lines is not None:
50
+ segments = []
51
+ for line in lines:
52
+ x1, y1, x2, y2 = line[0]
53
+ if x1 > x2:
54
+ x1, x2 = x2, x1
55
+ y1, y2 = y2, y1
56
+ segments.append((x1, y1, x2, y2))
57
+
58
+ segments.sort(key=lambda seg: seg[0], reverse=True)
59
+
60
+ for i in range(len(segments) - 1):
61
+ x1, y1, x2, y2 = segments[i]
62
+ x1_next, y1_next, x2_next, y2_next = segments[i + 1]
63
+
64
+ if abs(x1 - x1_next) < bend_threshold and abs(y1 - y1_next) < bend_threshold:
65
+ bend_points.append((x1, y1))
66
+
67
+ # Step 6: Calculate angles between bends
68
+ angles = []
69
+ for i in range(len(bend_points) - 1):
70
+ x1, y1 = bend_points[i]
71
+ x2, y2 = bend_points[i + 1]
72
+ dx, dy = x2 - x1, y2 - y1
73
+ angle = np.arctan2(dy, dx) * 180 / np.pi
74
+ angle = angle if angle >= 0 else angle + 180
75
+ angles.append((bend_points[i], angle))
76
+
77
+ return bend_points, angles
78
+
79
+ def process_image(
80
+ image,
81
+ blur_kernel_size: int,
82
+ canny_threshold1: int,
83
+ canny_threshold2: int,
84
+ dilation_kernel_size: int,
85
+ hough_threshold: int,
86
+ min_line_length: int,
87
+ max_line_gap: int,
88
+ bend_threshold: int
89
+ ) -> Tuple[np.ndarray, str]:
90
+ """
91
+ Process the image and return the visualization and angle measurements.
92
+ """
93
+ bend_points, angles = detect_bends_and_angles(
94
+ image,
95
+ blur_kernel_size,
96
+ canny_threshold1,
97
+ canny_threshold2,
98
+ dilation_kernel_size,
99
+ hough_threshold,
100
+ min_line_length,
101
+ max_line_gap,
102
+ bend_threshold
103
+ )
104
+
105
+ # Create visualization
106
+ result_img = image.copy()
107
+ for i, (x, y) in enumerate(bend_points):
108
+ cv2.circle(result_img, (x, y), 5, (0, 0, 255), -1)
109
+ cv2.putText(
110
+ result_img, f"Bend {chr(65 + i)}", (x, y - 10),
111
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1
112
+ )
113
+ for (x, y), angle in angles:
114
+ cv2.putText(
115
+ result_img, f"{angle:.1f}°", (x, y + 20),
116
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1
117
+ )
118
+
119
+ # Create angle measurements text
120
+ measurements = "Angle Measurements:\n"
121
+ for i, ((x, y), angle) in enumerate(angles):
122
+ measurements += f"Bend {chr(65 + i)} at ({x}, {y}): {angle:.1f}°\n"
123
+
124
+ return result_img, measurements
125
+
126
+ # Create Gradio interface
127
+ def create_gradio_interface():
128
+ with gr.Blocks(title="Angle Detection App", theme=gr.themes.Soft()) as interface:
129
+ gr.Markdown("# Angle Detection App")
130
+ gr.Markdown("Upload an image to detect bends and measure angles.")
131
+
132
+ with gr.Row():
133
+ with gr.Column():
134
+ input_image = gr.Image(label="Input Image")
135
+
136
+ with gr.Accordion("Algorithm Parameters", open=False):
137
+ blur_kernel_size = gr.Slider(
138
+ minimum=3, maximum=15, step=2,
139
+ value=7, label="Blur Kernel Size"
140
+ )
141
+ canny_threshold1 = gr.Slider(
142
+ minimum=0, maximum=100, step=10,
143
+ value=30, label="Canny Threshold 1"
144
+ )
145
+ canny_threshold2 = gr.Slider(
146
+ minimum=100, maximum=300, step=10,
147
+ value=150, label="Canny Threshold 2"
148
+ )
149
+ dilation_kernel_size = gr.Slider(
150
+ minimum=1, maximum=5, step=1,
151
+ value=2, label="Dilation Kernel Size"
152
+ )
153
+ hough_threshold = gr.Slider(
154
+ minimum=10, maximum=100, step=10,
155
+ value=50, label="Hough Threshold"
156
+ )
157
+ min_line_length = gr.Slider(
158
+ minimum=5, maximum=50, step=5,
159
+ value=10, label="Minimum Line Length"
160
+ )
161
+ max_line_gap = gr.Slider(
162
+ minimum=10, maximum=100, step=10,
163
+ value=60, label="Maximum Line Gap"
164
+ )
165
+ bend_threshold = gr.Slider(
166
+ minimum=5, maximum=30, step=5,
167
+ value=15, label="Bend Threshold"
168
+ )
169
+
170
+ process_btn = gr.Button("Process Image", variant="primary")
171
+
172
+ with gr.Column():
173
+ output_image = gr.Image(label="Result")
174
+ output_text = gr.Textbox(label="Measurements", lines=10)
175
+
176
+ process_btn.click(
177
+ fn=process_image,
178
+ inputs=[
179
+ input_image,
180
+ blur_kernel_size,
181
+ canny_threshold1,
182
+ canny_threshold2,
183
+ dilation_kernel_size,
184
+ hough_threshold,
185
+ min_line_length,
186
+ max_line_gap,
187
+ bend_threshold
188
+ ],
189
+ outputs=[output_image, output_text]
190
+ )
191
+
192
+ # Add example images
193
+ gr.Examples(
194
+ examples=[
195
+ ["Initial_images/22432269-0abf-4af8-b4b3-207bb48867cf.jpeg"],
196
+ ["Initial_images/processed_JPG/1b_crop.png"],
197
+ ["Initial_images/processed_JPG/7feb_00.png"],
198
+ ],
199
+ inputs=input_image,
200
+ outputs=[output_image, output_text],
201
+ fn=process_image,
202
+ cache_examples=True,
203
+ )
204
+
205
+ return interface
206
+
207
+ if __name__ == "__main__":
208
+ interface = create_gradio_interface()
209
+ # Get port from environment variable or use default
210
+ port = int(os.environ.get("PORT", 7860))
211
+ # Get host from environment variable or use default
212
+ host = os.environ.get("HOST", "0.0.0.0")
213
+ # Launch the interface
214
+ interface.launch(
215
+ server_name=host,
216
+ server_port=port,
217
+ share=False # Set to True if you want to create a public URL
218
+ )
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ opencv-python>=4.8.0
3
+ numpy>=1.24.0
4
+ matplotlib>=3.7.0