Skip to content

Commit

Permalink
fixed aruco
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSaliba01 committed Aug 12, 2024
1 parent 1c42b01 commit a6ca0fe
Showing 1 changed file with 57 additions and 43 deletions.
100 changes: 57 additions & 43 deletions camera_data/src/aruco.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,74 @@
import cv2
import numpy as np
import rospy
from std_msgs.msg import String
from cv2 import aruco
import base64

def main():
# Import aruco after cv2
from cv2 import aruco

# Load the ArUco dictionary and parameters
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_4X4_100)
parameters = aruco.DetectorParameters()
detector = cv2.aruco.ArucoDetector(aruco_dict, parameters)
class Node_ArucoDetector():

# Open the video capture (0 for the default camera, or provide a video file path)
cap = cv2.VideoCapture(1)
def __init__(self):
rospy.init_node('aruco_detector')
self.pub1 = rospy.Publisher(f'camera_frames_1', String, queue_size=10)
self.main()

if not cap.isOpened():
print("Error: Could not open video capture.")
return

while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
def main(self):
# Import aruco after cv2
rate = rospy.Rate(30)

# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Load the ArUco dictionary and parameters
aruco_dict = aruco.getPredefinedDictionary(aruco.DICT_4X4_100)
parameters = aruco.DetectorParameters()
detector = cv2.aruco.ArucoDetector(aruco_dict, parameters)

# Detect ArUco markers
corners, ids, rejectedImgPoints = detector.detectMarkers(frame)
# Open the video capture (0 for the default camera, or provide a video file path)
cap = cv2.VideoCapture(1)

# Draw detected markers
if ids is not None:
frame_markers = cv2.aruco.drawDetectedMarkers(frame, corners, ids, borderColor=(0, 255, 0)) # Green border
if not cap.isOpened():
print("Error: Could not open video capture.")
return

# Draw the marker IDs as text
for i, marker_id in enumerate(ids.flatten()):
# Calculate text position
x, y = int(corners[i][0][0][0]), int(corners[i][0][0][1])
text = f"ID: {marker_id}"
while not rospy.is_shutdown():
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break

# Put text on the frame with red color
cv2.putText(frame_markers, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2, cv2.LINE_AA)
else:
frame_markers = frame
# Detect ArUco markers
corners, ids, rejectedImgPoints = detector.detectMarkers(frame)

# Display the frame with markers
cv2.imshow('ArUco Marker Detection', frame_markers)
# Draw detected markers
if ids is not None:
frame_markers = cv2.aruco.drawDetectedMarkers(frame, corners, ids, borderColor=(0, 255, 0)) # Green border

# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Draw the marker IDs as text
for i, marker_id in enumerate(ids.flatten()):
# Calculate text position
x, y = int(corners[i][0][0][0]), int(corners[i][0][0][1])
text = f"ID: {marker_id}"

# Put text on the frame with red color
cv2.putText(frame_markers, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2, cv2.LINE_AA)
else:
frame_markers = frame

# Display the frame with markers

frame_resized = cv2.resize(frame, (640, 480))
# Convert the frame to JPEG format and compress
_, encoded_image = cv2.imencode('.jpg', frame_resized, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
# Convert to base64
base64_image = base64.b64encode(encoded_image).decode('utf-8')
# Publish as a string
self.pub1.publish(base64_image)
rate.sleep()

# Release the capture and close windows
cap.release()

# Release the capture and close windows
cap.release()
cv2.destroyAllWindows()

if __name__ == "__main__":
main()
Node_ArucoDetector()

0 comments on commit a6ca0fe

Please sign in to comment.