Can Günen
commited on
Commit
·
d087928
1
Parent(s):
858a6b3
changed the chessboard shape
Browse files- app.py +9 -12
- distortion.py +14 -16
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,17 +1,20 @@
|
|
1 |
-
import numpy as np
|
2 |
import gradio as gr
|
3 |
from distortion import main
|
4 |
-
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
coords = [[0,0]]
|
10 |
|
11 |
-
import gradio as gr
|
12 |
-
import cv2
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
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.
|
17 |
|
@@ -39,12 +42,6 @@ with gr.Blocks() as demo:
|
|
39 |
with gr.Tab("Pick Corners"):
|
40 |
with gr.Row():
|
41 |
|
42 |
-
def get_select_coords(img, evt: gr.SelectData):
|
43 |
-
row, col = evt.index
|
44 |
-
coords.append([row, col])
|
45 |
-
selected_coords.value = f"Selected Coordinates: ({row}, {col})"
|
46 |
-
return coords[-1]
|
47 |
-
|
48 |
input_img = gr.Image(label="Select Image")
|
49 |
selected_coords = gr.Textbox(label="Selected Coordinates")
|
50 |
|
@@ -59,9 +56,9 @@ with gr.Blocks() as demo:
|
|
59 |
chess_vert_input = gr.Textbox(label="Enter the number of squares in vertical direction")
|
60 |
chess_horz_input = gr.Textbox(label="Enter the number of squares in horizontal direction")
|
61 |
download_dxf = gr.Button("Generate the YAML File")
|
62 |
-
|
63 |
-
|
64 |
|
|
|
65 |
download_dxf.click(main, inputs=[distorted_image, chess_vert_input, chess_horz_input], outputs= yaml_file)
|
66 |
with gr.Column():
|
67 |
gr.Markdown(SHARED_UI_WARNING)
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from distortion import main
|
3 |
+
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
coords = [[0,0]]
|
9 |
|
|
|
|
|
10 |
|
11 |
|
12 |
+
def get_select_coords(img, evt: gr.SelectData):
|
13 |
+
row, col = evt.index
|
14 |
+
coords.append([row, col])
|
15 |
+
selected_coords.value = f"Selected Coordinates: ({row}, {col})"
|
16 |
+
return coords[-1]
|
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 |
|
|
|
42 |
with gr.Tab("Pick Corners"):
|
43 |
with gr.Row():
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
input_img = gr.Image(label="Select Image")
|
46 |
selected_coords = gr.Textbox(label="Selected Coordinates")
|
47 |
|
|
|
56 |
chess_vert_input = gr.Textbox(label="Enter the number of squares in vertical direction")
|
57 |
chess_horz_input = gr.Textbox(label="Enter the number of squares in horizontal direction")
|
58 |
download_dxf = gr.Button("Generate the YAML File")
|
59 |
+
error_box = gr.Textbox(label="Error", visible=False)
|
|
|
60 |
|
61 |
+
yaml_file = gr.File()
|
62 |
download_dxf.click(main, inputs=[distorted_image, chess_vert_input, chess_horz_input], outputs= yaml_file)
|
63 |
with gr.Column():
|
64 |
gr.Markdown(SHARED_UI_WARNING)
|
distortion.py
CHANGED
@@ -3,6 +3,7 @@ import numpy as np
|
|
3 |
from pathlib import Path
|
4 |
|
5 |
|
|
|
6 |
def save_coefficients(mtx, dist, path):
|
7 |
"""Save camera matrix and distortion coefficients to file."""
|
8 |
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_WRITE)
|
@@ -26,7 +27,7 @@ def main(filename, board_vert, board_horz):
|
|
26 |
filename_stem = Path(filename).stem
|
27 |
|
28 |
# Define the checkerboard pattern size and criteria for corner detection
|
29 |
-
CHECKERBOARD = (int(board_vert), int(board_horz))
|
30 |
#CHECKERBOARD = (9, 7)
|
31 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
|
32 |
|
@@ -55,18 +56,15 @@ def main(filename, board_vert, board_horz):
|
|
55 |
|
56 |
# Calibrate camera using object points and image points
|
57 |
h, w = gray.shape[:2]
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
# Save the undistorted image
|
71 |
-
cv2.imwrite(f'undist_{filename_stem}.jpg', dst)
|
72 |
-
return f"{filename_stem}.yml"
|
|
|
3 |
from pathlib import Path
|
4 |
|
5 |
|
6 |
+
|
7 |
def save_coefficients(mtx, dist, path):
|
8 |
"""Save camera matrix and distortion coefficients to file."""
|
9 |
cv_file = cv2.FileStorage(path, cv2.FILE_STORAGE_WRITE)
|
|
|
27 |
filename_stem = Path(filename).stem
|
28 |
|
29 |
# Define the checkerboard pattern size and criteria for corner detection
|
30 |
+
CHECKERBOARD = (int(board_vert)-1, int(board_horz)-1)
|
31 |
#CHECKERBOARD = (9, 7)
|
32 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
|
33 |
|
|
|
56 |
|
57 |
# Calibrate camera using object points and image points
|
58 |
h, w = gray.shape[:2]
|
59 |
+
try:
|
60 |
+
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_points, image_points, gray.shape[::-1], None, None)
|
61 |
+
# Save camera matrix and distortion coefficients to file
|
62 |
+
save_coefficients(mtx, dist, f"{filename_stem}.yml")
|
63 |
+
return f"{filename_stem}.yml"
|
64 |
+
except:
|
65 |
+
print("Please check the Chessboard Dimensions")
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
numpy
|
2 |
-
gradio
|
|
|
|
1 |
numpy
|
2 |
+
gradio
|
3 |
+
opencv-python
|