ArucoDetector / app.py
vincentb25's picture
working locally
80f1239
raw
history blame
2.07 kB
import cv2
#from aruco_detector import ArucoDetector
import gradio as gr
import os
dict_list = ['DICT_4X4_50', 'DICT_4X4_100', 'DICT_4X4_250', 'DICT_4X4_1000', 'DICT_5X5_50', 'DICT_5X5_100', 'DICT_5X5_250', 'DICT_5X5_1000', 'DICT_6X6_50', 'DICT_6X6_100', 'DICT_6X6_250', 'DICT_6X6_1000', 'DICT_7X7_50', 'DICT_7X7_100', 'DICT_7X7_250', 'DICT_7X7_1000', 'DICT_ARUCO_ORIGINAL', 'DICT_APRILTAG_16h5', 'DICT_APRILTAG_25h9', 'DICT_APRILTAG_36h10', 'DICT_APRILTAG_36h11']
def inference(image_path, dict_name):
if not dict_name:
raise gr.Error("No model selected. Please select a model.")
if not image_path:
raise gr.Error("No image provided. Please upload an image.")
dict_index = dict_list.index(dict_name)
aruco_dict = cv2.aruco.getPredefinedDictionary(dict_index)
aruco_params = cv2.aruco.DetectorParameters()
detector = cv2.aruco.ArucoDetector(aruco_dict, aruco_params)
image = cv2.imread(image_path)
corners, ids, rejectedImgPoints = detector.detectMarkers(image)
image = cv2.aruco.drawDetectedMarkers(image, corners, ids, borderColor=(0, 255, 0))
for corner in corners:
cv2.polylines(image, [corner.astype(int)], isClosed=True, color=(0, 255, 0), thickness=3)
cv2.imwrite("output.jpg", image)
output_image = cv2.cvtColor(cv2.imread("output.jpg"), cv2.COLOR_BGR2RGB)
return output_image
def get_aruco_dict():
#PREDEFINED_DICTIONARY_NAME
return dict_list
aruco_dict = get_aruco_dict()
image_paths= [['examples/cans.png', 'DICT_4X4_50', 0.5],
['examples/image4k.png', 'DICT_4X4_50', 0.5],
]
demo = gr.Interface(
fn=inference,
inputs=[
gr.Image(type="filepath", label="Upload Image"),
gr.Dropdown(choices=aruco_dict, label="Select aruco library"),
],
outputs=gr.Image(type="numpy", label="Output Image"),
title="Aruco tag detection",
description="Select the aruco library, upload an image, and detect the aruco tags.",
examples=image_paths,
# flagging_mode="auto"
)
demo.launch()