rmsalinas commited on
Commit
eb50885
·
verified ·
1 Parent(s): fe632c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -1,38 +1,62 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
 
5
- def detect_aruco_markers(image):
 
6
  # Convert to grayscale
7
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
8
-
9
  # Load predefined dictionary
10
- aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_250)
11
-
12
  # Initialize detector parameters
13
  parameters = cv2.aruco.DetectorParameters()
 
 
 
 
14
  detector = cv2.aruco.ArucoDetector(aruco_dict, parameters)
 
15
  # Detect markers
16
  corners, ids, rejected = detector.detectMarkers(gray)
17
- #print ids
18
- print(ids)
19
 
20
-
21
- # Draw detected markers
22
  if ids is not None:
23
  annotated_image = cv2.aruco.drawDetectedMarkers(image.copy(), corners, ids)
 
 
 
 
 
 
24
  else:
25
  annotated_image = image.copy()
26
-
27
- return annotated_image
 
 
 
 
28
 
29
  # Gradio interface
30
  iface = gr.Interface(
31
  fn=detect_aruco_markers,
32
- inputs=gr.Image(type="numpy"),
33
- outputs=gr.Image(type="numpy"),
34
- title="ArUco Marker Detector",
35
- description="Upload an image and detect ArUco markers using OpenCV."
 
 
 
 
 
 
 
 
 
36
  )
37
 
38
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ import json
5
 
6
+ def detect_aruco_markers(image, dictionary_preset,
7
+ cornerRefinementMethod, detectInvertedMarker, adaptiveThreshConstant):
8
  # Convert to grayscale
9
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
10
+
11
  # Load predefined dictionary
12
+ aruco_dict = cv2.aruco.getPredefinedDictionary(getattr(cv2.aruco, dictionary_preset))
13
+
14
  # Initialize detector parameters
15
  parameters = cv2.aruco.DetectorParameters()
16
+ parameters.cornerRefinementMethod = getattr(cv2.aruco, cornerRefinementMethod)
17
+ parameters.detectInvertedMarker = detectInvertedMarker
18
+ parameters.adaptiveThreshConstant = adaptiveThreshConstant
19
+
20
  detector = cv2.aruco.ArucoDetector(aruco_dict, parameters)
21
+
22
  # Detect markers
23
  corners, ids, rejected = detector.detectMarkers(gray)
24
+ print("Detected IDs:", ids)
 
25
 
26
+ detections = []
 
27
  if ids is not None:
28
  annotated_image = cv2.aruco.drawDetectedMarkers(image.copy(), corners, ids)
29
+ for i in range(len(ids)):
30
+ marker_info = {
31
+ "id": int(ids[i][0]),
32
+ "corners": [np.array(corner).tolist() for corner in corners[i]]
33
+ }
34
+ detections.append(marker_info)
35
  else:
36
  annotated_image = image.copy()
37
+
38
+ return annotated_image, json.dumps(detections, indent=4)
39
+
40
+ # Available ArUco dictionary presets
41
+ dictionary_choices = [name for name in dir(cv2.aruco) if name.startswith("DICT_")]
42
+ corner_refinement_choices = ["CORNER_REFINE_NONE", "CORNER_REFINE_SUBPIX", "CORNER_REFINE_CONTOUR", "CORNER_REFINE_APRILTAG"]
43
 
44
  # Gradio interface
45
  iface = gr.Interface(
46
  fn=detect_aruco_markers,
47
+ inputs=[
48
+ gr.Image(type="numpy"),
49
+ gr.Dropdown(choices=dictionary_choices, label="Dictionary Preset", value="DICT_6X6_250"),
50
+ gr.Dropdown(choices=corner_refinement_choices, label="Corner Refinement Method", value="CORNER_REFINE_NONE"),
51
+ gr.Checkbox(label="Detect Inverted Marker", value=False),
52
+ gr.Slider(minimum=3, maximum=201, step=2, label="Adaptive Thresh", value=7),
53
+ ],
54
+ outputs=[
55
+ gr.Image(type="numpy", label="Detected Markers"),
56
+ gr.Code(label="Detection Data (JSON)", language="json")
57
+ ],
58
+ title="ArUco Marker Detector with Parameter Control",
59
+ description="Upload an image and adjust ArUco detection parameters. The detected markers are drawn on the image, and the detection data (IDs and corner coordinates) is provided in JSON format.",
60
  )
61
 
62
  if __name__ == "__main__":