Dateien nach "den" hochladen

This commit is contained in:
2023-12-14 13:22:26 +01:00
parent 5aacbd541e
commit dcd984c53c
5 changed files with 33827 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python
# Taken from here:http://wiki.ros.org/cv_bridge/Tutorials/ConvertingBetweenROSImagesAndOpenCVImagesPython
# USE: chmod +x
from __future__ import print_function
import roslib
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
class image_converter:
def __init__(self):
self.image_pub = rospy.Publisher("image_topic_2", Image)
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber("image_topic", Image, self.callback)
def callback(self, data):
try:
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print(e)
(rows, cols, channels) = cv_image.shape
if cols > 60 and rows > 60:
cv2.circle(cv_image, (50, 50), 10, 255)
cv2.imshow("Image window", cv_image)
cv2.waitKey(3)
try:
self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
except CvBridgeError as e:
print(e)
def main(args):
ic = image_converter()
rospy.init_node("image_converter", anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == "__main__":
main(sys.argv)
+33314
View File
File diff suppressed because it is too large Load Diff
+194
View File
@@ -0,0 +1,194 @@
#!/usr/bin/env python
from __future__ import print_function
import roslib
import sys
import rospy
import cv2
import argparse
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist
ARUCO_DICT = {
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
"DICT_5X5_100": cv2.aruco.DICT_5X5_100,
"DICT_5X5_250": cv2.aruco.DICT_5X5_250,
"DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv2.aruco.DICT_6X6_50,
"DICT_6X6_100": cv2.aruco.DICT_6X6_100,
"DICT_6X6_250": cv2.aruco.DICT_6X6_250,
"DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv2.aruco.DICT_7X7_50,
"DICT_7X7_100": cv2.aruco.DICT_7X7_100,
"DICT_7X7_250": cv2.aruco.DICT_7X7_250,
"DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
"DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5,
"DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9,
"DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10,
"DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11,
}
class image_converter:
def __init__(self):
# self.image_pub = rospy.Publisher("image_topic_2",Image)
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber(
"/camera/color/image_raw", Image, self.callback
)
ap = argparse.ArgumentParser()
ap.add_argument(
"-t",
"--type",
type=str,
default="DICT_4X4_50",
help="type of ArCUo tag to detect",
)
args = vars(ap.parse_args())
self.arucoDict = cv2.aruco.getPredefinedDictionary(ARUCO_DICT[args["type"]])
self.arucoParams = cv2.aruco.DetectorParameters_create()
### For turtle controll
self.pub = rospy.Publisher("/cmd_vel_mux/input/navi", Twist, queue_size=10)
rospy.init_node("turtle_control", anonymous=True)
self.rate = rospy.Rate(200) # update
self.t = Twist()
### Markers haven been detected, now do something
def markDetected(self, ids, img, corners):
(h, w) = img.shape[:2]
cv2.circle(img, (w//2, h//2), 100, (255, 255, 255), -1)
thresh_left = w//2 - 100
thresh_right = w//2 + 100
cv2.line(img, (thresh_left, h//2), (thresh_right, h//2), (0,0,0), 2)
for markerCorner in corners:
corners = markerCorner.reshape((4,2))
(top_left, top_right, bottom_right, bottom_left) = corners
top_left = (int(top_left[0]), int(top_left[1]))
top_right = (int(top_right[0]), int(top_right[1]))
bottom_right = (int(bottom_right[0]), int(bottom_right[1]))
bottom_left = (int(bottom_left[0]), int(bottom_left[1]))
cv2.circle(img, top_left, 10, (0,255,0), -1)
print("top_left(",top_left[0],",",top_left[1],")")
print("top_right(",top_right[0],",",top_right[1],")")
print("bottom_left(",bottom_left[0],",",bottom_left[1],")")
print("bottom_right(",bottom_right[0],",",bottom_right[1],")")
if ((top_left[0] > thresh_left and top_left[0] < thresh_right) or
(top_right[0] > thresh_left and top_right[0] < thresh_right) or
(bottom_left[0] > thresh_left and bottom_left[0] < thresh_right) or
(bottom_right[0] > thresh_left and bottom_right[0] < thresh_right)):
print("##########################")
print("Object found")
print("top_left(",top_left[0],",",top_left[1],")")
print("top_right(",top_right[0],",",top_right[1],")")
print("bottom_left(",bottom_left[0],",",bottom_left[1],")")
print("bottom_right(",bottom_right[0],",",bottom_right[1],")")
print(thresh_left)
print(thresh_right)
print("##############################################")
self.t.linear.x = 0.1
self.pub.publish(self.t)
self.rate.sleep()
elif(top_right[0] < thresh_left or
bottom_right[0] < thresh_left):
print("##########################")
print("Object found")
print("top_left(",top_left[0],",",top_left[1],")")
print("top_right(",top_right[0],",",top_right[1],")")
print("bottom_left(",bottom_left[0],",",bottom_left[1],")")
print("bottom_right(",bottom_right[0],",",bottom_right[1],")")
print(thresh_left)
print(thresh_right)
print("##############################################")
self.t.angular.z = 0.2
self.pub.publish(self.t)
self.rate.sleep()
elif(top_left[0] > thresh_right or
bottom_left[0] > thresh_right):
print("##########################")
print("Object found")
print("top_left(",top_left[0],",",top_left[1],")")
print("top_right(",top_right[0],",",top_right[1],")")
print("bottom_left(",bottom_left[0],",",bottom_left[1],")")
print("bottom_right(",bottom_right[0],",",bottom_right[1],")")
print(thresh_left)
print(thresh_right)
print("##############################################")
self.t.angular.z = -0.2
self.pub.publish(self.t)
self.rate.sleep()
#print(corners[0][0])
ids = ids.flatten()
#if [1] in ids: # ID:1
# self.t.angular.z = 0.0
# self.t.linear.x = 0.3
#elif [2] in ids:
# self.t.angular.z = 0.0
# self.t.linear.x = -0.3
#elif [3] in ids:
# self.t.angular.z = 0.9
# self.t.linear.x = 0.0
#else:
# return
#self.pub.publish(self.t)
#self.rate.sleep()
return img
def callback(self, data):
try:
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print(e)
(rows, cols, channels) = cv_image.shape
if cols > 60 and rows > 60:
#cv2.circle(cv_image, (50, 50), 10, 255)
pass
(corners, ids, rejected) = cv2.aruco.detectMarkers(
cv_image, self.arucoDict, parameters=self.arucoParams
)
idString = "IDs:" + str(ids)
cv2.putText(cv_image, idString, (50,50), cv2.FONT_HERSHEY_SIMPLEX , 1, (200, 100, 0), 3, cv2.LINE_AA)
#####
# verify *at least* one ArUCo marker was detected
if len(corners) > 0:
cv_image = self.markDetected(ids, cv_image, corners)
#else:
#self.t.angular.z = 0.8
#self.t.linear.x = 0.2
#self.pub.publish(self.t)
#self.rate.sleep()
#self.t.angular.z = -0.8
#self.pub.publish(self.t)
#self.rate.sleep()
#####
cv2.imshow("Image window", cv_image)
cv2.waitKey(3)
def main(args):
ic = image_converter()
# rospy.init_node('image_converter', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == "__main__":
main(sys.argv)
+140
View File
@@ -0,0 +1,140 @@
#!/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_create()
# 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) = cv2.aruco.detectMarkers(
frame, arucoDict, parameters=arucoParams
)
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
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python
from __future__ import print_function
import roslib
import sys
import rospy
import cv2
import argparse
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
from geometry_msgs.msg import Twist
ARUCO_DICT = {
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
"DICT_5X5_100": cv2.aruco.DICT_5X5_100,
"DICT_5X5_250": cv2.aruco.DICT_5X5_250,
"DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv2.aruco.DICT_6X6_50,
"DICT_6X6_100": cv2.aruco.DICT_6X6_100,
"DICT_6X6_250": cv2.aruco.DICT_6X6_250,
"DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv2.aruco.DICT_7X7_50,
"DICT_7X7_100": cv2.aruco.DICT_7X7_100,
"DICT_7X7_250": cv2.aruco.DICT_7X7_250,
"DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
"DICT_APRILTAG_16h5": cv2.aruco.DICT_APRILTAG_16h5,
"DICT_APRILTAG_25h9": cv2.aruco.DICT_APRILTAG_25h9,
"DICT_APRILTAG_36h10": cv2.aruco.DICT_APRILTAG_36h10,
"DICT_APRILTAG_36h11": cv2.aruco.DICT_APRILTAG_36h11,
}
class image_converter:
def __init__(self):
# self.image_pub = rospy.Publisher("image_topic_2",Image)
self.bridge = CvBridge()
self.image_sub = rospy.Subscriber(
"/camera/color/image_raw", Image, self.callback
)
ap = argparse.ArgumentParser()
ap.add_argument(
"-t",
"--type",
type=str,
default="DICT_4X4_50",
help="type of ArCUo tag to detect",
)
args = vars(ap.parse_args())
self.arucoDict = cv2.aruco.getPredefinedDictionary(ARUCO_DICT[args["type"]])
self.arucoParams = cv2.aruco.DetectorParameters_create()
### For turtle controll
self.pub = rospy.Publisher("/cmd_vel_mux/input/navi", Twist, queue_size=10)
rospy.init_node("turtle_control", anonymous=True)
self.rate = rospy.Rate(200) # update
self.t = Twist()
### Markers haven been detected, now do something
def markDetected(self, ids):
ids = ids.flatten()
if [1] in ids: # ID:1
self.t.angular.z = 0.0
self.t.linear.x = 0.3
elif [2] in ids:
self.t.angular.z = 0.0
self.t.linear.x = -0.3
elif [3] in ids:
self.t.angular.z = 0.9
self.t.linear.x = 0.0
else:
return
self.pub.publish(self.t)
self.rate.sleep()
def callback(self, data):
try:
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
except CvBridgeError as e:
print(e)
(rows, cols, channels) = cv_image.shape
if cols > 60 and rows > 60:
#cv2.circle(cv_image, (50, 50), 10, 255)
pass
(corners, ids, rejected) = cv2.aruco.detectMarkers(
cv_image, self.arucoDict, parameters=self.arucoParams
)
idString = "IDs:" + str(ids)
cv2.putText(cv_image, idString, (50,50), cv2.FONT_HERSHEY_SIMPLEX , 1, (200, 100, 0), 3, cv2.LINE_AA)
#####
# verify *at least* one ArUCo marker was detected
if len(corners) > 0:
self.markDetected(ids)
else:
self.t.angular.z = 0.8
self.t.linear.x = 0.2
self.pub.publish(self.t)
self.rate.sleep()
#####
cv2.imshow("Image window", cv_image)
cv2.waitKey(3)
def main(args):
ic = image_converter()
# rospy.init_node('image_converter', anonymous=True)
try:
rospy.spin()
except KeyboardInterrupt:
print("Shutting down")
cv2.destroyAllWindows()
if __name__ == "__main__":
main(sys.argv)