Can Günen
commited on
Commit
·
5794114
1
Parent(s):
18dec51
Finished bugs related to second tab
Browse files- app.py +56 -12
- distortion.py +1 -1
app.py
CHANGED
@@ -1,20 +1,54 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
|
|
|
|
|
12 |
def get_select_coords(img, evt: gr.SelectData):
|
|
|
13 |
row, col = evt.index
|
14 |
-
|
15 |
selected_coords.value = f"Selected Coordinates: ({row}, {col})"
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
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 |
|
@@ -41,12 +75,22 @@ with gr.Blocks() as demo:
|
|
41 |
|
42 |
with gr.Tab("Pick Corners"):
|
43 |
with gr.Row():
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
with gr.Tab("O-Matrix"):
|
51 |
with gr.Row():
|
52 |
with gr.Column():
|
@@ -59,7 +103,7 @@ with gr.Blocks() as demo:
|
|
59 |
error_box = gr.Textbox(label="Error", visible=False)
|
60 |
|
61 |
yaml_file = gr.File()
|
62 |
-
download_dxf.click(
|
63 |
with gr.Column():
|
64 |
gr.Markdown(SHARED_UI_WARNING)
|
65 |
|
|
|
1 |
+
import cv2
|
2 |
+
import ezdxf
|
3 |
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
from distortion import generate_matrix
|
6 |
|
7 |
|
8 |
|
9 |
+
def load_coefficients(path):
|
10 |
+
"""Load camera matrix and distortion coefficients from file."""
|
11 |
+
cv_file = cv2.FileStorage(path.name, cv2.FILE_STORAGE_READ)
|
12 |
+
camera_matrix = cv_file.getNode('K').mat()
|
13 |
+
dist_matrix = cv_file.getNode('D').mat()
|
14 |
+
cv_file.release()
|
15 |
+
return [camera_matrix, dist_matrix]
|
16 |
|
17 |
|
18 |
+
def correct_image(image, yaml):
|
19 |
+
image = cv2.imread(image)
|
20 |
+
mtx, dist = load_coefficients(yaml)
|
21 |
+
dst = cv2.undistort(image, mtx, dist, None, None)
|
22 |
+
return dst
|
23 |
|
24 |
|
25 |
|
26 |
+
coordis = []
|
27 |
+
|
28 |
def get_select_coords(img, evt: gr.SelectData):
|
29 |
+
|
30 |
row, col = evt.index
|
31 |
+
coordis.append([row, col])
|
32 |
selected_coords.value = f"Selected Coordinates: ({row}, {col})"
|
33 |
+
|
34 |
+
if len(coordis) == 4 :
|
35 |
+
coordinates = np.array(coordis)
|
36 |
+
print("shape of second array:", coordinates.shape)
|
37 |
+
print(coordinates)
|
38 |
+
dwg = ezdxf.new("R2010")
|
39 |
+
msp = dwg.modelspace()
|
40 |
+
dwg.layers.new(name="greeny green lines", dxfattribs={"color": 3})
|
41 |
+
|
42 |
+
msp.add_line((coordinates[0][0], -coordinates[0][1]), (coordinates[1][0], -coordinates[1][1]))
|
43 |
+
msp.add_line((coordinates[1][0], -coordinates[1][1]), (coordinates[2][0], -coordinates[2][1]))
|
44 |
+
msp.add_line((coordinates[2][0], -coordinates[2][1]), (coordinates[3][0], -coordinates[3][1]))
|
45 |
+
msp.add_line((coordinates[3][0], -coordinates[3][1]), (coordinates[0][0], -coordinates[0][1]))
|
46 |
+
|
47 |
+
dwg.saveas("output.dxf")
|
48 |
+
coordis.clear()
|
49 |
+
|
50 |
+
return "DXF File has been saved successfully"
|
51 |
+
|
52 |
|
53 |
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.
|
54 |
|
|
|
75 |
|
76 |
with gr.Tab("Pick Corners"):
|
77 |
with gr.Row():
|
78 |
+
with gr.Column():
|
79 |
+
title = """<p><h1 align="center" style="font-size: 24px;">First, let's correct the distorted Image</h1></p>"""
|
80 |
+
gr.HTML(title)
|
81 |
+
distorted_input = gr.Image(label="Select distorted Image", type="filepath")
|
82 |
+
yml_input = gr.File(label="Select O-Matrix (.yml file)", type="file")
|
83 |
+
correct_button = gr.Button("Correct the Image")
|
84 |
+
|
85 |
+
with gr.Column():
|
86 |
+
title = """<p><h1 align="center" style="font-size: 24px;">Then, let's mark the corners of the Workpiece</h1></p>"""
|
87 |
+
gr.HTML(title)
|
88 |
+
corrected_img = gr.Image(label="Corrected Image")
|
89 |
+
selected_coords = gr.Textbox()
|
90 |
+
|
91 |
+
corrected_img.select(get_select_coords, [corrected_img], selected_coords)
|
92 |
+
correct_button.click(fn=correct_image, inputs=[distorted_input, yml_input], outputs=corrected_img)
|
93 |
+
|
94 |
with gr.Tab("O-Matrix"):
|
95 |
with gr.Row():
|
96 |
with gr.Column():
|
|
|
103 |
error_box = gr.Textbox(label="Error", visible=False)
|
104 |
|
105 |
yaml_file = gr.File()
|
106 |
+
download_dxf.click(generate_matrix, inputs=[distorted_image, chess_vert_input, chess_horz_input], outputs= yaml_file)
|
107 |
with gr.Column():
|
108 |
gr.Markdown(SHARED_UI_WARNING)
|
109 |
|
distortion.py
CHANGED
@@ -21,7 +21,7 @@ def load_coefficients(path):
|
|
21 |
return [camera_matrix, dist_matrix]
|
22 |
|
23 |
|
24 |
-
def
|
25 |
"""Main function to calibrate camera and undistort image."""
|
26 |
|
27 |
filename_stem = Path(filename).stem
|
|
|
21 |
return [camera_matrix, dist_matrix]
|
22 |
|
23 |
|
24 |
+
def generate_matrix(filename, board_vert, board_horz):
|
25 |
"""Main function to calibrate camera and undistort image."""
|
26 |
|
27 |
filename_stem = Path(filename).stem
|