Created
May 11, 2022 01:09
-
-
Save RH2/683927cc3a6508e5176ab730c067c590 to your computer and use it in GitHub Desktop.
aprilDirectoryDetect.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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_APRILTAG_16h5", | |
help="type of ArUCo tag to detect") | |
args = vars(ap.parse_args()) | |
################ function we will use using in loop below | |
def processimage(image,cameraData,path,outputfoldername): | |
image,CAM_DATA,currentdirectory,"out" | |
(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(path,outputfoldername) | |
txtpath = os.path.join(outpath, str(tail.split(".")[0]+".txt")) | |
csvpath = os.path.join(outpath, str(tail.split(".")[0]+".csv")) | |
data = {"corners","id","rejected"} | |
with open(txtpath, '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() | |
with open(csvpath, '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(tail.split(".")[0]+".png,")) | |
f.write(str(markerID)+"-"+str(cindex) +"," ) | |
f.write(str(c[0]) +","+str(c[1])+"," ) | |
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(outpath, str(tail.split(".")[0]+".png")),image) | |
# cv.imshow("Image", image) | |
# cv.imwrite("detectionOutput.png",image) | |
# cv.waitKey(0) | |
#STARTING POINT... | |
directory = r"C:\Users\Reference\Downloads\iphone_test_592022" | |
scanfolder = [f for f in os.listdir(directory) if not os.path.isfile(f)] | |
for subscan in scanfolder: | |
print(subscan) | |
currentdirectory = os.path.join(directory,subscan) | |
#camera data is in string(subscan)+".csv" | |
cameraData = os.path.join(currentdirectory, str(subscan)+".csv") | |
CAM_DATA = open( cameraData , "r").read() | |
framedirectory = os.path.join(currentdirectory,"frames") | |
framepaths = os.path.join(framedirectory,r"*.png") | |
outputfoldername = "out" | |
outpath = os.path.join(currentdirectory,outputfoldername) | |
try: | |
os.mkdir(outpath) | |
except OSError as error: | |
print(error) | |
for image in glob.iglob(framepaths): | |
#print(image) | |
print(currentdirectory) | |
processimage(image,CAM_DATA,currentdirectory,outputfoldername) | |
csvpaths = os.path.join(outpath,r"*.csv") | |
completeString = "" | |
for spreadsheet in glob.iglob(csvpaths): | |
filecontents = open( spreadsheet , "r").read() | |
completeString += filecontents | |
txtpath = os.path.join(outpath, "all.csv") | |
with open(txtpath, 'w',encoding='utf-8') as f: | |
f.write(completeString) | |
f.close() | |
#PROCESS ORIGINAL IMAGES | |
framedirectory = os.path.join(currentdirectory,"raw") | |
framepaths = os.path.join(framedirectory,r"*.png") | |
outputfoldername = "raw_out" | |
outpath = os.path.join(currentdirectory,outputfoldername) | |
try: | |
os.mkdir(outpath) | |
except OSError as error: | |
print(error) | |
for image in glob.iglob(framepaths): | |
#print(image) | |
print(currentdirectory) | |
processimage(image,CAM_DATA,currentdirectory,outputfoldername) | |
csvpaths = os.path.join(outpath,r"*.csv") | |
completeString = "" | |
for spreadsheet in glob.iglob(csvpaths): | |
filecontents = open( spreadsheet , "r").read() | |
completeString += filecontents | |
txtpath = os.path.join(outpath, "all.csv") | |
with open(txtpath, 'w',encoding='utf-8') as f: | |
f.write(completeString) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment