Can Günen commited on
Commit
2ae1ac2
·
1 Parent(s): 43c7551

Auto corner detection for the first tab

Browse files
Files changed (2) hide show
  1. app.py +11 -17
  2. distortion.py +61 -1
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
4
- from distortion import generate_matrix, get_select_coords, correct_image, track
5
-
6
 
7
 
8
  SHARED_UI_WARNING = f'''##### Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus dignissim odio, at elementum erat vulputate sit amet. Vestibulum sodales viverra fermentum. In ac hendrerit dolor, vitae mattis odio. Maecenas suscipit consectetur suscipit. Curabitur sodales dui eget neque venenatis tincidunt. In sed libero mi. Nam sollicitudin metus urna, sit amet sagittis ex laoreet sed.
@@ -20,8 +19,11 @@ with gr.Blocks() as demo:
20
 
21
  with gr.Row():
22
  with gr.Column():
23
- image_input = gr.Image(type="filepath")
24
- image_output = gr.Image()
 
 
 
25
  with gr.Column():
26
  first_slider = gr.Slider(0,255, value=0, label="1. Slider")
27
  second_slider = gr.Slider(0,255, value=0,label="2. Slider")
@@ -30,8 +32,10 @@ with gr.Blocks() as demo:
30
  fifth_slider = gr.Slider(0,255, value=255,label="5. Slider")
31
  sixth_slider = gr.Slider(0,255, value=255,label="6. Slider")
32
  current_values = gr.Textbox("Current vallues of sliders")
33
-
34
-
 
 
35
 
36
 
37
  first_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
@@ -41,17 +45,7 @@ with gr.Blocks() as demo:
41
  fifth_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
42
  sixth_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
43
 
44
- with gr.Row():
45
- with gr.Column():
46
- image_input = gr.Image(label="Select Image")
47
- file_input = gr.File(label="Select O-Matrix")
48
- upload_button = gr.Button("Upload")
49
- mask_input = gr.Button("Initiate masking (Temporary)") #will call the opencv screen
50
- with gr.Column():
51
- image_output = gr.Image()
52
- download_dxf = gr.Button("Download the DXF File")
53
-
54
-
55
  with gr.Tab("Pick Corners"):
56
  with gr.Row():
57
  with gr.Column():
 
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
4
+ from distortion import generate_matrix, get_select_coords, correct_image, track, allign_tab
 
5
 
6
 
7
  SHARED_UI_WARNING = f'''##### Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus dignissim odio, at elementum erat vulputate sit amet. Vestibulum sodales viverra fermentum. In ac hendrerit dolor, vitae mattis odio. Maecenas suscipit consectetur suscipit. Curabitur sodales dui eget neque venenatis tincidunt. In sed libero mi. Nam sollicitudin metus urna, sit amet sagittis ex laoreet sed.
 
19
 
20
  with gr.Row():
21
  with gr.Column():
22
+ image_input = gr.Image(label="Unmasked Image", type="filepath")
23
+ image_output = gr.Image(label="Masked Image", type="filepath")
24
+ file_input = gr.File(label="Select O-Matrix", type="file")
25
+ third_image = gr.Image(label="Detected Shape")
26
+
27
  with gr.Column():
28
  first_slider = gr.Slider(0,255, value=0, label="1. Slider")
29
  second_slider = gr.Slider(0,255, value=0,label="2. Slider")
 
32
  fifth_slider = gr.Slider(0,255, value=255,label="5. Slider")
33
  sixth_slider = gr.Slider(0,255, value=255,label="6. Slider")
34
  current_values = gr.Textbox("Current vallues of sliders")
35
+ download_dxf = gr.Button("Generate DXF File")
36
+ last_output = gr.File(label="Generated dxf file", type="file")
37
+
38
+ download_dxf.click(fn=allign_tab, inputs=[image_output, file_input], outputs=[last_output, third_image])
39
 
40
 
41
  first_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
 
45
  fifth_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
46
  sixth_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
47
 
48
+
 
 
 
 
 
 
 
 
 
 
49
  with gr.Tab("Pick Corners"):
50
  with gr.Row():
51
  with gr.Column():
distortion.py CHANGED
@@ -130,4 +130,64 @@ def track(img, h1, s1, v1, h2, s2, v2):
130
  res = cv2.bitwise_and(img,img, mask= mask)
131
  #hsv = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
132
 
133
- return res, total
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  res = cv2.bitwise_and(img,img, mask= mask)
131
  #hsv = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
132
 
133
+ return res, total
134
+
135
+ def allign_tab(file_path, yaml):
136
+
137
+
138
+ coordinates = []
139
+ # Load the masked image
140
+ img = cv2.imread(file_path)
141
+
142
+ mtx, dist = load_coefficients_yaml(yaml)
143
+
144
+ img = cv2.undistort(img, mtx, dist, None, None)
145
+
146
+ height, width, _ = img.shape
147
+
148
+ # Convert the image to grayscale
149
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
150
+
151
+ # Apply a threshold to convert the grayscale image into a binary image
152
+ _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
153
+
154
+ # Find the contours in the binary image
155
+ contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
156
+
157
+ # Select the contour with the largest area (the object we want to extract the corners from)
158
+ contour = max(contours, key=cv2.contourArea)
159
+
160
+ # Approximate the contour with a polygon
161
+ epsilon = 0.01 * cv2.arcLength(contour, True)
162
+ approx = cv2.approxPolyDP(contour, epsilon, True)
163
+ print(approx)
164
+
165
+ # Draw the polygon on the original image
166
+ cv2.polylines(img, [approx], True, (0, 255, 0), thickness=2)
167
+
168
+ # cv2.imshow('image', img)
169
+ # cv2.waitKey(0)
170
+ # cv2.destroyAllWindows()
171
+
172
+ doc = ezdxf.new("R2010", setup=True)
173
+ msp = doc.modelspace()
174
+
175
+ # Print the coordinates of the corners
176
+ for corner in approx:
177
+ x, y = corner[0]
178
+ coordinates.append([x,y])
179
+ #print(f"({x}, {y})")
180
+
181
+
182
+ msp.add_line((coordinates[0][0], -coordinates[0][1]), (coordinates[1][0], -coordinates[1][1]))
183
+ msp.add_line((coordinates[1][0], -coordinates[1][1]), (coordinates[2][0], -coordinates[2][1]))
184
+ msp.add_line((coordinates[2][0], -coordinates[2][1]), (coordinates[3][0], -coordinates[3][1]))
185
+ msp.add_line((coordinates[3][0], -coordinates[3][1]), (coordinates[0][0], -coordinates[0][1]))
186
+ msp.add_line((0,0), (0, -height))
187
+ msp.add_line((0, -height), (width, -height))
188
+ msp.add_line((width, -height), (width, 0))
189
+ msp.add_line((width, 0), (0,0))
190
+
191
+
192
+ dwf_file = doc.saveas("output.dxf")
193
+ return "output.dxf", img