Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|
| 63 |
+
iface.launch()
|