myAGV JN 2023 Camera Recognition of ArUco Codes
Environment Setup
JetPack Version "7.2"
Python Version "3.8.10"
opencv-python Version "4.6.0.66"
opencv-contrib-python Version "4.6.0.66"
Camera Model "IMX219"
Checking Camera Functionality
Enter the following command in the terminal to check the camera display. If the camera display appears, it indicates that the camera functionality is normal.
nvgstcapture
Using OpenCV to Drive CSI Camera
- Open the camera using the Gstreamer pipeline. Install Gstreamer.
sudo add-apt-repository universe
sudo add-apt-repository multiverse
sudo apt-get update
sudo apt-get install gstreamer1.0-tools gstreamer1.0-alsa gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer-plugins-bad1.0-dev
- Create a new Python script:
sudo vim opencv_aruco.py
- Write Python code to utilize OpenCV for ArUco code recognition.
import cv2
import cv2.aruco as aruco
def gstreamer_pipeline(
sensor_id=0,
capture_width=1920,
capture_height=1080,
display_width=960,
display_height=540,
framerate=30,
flip_method=0,
):
return (
"nvarguscamerasrc sensor-id=%d !"
"video/x-raw(memory:NVMM), width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
sensor_id,
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def show_camera():
window_title = "CSI Camera"
print(gstreamer_pipeline(flip_method=0))
video_capture = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
if video_capture.isOpened():
try:
window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
while True:
ret_val, frame = video_capture.read()
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_50)
corners, ids, rejectedImgPoints = aruco.detectMarkers(frame, aruco_dict)
if ids is not None:
aruco.drawDetectedMarkers(frame, corners, ids)
for i in range(len(ids)):
cv2.putText(frame, str(ids[i][0]), (corners[i][0, 0, 0], corners[i][0, 0, 1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2, cv2.LINE_AA)
if cv2.getWindowProperty(window_title, cv2.WND_PROP_AUTOSIZE) >= 0:
cv2.imshow(window_title, frame)
else:
break
keyCode = cv2.waitKey(10) & 0xFF
# Stop the program on the ESC key or 'q'
if keyCode == 27 or keyCode == ord('q'):
break
finally:
video_capture.release()
cv2.destroyAllWindows()
else:
print("Error: Unable to open camera")
if __name__ == "__main__":
show_camera()
- To run the program:
After editing, press Esc to switch to command mode, then type :wq
and press Enter to automatically save and exit. Then enter sudo python3 opencv_aruco.py
to run the program.
myAGV Pi 2023 Camera Recognition of ArUco Codes
Environment Setup
Python Version "3.8.10"
opencv-python Version "4.6.0.66"
opencv-contrib-python Version "4.6.0.66"
Camera Model "Raspberry Pi Camera Rev1.3"
Checking Camera Functionality
Enter the following command in the terminal to check the camera display. If the camera display appears, it indicates that the camera functionality is normal.
sudo mplayer tv://
Using OpenCV to Drive Camera for ArUco Code Recognition
- Create a new Python script:
sudo vim opencv_aruco.py
- Write Python code to utilize OpenCV for ArUco code recognition.
import cv2
import numpy as np
# Create ArUco dictionary
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_6X6_50)
# Create ArUco detector
aruco_params = cv2.aruco.DetectorParameters()
# Open the camera
cap = cv2.VideoCapture(0)
# Intrinsic matrix of the camera
camera_matrix = np.array([
[781.33379113, 0., 347.53500524],
[0., 783.79074192, 246.67627253],
[0., 0., 1.]])
# Distortion coefficients of the camera
dist_coeffs = np.array(([[3.41360787e-01, -2.52114260e+00, -1.28012469e-03, 6.70503562e-03,
2.57018000e+00]]))
while True:
# Read frames from the camera
ret, frame = cap.read()
# Detect ArUco markers
corners, ids, rejected = cv2.aruco.detectMarkers(frame, aruco_dict, parameters=aruco_params)
if ids is not None:
# Draw detected ArUco markers
cv2.aruco.drawDetectedMarkers(frame, corners, ids)
# Estimate the pose of ArUco markers
rvecs, tvecs, _ = cv2.aruco.estimatePoseSingleMarkers(corners, 0.05, camera_matrix, dist_coeffs)
for i in range(len(ids)):
# Draw coordinate axes on detected ArUco markers
cv2.drawFrameAxes(frame, camera_matrix, dist_coeffs, rvecs[i], tvecs[i], 0.1)
# Display frames
cv2.imshow('ArUco Detection', frame)
# Press ESC key to exit the loop
if cv2.waitKey(1) & 0xFF == 27:
break
# Release the camera and close windows
cap.release()
cv2.destroyAllWindows()
- Run the program
After editing, press Esc to switch to command mode, then type :wq
and press Enter to automatically save and exit. Then enter sudo python3 opencv_aruco.py
to run the program.