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

added sliders for the first tab

Browse files
Files changed (2) hide show
  1. app.py +25 -45
  2. distortion.py +66 -3
app.py CHANGED
@@ -1,54 +1,10 @@
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
-
33
- if len(coordis) == 4 :
34
- coordinates = np.array(coordis)
35
- print("shape of second array:", coordinates.shape)
36
- print(coordinates)
37
- dwg = ezdxf.new("R2010")
38
- msp = dwg.modelspace()
39
- dwg.layers.new(name="greeny green lines", dxfattribs={"color": 3})
40
-
41
- msp.add_line((coordinates[0][0], -coordinates[0][1]), (coordinates[1][0], -coordinates[1][1]))
42
- msp.add_line((coordinates[1][0], -coordinates[1][1]), (coordinates[2][0], -coordinates[2][1]))
43
- msp.add_line((coordinates[2][0], -coordinates[2][1]), (coordinates[3][0], -coordinates[3][1]))
44
- msp.add_line((coordinates[3][0], -coordinates[3][1]), (coordinates[0][0], -coordinates[0][1]))
45
-
46
- dwg_file = dwg.saveas("output.dxf")
47
- coordis.clear()
48
-
49
- return "output.dxf"
50
-
51
-
52
  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.
53
 
54
  Pellentesque nunc turpis, porta ut accumsan eget, iaculis nec odio. Praesent fringilla a sem sed elementum. Proin orci justo, rutrum et feugiat eleifend, auctor sed odio. Maecenas posuere urna tortor, ut euismod ligula mattis sed. Sed ipsum velit, pretium sed lacinia sed, placerat eget urna. Mauris lobortis mi vitae odio luctus viverra sed eget urna. Pellentesque blandit semper felis sed congue. Aenean congue enim id euismod finibus.
@@ -61,6 +17,30 @@ with gr.Blocks() as demo:
61
  title = """<p><h1 align="center" style="font-size: 36px;">Auto Allign Quadrilateral Workpiece</h1></p>"""
62
  gr.HTML(title)
63
  with gr.Tab("Allign"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Row():
65
  with gr.Column():
66
  image_input = gr.Image(label="Select Image")
 
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.
9
 
10
  Pellentesque nunc turpis, porta ut accumsan eget, iaculis nec odio. Praesent fringilla a sem sed elementum. Proin orci justo, rutrum et feugiat eleifend, auctor sed odio. Maecenas posuere urna tortor, ut euismod ligula mattis sed. Sed ipsum velit, pretium sed lacinia sed, placerat eget urna. Mauris lobortis mi vitae odio luctus viverra sed eget urna. Pellentesque blandit semper felis sed congue. Aenean congue enim id euismod finibus.
 
17
  title = """<p><h1 align="center" style="font-size: 36px;">Auto Allign Quadrilateral Workpiece</h1></p>"""
18
  gr.HTML(title)
19
  with gr.Tab("Allign"):
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")
28
+ third_slider = gr.Slider(0,255, value=0,label="3. Slider")
29
+ forth_slider = gr.Slider(0,255, value=255,label="4. Slider")
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])
38
+ second_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
39
+ third_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
40
+ forth_slider.change(track, inputs=[image_input, first_slider, second_slider, third_slider, forth_slider, fifth_slider, sixth_slider], outputs=[image_output, current_values])
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")
distortion.py CHANGED
@@ -1,8 +1,10 @@
1
  import cv2
2
  import numpy as np
 
 
3
  from pathlib import Path
4
 
5
-
6
 
7
  def save_coefficients(mtx, dist, path):
8
  """Save camera matrix and distortion coefficients to file."""
@@ -10,6 +12,23 @@ def save_coefficients(mtx, dist, path):
10
  cv_file.write('K', mtx)
11
  cv_file.write('D', dist)
12
  cv_file.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
 
15
  def load_coefficients(path):
@@ -20,6 +39,29 @@ def load_coefficients(path):
20
  cv_file.release()
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."""
@@ -64,7 +106,28 @@ def generate_matrix(filename, board_vert, board_horz):
64
  except:
65
  print("Please check the Chessboard Dimensions")
66
 
67
-
68
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
 
 
 
70
 
 
 
1
  import cv2
2
  import numpy as np
3
+ import ezdxf
4
+ import gradio as gr
5
  from pathlib import Path
6
 
7
+ coordis = []
8
 
9
  def save_coefficients(mtx, dist, path):
10
  """Save camera matrix and distortion coefficients to file."""
 
12
  cv_file.write('K', mtx)
13
  cv_file.write('D', dist)
14
  cv_file.release()
15
+
16
+
17
+ def load_coefficients_yaml(path):
18
+ """Load camera matrix and distortion coefficients from file."""
19
+ cv_file = cv2.FileStorage(path.name, cv2.FILE_STORAGE_READ)
20
+ camera_matrix = cv_file.getNode('K').mat()
21
+ dist_matrix = cv_file.getNode('D').mat()
22
+ cv_file.release()
23
+ return [camera_matrix, dist_matrix]
24
+
25
+
26
+ def correct_image(image, yaml):
27
+ image = cv2.imread(image)
28
+ mtx, dist = load_coefficients_yaml(yaml)
29
+ dst = cv2.undistort(image, mtx, dist, None, None)
30
+ return dst
31
+
32
 
33
 
34
  def load_coefficients(path):
 
39
  cv_file.release()
40
  return [camera_matrix, dist_matrix]
41
 
42
+ def get_select_coords(img, evt: gr.SelectData):
43
+
44
+ row, col = evt.index
45
+ coordis.append([row, col])
46
+
47
+ if len(coordis) == 4 :
48
+ coordinates = np.array(coordis)
49
+ print("shape of second array:", coordinates.shape)
50
+ print(coordinates)
51
+ dwg = ezdxf.new("R2010")
52
+ msp = dwg.modelspace()
53
+ dwg.layers.new(name="greeny green lines", dxfattribs={"color": 3})
54
+
55
+ msp.add_line((coordinates[0][0], -coordinates[0][1]), (coordinates[1][0], -coordinates[1][1]))
56
+ msp.add_line((coordinates[1][0], -coordinates[1][1]), (coordinates[2][0], -coordinates[2][1]))
57
+ msp.add_line((coordinates[2][0], -coordinates[2][1]), (coordinates[3][0], -coordinates[3][1]))
58
+ msp.add_line((coordinates[3][0], -coordinates[3][1]), (coordinates[0][0], -coordinates[0][1]))
59
+
60
+ dwg_file = dwg.saveas("output.dxf")
61
+ coordis.clear()
62
+
63
+ return "output.dxf"
64
+
65
 
66
  def generate_matrix(filename, board_vert, board_horz):
67
  """Main function to calibrate camera and undistort image."""
 
106
  except:
107
  print("Please check the Chessboard Dimensions")
108
 
109
+
110
+ def track(img, h1, s1, v1, h2, s2, v2):
111
+ # Load the image
112
+ img = cv2.imread(img)
113
+
114
+
115
+ while(1):
116
+ #hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
117
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
118
+
119
+ # Create the NumPy arrays
120
+ lower_red = np.array([h1, s1, v1])
121
+ upper_red = np.array([h2, s2, v2])
122
+
123
+ # Convert every element to integer using int() function
124
+ lower_red = np.array([int(x) for x in lower_red])
125
+ upper_red = np.array([int(x) for x in upper_red])
126
+
127
+ total = np.array([h1, s1, v1, h2, s2, v2])
128
 
129
+ mask = cv2.inRange(img, lower_red, upper_red)
130
+ res = cv2.bitwise_and(img,img, mask= mask)
131
+ #hsv = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
132
 
133
+ return res, total