Skip to content

Instantly share code, notes, and snippets.

@RH2
Created May 5, 2022 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RH2/a645431caa577e021a04f04aab761bc5 to your computer and use it in GitHub Desktop.
Save RH2/a645431caa577e021a04f04aab761bc5 to your computer and use it in GitHub Desktop.
ArucoDetect.py
#pip install opencv-contrib-python --user
import cv2 as cv
import numpy as np
import argparse
import sys
import imutils
import glob
import os
import json
ARUCO_DICT = {
"DICT_4X4_50": cv.aruco.DICT_4X4_50,
"DICT_4X4_100": cv.aruco.DICT_4X4_100,
"DICT_4X4_250": cv.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv.aruco.DICT_5X5_50,
"DICT_5X5_100": cv.aruco.DICT_5X5_100,
"DICT_5X5_250": cv.aruco.DICT_5X5_250,
"DICT_5X5_1000": cv.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv.aruco.DICT_6X6_50,
"DICT_6X6_100": cv.aruco.DICT_6X6_100,
"DICT_6X6_250": cv.aruco.DICT_6X6_250,
"DICT_6X6_1000": cv.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv.aruco.DICT_7X7_50,
"DICT_7X7_100": cv.aruco.DICT_7X7_100,
"DICT_7X7_250": cv.aruco.DICT_7X7_250,
"DICT_7X7_1000": cv.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv.aruco.DICT_ARUCO_ORIGINAL,
"DICT_APRILTAG_16h5": cv.aruco.DICT_APRILTAG_16h5,
"DICT_APRILTAG_25h9": cv.aruco.DICT_APRILTAG_25h9,
"DICT_APRILTAG_36h10": cv.aruco.DICT_APRILTAG_36h10,
"DICT_APRILTAG_36h11": cv.aruco.DICT_APRILTAG_36h11
}
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True,
# help="path to input image containing ArUCo tag")
ap.add_argument("-t", "--type", type=str,
default="DICT_4X4_50",
help="type of ArUCo tag to detect")
args = vars(ap.parse_args())
def processimage(image,completePath,cameraData):
(head,tail)= os.path.split(image)
image = cv.imread(image)
print(image.shape)
print(tail)
if ARUCO_DICT.get(args["type"], None) is None:
print("[INFO] ArUCo tag of '{}' is not supported".format(
args["type"]))
sys.exit(0)
arucoDict = cv.aruco.Dictionary_get(ARUCO_DICT[args["type"]])
arucoParams = cv.aruco.DetectorParameters_create()
(corners, ids, rejected) = cv.aruco.detectMarkers(image, arucoDict,parameters=arucoParams)
# output detection log
outpath = os.path.join(r"A:\Controllerscan\coke\mov1\det", str(tail.split(".")[0]+".txt"))
data = {"corners","id","rejected"}
with open(outpath, 'w',encoding='utf-8') as f:
#see if image has registration...
frameData = cameraData.split("\n")
releventData = -1
for frame in frameData:
try:
frame.split(",").index(tail)
releventData= frame
# print(releventData)
break
except ValueError:
releventData=-1
#print("was not able to find: ",tail)
if corners is not None and ids is not None:
ids = ids.flatten()
for (markerCorner, markerID) in zip(corners, ids):
for cindex,c in enumerate(markerCorner[0]):
f.write(str(markerID)+","+str(cindex) +"," )
f.write(str(c[0]/image.shape[1]) +","+str(c[1]/image.shape[0])+"," )
f.write(str(image.shape[1])+","+str(image.shape[0])+",")
f.write(releventData)
# f.write( "{name},{x},{y},{alt},{heading},{pitch},{roll},{f},{px},{py},{k1},{k2},{k3},{k4},{t1},{t2}".format(name=str() )
f.write("\n")
f.close()
# verify *at least* one ArUco marker was detected
#OUTPUT IMAGE
if len(corners) > 0:
# flatten the ArUco IDs list
ids = ids.flatten()
# loop over the detected ArUCo corners
for (markerCorner, markerID) in zip(corners, ids):
# extract the marker corners (which are always returned in
# top-left, top-right, bottom-right, and bottom-left order)
corners = markerCorner.reshape((4, 2))
(topLeft, topRight, bottomRight, bottomLeft) = corners
# convert each of the (x, y)-coordinate pairs to integers
topRight = (int(topRight[0]), int(topRight[1]))
bottomRight = (int(bottomRight[0]), int(bottomRight[1]))
bottomLeft = (int(bottomLeft[0]), int(bottomLeft[1]))
topLeft = (int(topLeft[0]), int(topLeft[1]))
# draw the bounding box of the ArUCo detection
cv.line(image, topLeft, topRight, (0, 255, 0), 2)
cv.line(image, topRight, bottomRight, (0, 255, 0), 2)
cv.line(image, bottomRight, bottomLeft, (0, 255, 0), 2)
cv.line(image, bottomLeft, topLeft, (0, 255, 0), 2)
# compute and draw the center (x, y)-coordinates of the ArUco
# marker
cX = int((topLeft[0] + bottomRight[0]) / 2.0)
cY = int((topLeft[1] + bottomRight[1]) / 2.0)
cv.circle(image, (cX, cY), 4, (0, 0, 255), -1)
# draw the ArUco marker ID on the image
cv.putText(image, str(markerID),
(topLeft[0], topLeft[1] - 15), cv.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
print("[INFO] ArUco marker ID: {}".format(markerID))
# show the output image
cv.imwrite(os.path.join(r"A:\Controllerscan\coke\mov1\out", str(tail.split(".")[0]+".jpg")),image)
# cv.imshow("Image", image)
# cv.imwrite("detectionOutput.png",image)
# cv.waitKey(0)
cameraData = open( os.path.join(r"A:\Controllerscan\coke\mov1", "internal_external.csv") , "r").read()
print(cameraData)
path = r"A:\Controllerscan\coke\mov1\seq"
wildcard = r"*.png"
completePath = os.path.join(path, wildcard)
print(completePath)
for image in glob.iglob(completePath):
print(image)
processimage(image,completePath,cameraData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment