Welcome to ROSMASTER M1 repository
11.Palm target positioning
Palm Targeting
1. Content Description
This course implements color image acquisition and palm detection using the MediaPipe framework, outputting the palm coordinates. This can then be combined with a robot chassis or robotic arm to track hand movements. This section requires entering commands in a terminal. The terminal you open depends on your board type. This lesson uses the Raspberry Pi as an example.
For Raspberry Pi and Jetson-Nano boards, you need to open a terminal on the host computer and enter the command to enter the Docker container. Once inside the Docker container, enter the commands mentioned in this lesson in the terminal. For instructions on entering the Docker container from the host computer, refer to [01. Robot Configuration and Operation Guide] -- [4.Enter Docker (For JETSON Nano and RPi 5)].
For RDKX5 and Orin boards, simply open a terminal and enter the commands mentioned in this lesson.
2. Program Startup
For Raspberry Pi and Jetson-Nano boards, you need to enter the Docker container first. For RDKX5 and Orin controllers, this is not necessary.
Enter the Docker container (see [Docker course] --- [4. Docker Startup Script] for steps).
All the following commands must be executed within the same Docker container (see [Docker course] --- [3. Docker Submission and Multi-Terminal Access] for steps).
First, enter the following command in the terminal to start the camera.
#usb cameraros2 launch usb_cam camera.launch.py#nuwa cameraros2 launch ascamera hp60c.launch.pyAfter successfully starting the camera, open another terminal and enter the following command to start the palm tracking program.
xxxxxxxxxxros2 run yahboomcar_mediapipe 11_FindHandAfter starting the program, as shown in the image below, when a hand is detected, it will be outlined in green on the screen, and the center coordinates of the hand will be output in the terminal.

3. Core Code Analysis
Program Code Path:
Raspberry Pi 5 and Jetson Nano Board
The program code is running in Docker. The path in Docker is
/root/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe/11_FindHand.pyOrin board
The program code path is
/home/jetson/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe/11_FindHand.pyRDK X5
The program code path is
/home/sunrise/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe/11_FindHand.py
Import the library files used.
xxxxxxxxxximport threadingimport numpy as npimport timeimport osimport sysimport cv2 as cvimport rclpyfrom rclpy.node import Nodefrom sensor_msgs.msg import Imagefrom std_msgs.msg import Boolfrom geometry_msgs.msg import Twistfrom cv_bridge import CvBridgesys.path.append('/home/jetson/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe')from media_library import *Initialize data and define publishers and subscribers,
xef __init__(self): super().__init__('hand_ctrl_arm_node') #Call the media_library library to create an object of the HandDetector class self.hand_detector = HandDetector() #create a publisher self.bridge = CvBridge() #Define subscribers for the color image topic camera_type = os.getenv('CAMERA_TYPE', 'usb') topic_name = '/ascamera_hp60c/camera_publisher/rgb0/image' if camera_type == 'nuwa' else '/usb_cam/image_raw' self.subscription = self.create_subscription( Image, topic_name, self.image_callback, 10)Color image callback function,
xxxxxxxxxxdef image_callback(self, msg): #Use CvBridge to convert color image message data into image data frame = self.bridge.imgmsg_to_cv2(msg, desired_encoding='bgr8') #Pass the obtained image into the process function for palm detection frame = self.process(frame) cv.imshow('frame', frame)process function,
xxxxxxxxxxdef process(self, frame): # Call the object method to perform palm detection and return the detected image as well as the lmList list and bbox list frame, lmList, bbox = self.hand_detector.findHands(frame) # If the lmList list is not empty, it means that a palm has been detected. if len(lmList) != 0: threading.Thread(target=self.find_hand_threading, args=(lmList, bbox)).start() return framefind_hand_threading function,
xxxxxxxxxxdef find_hand_threading(self, lmList, bbox): fingers = self.hand_detector.fingersUp(lmList) angle = self.hand_detector.ThumbTOforefinger(lmList) value = np.interp(angle, [0, 70], [185, 20]) # Get the xy coordinates of the palm. bbox stores the xy coordinates of the upper left and lower right corners of the palm indexX = (bbox[0] + bbox[2]) / 2 indexY = (bbox[1] + bbox[3]) / 2 print("index X: %.1f, Y: %.1f" % (indexX, indexY))The definition of the Medipipe recognition class can be found in the media_library library, located in the yahboomcar_mediapipe package.
Raspberry Pi 5 and Jetson-Nano boards
The path in Docker is
/root/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe/media_library.pyOrin boards
The program code path is
/home/jetson/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_mediapipe/yahboomcar_mediapipe/media_library.py
In this library, we leverage the native mediapipe library to extend and define multiple classes. Each class defines different functions.
We simply pass in parameters as needed. For example, the HandDetector class defines the following functions:
- findHands: Finds hands
- fingersUp: Fingers extended upward
- ThumbTOforefinger: Detects the angle between the thumb and index finger
- get_gesture: Detects gestures