BBongiovanni commited on
Commit
b0c9f2b
1 Parent(s): 12aac48

added coordinates to app

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -22,7 +22,23 @@ def get_warped_image():
22
 
23
  if len(roi_coords) == 4:
24
  pts = np.array(roi_coords, np.int32)
25
- homography = direct_linear_transformation(roi_coords, compute_target_coords(pts))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  mask = np.zeros((input_image.shape[0], input_image.shape[1]))
28
  cv2.fillConvexPoly(mask, pts, 1)
@@ -35,10 +51,11 @@ def get_warped_image():
35
  contours, hierachy = cv2.findContours(cv2.cvtColor(warped, cv2.COLOR_RGB2GRAY), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
36
  x, y, w, h = cv2.boundingRect(contours[0])
37
  contour_crop = warped[y:y + h, x:x + w]
38
- return contour_crop, np.around(homography, 2), *contour_crop.shape
 
39
 
40
  else:
41
- return None, None, None, None
42
 
43
 
44
  def click_callback(img_path, evt: gr.SelectData):
@@ -145,9 +162,10 @@ with gr.Blocks() as demo:
145
  image_output = gr.Image(label="Cropped Warp", type="filepath", tool="editor", interactive=True)
146
  slider_image_width = gr.Slider(label="Width", minimum=10, maximum=900, step=1)
147
  slider_image_height = gr.Slider(label="Height", minimum=10, maximum=900, step=1)
148
- json_t_matrix = gr.Numpy(label="Transformation Matrix", row_count=3, col_count=3, headers=['', '', ''], interactive=False)
 
149
 
150
- image_input.select(click_callback, image_input, [image_input, image_output, json_t_matrix, slider_image_width, slider_image_height])
151
  image_input.clear(clear_variables)
152
  button_clear.click(clear_variables, None, image_input)
153
  slider_image_width.release(resize_image, [image_output, slider_image_width, gr.State(None)], image_output)
 
22
 
23
  if len(roi_coords) == 4:
24
  pts = np.array(roi_coords, np.int32)
25
+ target_coords = compute_target_coords(pts)
26
+ homography = direct_linear_transformation(roi_coords, target_coords)
27
+
28
+ coordinates = {
29
+ "input": {
30
+ "top-left": "({}, {})".format(roi_coords[0][0], roi_coords[0][1]),
31
+ "top-right": "({}, {})".format(roi_coords[1][0], roi_coords[1][1]),
32
+ "bottom-right": "({}, {})".format(roi_coords[2][0], roi_coords[2][1]),
33
+ "bottom-left": "({}, {})".format(roi_coords[3][0], roi_coords[3][1])
34
+ },
35
+ "projected": {
36
+ "top-left": "({}, {})".format(int(target_coords[0].tolist()[0]), int(target_coords[0].tolist()[1])),
37
+ "top-right": "({}, {})".format(int(target_coords[1].tolist()[0]), int(target_coords[1].tolist()[1])),
38
+ "bottom-right": "({}, {})".format(int(target_coords[2].tolist()[0]), int(target_coords[2].tolist()[1])),
39
+ "bottom-left": "({}, {})".format(int(target_coords[3].tolist()[0]), int(target_coords[3].tolist()[1]))
40
+ }
41
+ }
42
 
43
  mask = np.zeros((input_image.shape[0], input_image.shape[1]))
44
  cv2.fillConvexPoly(mask, pts, 1)
 
51
  contours, hierachy = cv2.findContours(cv2.cvtColor(warped, cv2.COLOR_RGB2GRAY), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
52
  x, y, w, h = cv2.boundingRect(contours[0])
53
  contour_crop = warped[y:y + h, x:x + w]
54
+
55
+ return coordinates, contour_crop, np.around(homography, 2), *contour_crop.shape
56
 
57
  else:
58
+ return None, None, None, None, None
59
 
60
 
61
  def click_callback(img_path, evt: gr.SelectData):
 
162
  image_output = gr.Image(label="Cropped Warp", type="filepath", tool="editor", interactive=True)
163
  slider_image_width = gr.Slider(label="Width", minimum=10, maximum=900, step=1)
164
  slider_image_height = gr.Slider(label="Height", minimum=10, maximum=900, step=1)
165
+ numpy_t_matrix = gr.Numpy(label="Transformation Matrix", row_count=3, col_count=3, headers=['', '', ''], interactive=False)
166
+ json_coordinates = gr.JSON(label="Coordinates")
167
 
168
+ image_input.select(click_callback, image_input, [image_input, json_coordinates, image_output, numpy_t_matrix, slider_image_width, slider_image_height])
169
  image_input.clear(clear_variables)
170
  button_clear.click(clear_variables, None, image_input)
171
  slider_image_width.release(resize_image, [image_output, slider_image_width, gr.State(None)], image_output)