Dateien nach "aruco" hochladen

This commit is contained in:
2024-03-03 16:41:32 +01:00
parent d4c81a2885
commit fdf80652c9
2 changed files with 252 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
# license removed for brevity
import rospy
from geometry_msgs.msg import Twist
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2 as cv
import sys
# define names of each possible ArUco tag OpenCV supports
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
}
def talker():
#construct the argument parser and parse the argument
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--type", type=str, default="DICT_7X7_50", help="type of ArCUo tag to detect")
args = vars(ap.parse_args())
#verify that the supplied ArUCo tag exists and is supported by OpenCV
if ARUCO_DICT.get(args["type"], None) is None:
print("[INFO] ArUCo tag of '{}' is not supported".format(args["type"]))
sys.exit(0)
#load the ArUCo dictionary and grab hte ArUCo parameters
print("[INFO] detecting '{}' tags...".format(args["type"]))
arucoDict = cv.aruco.getPredefinedDictionary(ARUCO_DICT[args["type"]])
arucoParams = cv.aruco.DetectorParameters()
detector = cv.aruco.ArucoDetector(arucoDict, arucoParams)
#initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)
rospy.init_node('turtle_control', anonymous=True)
rate = rospy.Rate(10) #
t = Twist()
while not rospy.is_shutdown():
#grab the frame from the threaded video and resize it
#to have a maximum width of 1000 pixels
frame = vs.read()
frame = imutils.resize(frame, width=1000)
#detect ArUCo markers in the input frame
(corners, ids, rejected) = detector.detectMarkers(frame)
print(ids)
#verify *at least* one ArUCo marker was detected
if len(corners) > 0:
#flatten the ArUCo IDs list
ids = ids.flatten()
if [1] in ids:
t.angular.z = 0.1
t.linear.x = 0.5
pub.publish(t)
rate.sleep()
#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)-coordinates 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(frame, topLeft, topRight, (0,255,0), 2)
cv.line(frame, topRight, bottomRight, (0,255,0), 2)
cv.line(frame, bottomRight, bottomLeft, (0,255,0), 2)
cv.line(frame, 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(frame, (cX,cY), 4, (0,0,255), -1)
#draw the ArUCo marker ID on the frame
cv.putText(frame, str(markerID), (topLeft[0], topLeft[1]-15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
#show the output frame
cv.imshow("Frame", frame)
key = cv.waitKey(1) & 0xFF
#if the 'q' key was pressed, break from the loop
if key == ord("q"):
break
#do a bit of cleanup
cv.destroyAllWindows()
vs.stop()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
# license removed for brevity
import rospy
from geometry_msgs.msg import Twist
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2 as cv
import sys
# define names of each possible ArUco tag OpenCV supports
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
}
def talker():
#construct the argument parser and parse the argument
ap = argparse.ArgumentParser()
ap.add_argument("-t", "--type", type=str, default="DICT_7X7_50", help="type of ArCUo tag to detect")
args = vars(ap.parse_args())
#verify that the supplied ArUCo tag exists and is supported by OpenCV
if ARUCO_DICT.get(args["type"], None) is None:
print("[INFO] ArUCo tag of '{}' is not supported".format(args["type"]))
sys.exit(0)
#load the ArUCo dictionary and grab hte ArUCo parameters
print("[INFO] detecting '{}' tags...".format(args["type"]))
arucoDict = cv.aruco.getPredefinedDictionary(ARUCO_DICT[args["type"]])
arucoParams = cv.aruco.DetectorParameters()
detector = cv.aruco.ArucoDetector(arucoDict, arucoParams)
#initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=10)
rospy.init_node('turtle_control', anonymous=True)
rate = rospy.Rate(10) #
t = Twist()
while not rospy.is_shutdown():
#grab the frame from the threaded video and resize it
#to have a maximum width of 1000 pixels
frame = vs.read()
frame = imutils.resize(frame, width=1000)
#detect ArUCo markers in the input frame
(corners, ids, rejected) = detector.detectMarkers(frame)
print(ids)
#verify *at least* one ArUCo marker was detected
if len(corners) > 0:
#flatten the ArUCo IDs list
ids = ids.flatten()
if [1] in ids:
t.angular.z = 0.1
t.linear.x = 0.5
pub.publish(t)
rate.sleep()
#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)-coordinates 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(frame, topLeft, topRight, (0,255,0), 2)
cv.line(frame, topRight, bottomRight, (0,255,0), 2)
cv.line(frame, bottomRight, bottomLeft, (0,255,0), 2)
cv.line(frame, 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(frame, (cX,cY), 4, (0,0,255), -1)
#draw the ArUCo marker ID on the frame
cv.putText(frame, str(markerID), (topLeft[0], topLeft[1]-15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
#show the output frame
cv.imshow("Frame", frame)
key = cv.waitKey(1) & 0xFF
#if the 'q' key was pressed, break from the loop
if key == ord("q"):
break
#do a bit of cleanup
cv.destroyAllWindows()
vs.stop()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass