yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
DOFBOT SE📖
1.First Trial
  • 1.First Trial
  • 2.Analysis of common problems
  • 3.Calibration offset
  • 4.Precautions for DIY Robotic Arm
  • 5.Robotic arm gripping anomaly solutions
2.Assembly Course
  • 1.Installation video
  • 2.Calibrate the Robotic Arm
  • 3.Install the map
3.Remote Control Course
  • 1.Mobile APP control
  • 2.USB handle control
  • 3.PC software control
4.Preparation
  • 1.Manage APP control service
  • 2.Manage JupyterLab service
  • 3.Write STM32 firmware
  • 4.Introduction to expansion board
5.Basic Control Course
  • 1.Control RGB light
  • 2.Control buzzer
  • 3.Control single servo
  • 4.Read servo angle
  • 5.Control all servo
  • 6.DOFBOT up down left right
  • 7.DOFBOT dancing
  • 8.DOFBOT memory action
  • 9.DOFBOT grab block
  • 10.Porter
  • 11.Stack block
  • 12.USB camera display
  • 13.Synchronous action
6.AI Vision Basic Course
  • 1.Gesture Recognition
  • 2.Color threshold adjustment Color calibration
  • 3.Color recognition
  • 4.Face recognition
  • 5.Garbage recognition
  • 6.Yolo object recognition
  • 7.Real-time web view
7.2D Visual Tracking Course
  • 1.PID algorithm basics
  • 2.Color block positioning
  • 3.Color block tracking
  • 4.Face positioning
  • 5.Face tracking
  • 6.Gesture recognition fixed action
8.2D Visual Grasping Course
  • 0.Visual positioning
  • 1.Gesture recognition stack blocks
  • 2.Color recognition grab blocks
  • 3.Catch game
  • 4.Color sorting and stacking
  • 5.Garbage sorting
  • 6.Color interaction
9.2D Mediapipe Course
  • 1.Hand detection
  • 2.Posture detection
  • 3.Overall inspection
  • 4.Face detection
  • 5.Face recognition
  • 6.Face effects
  • 7.3D object recognition
  • 8.Paintbrush
  • 9.Finger control
  • 10.Gesture recognition
  • 11.Gesture control robotic arm action group
  • 12.Arm posture control robotic arm
  • 13.Palm Targeting
  • 14.Robotic arm tracks the palm
  • 15.Fingertip trajectory recognition
  • 16.Fingertip trajectory control robotic arm
  • 17.Gesture Grasp and Release Objects
  • 18.Finger Control Robot Arm
10.AI Large Model Voice Module (Must Read)
  • 1. Introduction to AI Large Model Voice Module
  • 2. Flash the firmware (Must read)
  • 3. Modify the wake-up word and command word
11.2D Voice Control Course
  • 1.Simple Voice Control
  • 2.Smart Voice Control
  • 3.Voice Action Control
  • 4.Memory Learning Mode
  • 5.Color Recognition with Voice Broadcast
  • 6.Color Recognition and Grasping with Voice Broadcast
  • 7.Color Recognition and Sorting with Voice Broadcast
  • 8.Voice Recognition Target Tracking
  • 9.Voice Control Stacking
  • 10.Voice Control Color Sorting
  • 11.Garbage Recognition with Voice Broadcast
  • 12.Garbage Sorting with Voice Broadcast
12.Robotic Arm Control and Inverse Kinematics
  • 1. Robotic arm ROS control
  • 2. Inverse solution
13.3D Sorting and Grasping Course
  • 1.AprilTag ID sorting
  • 2.MediaPipe Gesture-AprilTag ID sorting
  • 3.MediaPipe Gesture-AprilTag distance sorting
  • 4.Block color sorting
  • 5.Block different shape sorting
  • 6.Yolov11 garbage sorting
14.3D Tracking Course
  • 1.AprilTag Tracking
  • 2.Color Block Tracking
  • 3.KCF Object Tracking
15.3D Voice Control for Sorting & Tracking Course
  • 1.Voice Control AprilTag ID Sorting
  • 2.Voice Control Color Block Sorting
  • 3.Voice Control Yolov11 Garbage Recognition Sorting
  • 4.Voice Control AprilTag Tracking
  • 5.Voice Control Color Block Tracking
  • 6.Voice Control Object Tracking
16.AI Model Basics
  • 1.AI large model types and principles
  • 2.RAG retrieval enhancement and model training samples
  • 3.Embodied intelligent robot system architecture
  • 4.Embodied intelligent functions core source code
17.AI Model Development
  • 1. Register a model service provider account
  • 2. Configure API-KEY
  • 3. Introduction to Dify
  • 4. Basic Dify Features
  • 5. AI Large Model Development - chatbot
  • 6. Deploy the RAG knowledge base
  • 7. RAG knowledge base + chatbot
  • 8. AI agent workflow
18.AI Model – Text Version
  • 1.Semantic understand instruction following
  • 2.Multimodal visual understand
  • 3.Text Large Model + robotic arm control
  • 4.Multimodal Large Model+video understanding
  • 5.Multimodal Large Model+robotic arm gripping
  • 6.Multimodal Large Model+robotic arm tracking
  • 7.Multimodal Large Model+robotic arm sorting
  • 8.Multimodal Large Model+robotic arm handling
  • 9.Multimodal Large Model+Color Blocks Back In Place
  • 10.Multimodal Large Model + Intent Prediction + Robotic Arm Gripping
  • 11.Text Large Model + YOLOv11 + Intelligent Garbage Classification
19.AI Model – Voice Version
  • 1.Semantic understand instruction following
  • 2.Multimodal visual understand
  • 3.Text Large Model + robotic arm control
  • 4.Multimodal Large Model+video understanding
  • 5.Multimodal Large Model+robotic arm gripping
  • 6.Multimodal Large Model+robotic arm tracking
  • 7.Multimodal Large Model+robotic arm sorting
  • 8.Multimodal Large Model+robotic arm handling
  • 9.Multimodal Large Model+Color Blocks Back In Place
  • 10.Multimodal Large Model + Intent Prediction + Robotic Arm Gripping
  • 11.Text Large Model + YOLOv11 + Intelligent Garbage Classification
20.AI Model – Fun Applications
  • 1.AI Butler-Intent Inference
  • 2.AI Encyclopedia-Arithmetic Master
  • 3.AI Encyclopedia-You asked I Pointed
  • 4.AI Encyclopedia-Nutritional Analysis
  • 5.AI Encyclopedia-Problem Solving Expert
  • 6.AI Encyclopedia-English Spelling
  • 7.AI Butler-Garbage Sorting
  • 8.AI Top Chef
21.MoveIt2 Course
  • 1. MoveIt Configuration
  • 2. MoveIt Usage
  • 3. MoveIt Drive Real Robot
  • 4. MoveIt Random Movement
  • 5. MoveIt Forward Kinematics Design
  • 6. MoveIt Inverse Kinematics Design
  • 7. MoveIt Cartesian Path
  • 8. MoveIt Scene Design
  • 9. MoveIt Trajectory Planning
  • 10. MoveIt Collision Detection
22.YOLOv11 Development Course
  • 1. YOLO Environment Setup
  • 2. CLI Usage
  • 3. Object Detection
  • 4. Instance Segmentation
  • 5. Pose Estimation
  • 6. Image Classification
  • 7. Oriented Bounding Boxes Object Detection
  • 8. Model Conversion
  • 9. Model Prediction
23. Linux Operating System
  • 1.Introduction to Linux system
  • 2.Ubuntu file system
  • 3.Ubuntu common commands
  • 4.Ubuntu common editors
  • 5.Ubuntu software operation commands
  • 6.Virtual machine installation
  • 7.SSH remote control
  • 8.VNC remote control
  • 9.Transfer files remotely
  • 10.Driver library and communication
  • 11.Static IP and hotspot mode
  • 12.Bind device ID
  • 13.Capacity expansion and resource allocation
  • 14.Update system software sources
  • 15.Set root user password
  • 16.sudo free password
  • 17.Connect to WiFi network
  • 18.View system version
  • 19.Customized service management
  • 20.Back up system image
24.OpenCV Course
  • 1.Introduction to Open Source CV
  • 2.Image reading and display
  • 3.Picture writing
  • 4.Image Quality
  • 5.Pixel operations
  • 6.Image zoom
  • 7.Picture cut
  • 8.Picture pan
  • 9.Picture mirroring
  • 10.Affine transformation
  • 11.Picture rotation
  • 12.perspective transformation
  • 13.Grayscale processing
  • 14.Binary image
  • 15.Edge green detection
  • 16.Line segment drawing
  • 17.Rectangular circle drawing
  • 18.Text and picture drawing
25.Advanced Visuals Course
  • 1. QR Code
  • 2. Human Pose Estimation
  • 3. Object Detection
  • 4. AR Vision
  • 5. ROS + OpenCV Basics
26.ROS2 Basic Course
  • 1.Introduction to ROS2
  • 2.ROS2 install Humble
  • 3.ROS2 development environment
  • 4.ROS2 workspace
  • 5.ROS2 function package
  • 6.ROS2 node
  • 7.ROS2 topic communication
  • 8.ROS2 service communication
  • 9.ROS2 action communication
  • 10.ROS2 custom interface message
  • 11.ROS2 parameter service case
  • 12.ROS2 meta-function package
  • 13.ROS2 distributed communication
  • 14.ROS2 DDS
  • 15.ROS2 time related API
  • 16.ROS2 common command tools
  • 17.ROS2 rviz2 use
  • 18.ROS2 rqt toolbox
  • 19.ROS2 Launch startup file configuration
  • 20.ROS2 recording and playback tool
  • 21.ROS2 URDF model
  • 22.ROS2 Gazebo simulation platform
  • 23.ROS2 TF2 coordinate transformation
27.Jupyter Lab Usage Course
  • 0.Read the document before operation
  • 1.Jupyter Lab environment construction
  • 2.Basic use of Jupyter Widgets
  • 3.Launch JupyterLab
  • 4.How to Use JupyterLab
Download
  • APP(iOS/Android)
  • VM File
  • Instruction manual
  • Hardware_3DModelFile
  • PC_Software
  • Code
  • Communication Protocol-Python Library
  • Tools
Download ZIP

Welcome to DOFBOT SE repository

3.KCF Object Tracking

KCF Object Tracking

1. Function Description

After the program starts, use the left mouse button to draw a box around the object you want to track and grab. Press the spacebar, and the robotic arm will adjust its posture to track the object.

2. Startup and Operation

2.1 Startup Command

Enter the following commands in the terminal to start:

xxxxxxxxxx
# Start camera and inverse kinematics program
ros2 launch dofbot_info camera_arm_kin.launch.py 
# Start robotic arm control program
ros2 run dofbot_sorting_3d grasp
# Start object tracking program
ros2 run dofbot_follow KCF_follow 

2.2 Operation

After the program starts, hold an object, click the mouse in the image frame to select the object you are holding. After releasing the mouse, the object will be outlined in the image. Press the spacebar to start tracking. Slowly move the object, and the robotic arm will adjust its posture.

Note: When tracking objects, move slowly within the camera's field of view. If the tracked object is affected by reflection and loses tracking, try manually adjusting the color block or object position to eliminate reflection influence, or long-press the left mouse button to re-select the target object to track.

3. Core Code Analysis

4.1 KCF_follow.py

Code path:

xxxxxxxxxx
/home/yahboom/dofbot_ws/src/dofbot_follow/dofbot_follow/KCF_follow.py

Import necessary libraries:

xxxxxxxxxx
import cv2
from polars import from_pandas
import rclpy
from rclpy.node import Node
import numpy as np
from message_filters import ApproximateTimeSynchronizer, Subscriber
from sensor_msgs.msg import Image
from std_msgs.msg import Float32, Bool ,Int16MultiArray,String
from cv_bridge import CvBridge
import cv2 as cv
​
import getpass
import threading
encoding = ['16UC1', '32FC1']
import time
import math
import os
from dofbot_interface.msg import AprilTagInfo,ArmJoint
from sensor_msgs.msg import CompressedImage,Image
from std_msgs.msg import Int32, Bool,UInt16
from Arm_Lib import Arm_Device
import dofbot_follow.PID as PID
from  dofbot_follow.Track_Lib import *

Constructor, mainly creating publishers and subscribers:

xxxxxxxxxx
class mono_Tracker(Node):
    # Monocular vision tracker 
    # 单目视觉跟踪
    
    def __init__(self):
        # Initialize the ROS2 node with name 'monoIdentify'
        # 使用名称 'monoIdentify' 初始化ROS2节点
        super().__init__('monoIdentify') 
        
        # Create an instance of the robotic arm device class
        # 创建机械臂设备类的实例
        self.Arm = Arm_Device()
        
        self.point_pose = (0, 0, 0)
        
        # Variables to store detected circle parameters
        # 存储检测到的圆形参数的变量
        self.circle = (0, 0, 0)
        self.circle_r = 0
        
        # Flag for dynamic HSV update
        # 动态HSV更新的标志
        self.dyn_update = True
        
        # Flag for region of interest (ROI) selection
        # 感兴趣区域(ROI)选择的标志
        self.select_flags = False
        
        # Flag for generic tracker state
        # 通用跟踪器的状态标志
        self.gTracker_state = False
        
        # Name of the display window
        # 显示窗口的名称
        self.windows_name = 'frame'
        
        # Initial joint angles for the robotic arm 
        # 机械臂的初始关节角度
        self.init_joints = [90.0, 150.0, 0.0, 20.0, 90.0, 0.0]
        
        # Mouse position coordinates
        # 鼠标位置坐标
        self.cols, self.rows = 0, 0
        
        # Mouse click position
        # 鼠标点击位置
        self.Mouse_XY = (0, 0)
        
        # End flag for tracking
        # 跟踪结束标志
        self.end = 0
        
        # Center coordinates of detected object
        # 检测到的物体中心坐标
        self.cx = 0
        self.cy = 0
​
        # Bridge for converting ROS2 Image messages to OpenCV RGB format
        # 用于将ROS2 Image消息转换为OpenCV RGB格式的桥接器
        self.rgb_bridge = CvBridge()
        
        #Depth information not used, can be ignored
        #没用到深度信息忽略即可
        self.depth_bridge = CvBridge()
​
        # Publisher for publishing target joint angles
        # 发布目标关节角度的发布者
        self.pubPoint = self.create_publisher(ArmJoint, "TargetAngle", 10)
        
        # Publisher for publishing position information
        # 发布位置信息的发布者
        self.pos_info_pub = self.create_publisher(AprilTagInfo, "PosInfo", qos_profile=10)
        
        # Publisher for joint6 angle
        # 发布关节角度的发布者
        self.joint6_pub = self.create_publisher(Float32, 'joint6', 1)
        
        # Subscription to receive raw camera images
        # 订阅以接收原始相机图像
        self.subscription = self.create_subscription(
            Image, '/image_raw', self.ImageCallback, 1)
        
        # Type of visual tracker
        # 视觉跟踪器类型
        self.tracker_type = 'KCF'
        
        # Video display switch
        # 视频显示开关
        self.VideoSwitch = True
        
        # Image flip flag
        # 图像翻转标志
        self.img_flip = False
​
        # Print OpenCV version for debugging
        # 打印OpenCV版本以进行调试
        print("OpenCV Version: ", cv.__version__)
        
        # State machine state for tracking
        # 跟踪的状态机状态
        self.Track_state = 'init'
​
        # Previous time for FPS calculation
        # 用于FPS计算的前一时间点
        self.pr_time = time.time()
        
        # Circle radius variable
        # 圆形半径变量
        self.circle_r = 0 
        
        # Current measured distance
        # 当前测得的距离
        self.cur_distance = 0.0
        
        # Corner coordinates from external source
        # 来自外部源的边角坐标
        self.corner_x = self.corner_y = 0.0
        
        # Move arm to initial position on startup
        # 启动时将机械臂移动到初始位置
        self.Arm.Arm_serial_servo_write6_array(self.init_joints, 2000)
​
        # Subscription to receive corner xy coordinates
        # 订阅以接收边角xy坐标
        self.xy_subscription = self.create_subscription(
            Int16MultiArray, 'corner_xy', self.GetXYCallback, qos_profile=1)
        
        
        # Initialize the visual tracker
        # 初始化视觉跟踪器
        self.gTracker = Tracker(tracker_type=self.tracker_type)
        
        # Flag indicating if xy data has been received
        # 标志位,表示是否已接收到xy数据
        self.get_xy = False
        
        # Initial ROI (x1, y1, x2, y2)
        # 初始感兴趣区域(x1, y1, x2, y2)
        self.Roi_init = (0, 0, 0, 0)
        
        # Variables for storing object position
        # 存储物体位置的变量
        self.px = 0.0
        self.py = 0.0
        
        # Target servo positions for x and y axes
        # x轴和y轴的目标舵机位置
        self.target_servox = 90
        self.target_servoy = 45
        
        # PID controllers for x and y axis servo control
        # 用于x轴和y轴舵机控制的PID控制器
        self.xservo_pid = PID.PositionalPID(0.5, 0.1, 0.1)
        self.yservo_pid = PID.PositionalPID(0.5, 0.1, 0.1)
        
        # Flags indicating if servo output is out of range
        # 标志位,表示舵机输出是否超出范围
        self.y_out_range = False
        self.x_out_range = False
        
        # Current joint angles of the arm
        # 机械臂当前的关节角度
        self.cur_joints = self.init_joints
        
        # Auxiliary flags for tracking logic
        # 跟踪逻辑的辅助标志
        self.a = 0
        self.b = 0
        
        # Flag to start following
        # 开始跟随的标志
        self.start_follow = False

Color image callback function ImageCallback:

xxxxxxxxxx
def ImageCallback(self, color_frame):
    # Convert ROS2 Image message to OpenCV BGR image format
    # 将ROS2 Image消息转换为OpenCV BGR图像格式
    rgb_image = self.rgb_bridge.imgmsg_to_cv2(color_frame, 'bgr8')
    
    # Create a copy of the image for drawing results
    # 创建图像副本用于绘制结果
    result_image = np.copy(rgb_image)
    
    # Capture key press for user interaction
    # 捕获按键以进行用户交互
    action = cv.waitKey(10) & 0xFF
    
    # Spacebar (ASCII 32) toggles the follow mode
    # 空格键(ASCII码32)切换跟随模式
    if action == 32:
        self.start_follow = True
    
    # Resize the image to 640x480 resolution
    # 将图像调整为640x480分辨率
    result_image = cv.resize(result_image, (640, 480))
    
    # Process the image (detect object, track, etc.)
    # 处理图像(检测物体、跟踪等)
    result_frame = self.process(result_image, action)
    
    # Check if object is detected and follow mode is active
    # 检查物体是否被检测到且跟随模式是否激活
    if self.cx != 0 and self.cy != 0 and self.start_follow == True:
        # Check if object center deviates from image center beyond threshold
        # 检查物体中心是否偏离图像中心超过阈值
        # Image center: (320, 240) for 640x480 resolution
        # 图像中心:(320, 240),对应640x480分辨率
        # Threshold: 10 pixels (dead zone to avoid jitter)
        # 阈值:10像素(死区以避免抖动)
        if (abs(self.cx - 320) > 10 or abs(self.cy - 240) > 10):
            # Call XY tracking function to adjust arm position
            # 调用XY跟踪函数以调整机械臂位置
            self.XY_track(self.cx, self.cy)
            
            # Print tracking status
            # 打印跟踪状态
            print("Tracking")
            print("-------------------------------------")
        
        # Draw a circle at the detected object's center
        # 在检测到的物体中心绘制一个圆
        cv2.circle(result_frame, (int(self.cx), int(self.cy)), 10, (255, 255, 0), thickness=None, lineType=None, shift=None)
    
    # Calculate and display FPS (frames per second)
    # 计算并显示FPS(每秒帧数)
    cur_time = time.time()
    fps = str(int(1 / (cur_time - self.pr_time)))
    self.pr_time = cur_time
    cv2.putText(result_frame, fps, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    
    # Display the result image in a window
    # 在窗口中显示结果图像
    cv.imshow(self.windows_name, result_frame)

Process function, draw box for object selection, update coordinates:

​x
def process(self, rgb_img, action):        
    # Resize the input image to 640x480 resolution
    # 将输入图像调整为640x480分辨率
    rgb_img = cv.resize(rgb_img, (640, 480))
​
    # If 'r' key is pressed, reset the track
    # 如果按下 'r' 键,重置
    if action == ord('r') or action == 114: 
        self.Reset()
​
    # State machine: Initialization state
    # 状态机:初始化状态
    if self.Track_state == 'init':
        # Create a named window for mouse interaction
        # 创建鼠标交互的命名窗口
        cv.namedWindow(self.windows_name, cv.WINDOW_AUTOSIZE)
        # Set mouse callback for selecting ROI
        # 设置鼠标回调以选择感兴趣区域
        cv.setMouseCallback(self.windows_name, self.onMouse, 0)
        
        if self.select_flags == True:
            # Draw a line from start to current mouse position
            # 从起点到当前鼠标位置绘制线条
            cv.line(rgb_img, self.cols, self.rows, (255, 0, 0), 2)
            # Draw a rectangle for the ROI
            # 为ROI绘制矩形
            cv.rectangle(rgb_img, self.cols, self.rows, (0, 255, 0), 2)
                
    # State machine: Tracking state
    # 状态机:跟踪状态
    if self.Track_state != 'init':
​
        if self.gTracker_state == True:
            # Validate the ROI coordinates
            # 验证ROI坐标的有效性
            x1, y1, x2, y2 = self.Roi_init
            left = min(x1, x2)
            top = min(y1, y2)
            right = max(x1, x2)
            bottom = max(y1, y2)
            
            # Check if ROI is within image boundaries
            # 检查ROI是否在图像边界内
            img_height, img_width = rgb_img.shape[:2]
            left = max(0, min(left, img_width - 1))
            top = max(0, min(top, img_height - 1))
            right = max(0, min(right, img_width))
            bottom = max(0, min(bottom, img_height))
            
            # Calculate ROI dimensions
            # 计算ROI的尺寸
            roi_width = right - left
            roi_height = bottom - top
            
            # Ensure ROI has valid dimensions
            # 确保ROI尺寸有效
            if roi_width > 10 and roi_height > 10:  # Minimum 10x10 pixels
                Roi = (left, top, roi_width, roi_height)  
                # Reinitialize the tracker with the selected ROI
                # 使用选定的ROI重新初始化跟踪器
                self.gTracker = Tracker(tracker_type=self.tracker_type)
                try:
                    self.gTracker.initWorking(rgb_img, Roi)
                    self.gTracker_state = False
                    print(f"Successfully initialized tracker with ROI: {Roi}")
                except Exception as e:
                    print(f"Failed to initialize tracker: {e}")
                    self.Track_state = 'init'  # Return to init state
            else:
                # Invalid ROI dimensions
                # ROI尺寸无效
                print(f"Invalid ROI dimensions: width={roi_width}, height={roi_height}")
                self.Track_state = 'init'  # Return to init state
        
        # Perform tracking only if the tracker is successfully initialized
        # 仅当跟踪器成功初始化后才进行跟踪
        if hasattr(self, 'gTracker') and self.gTracker is not None:
            # Track the object in the current frame
            # 在当前帧中跟踪物体
            rgb_img, (targBegin_x, targBegin_y), (targEnd_x, targEnd_y) = self.gTracker.track(rgb_img)
            
            # Calculate the center of the tracked object
            # 计算跟踪物体的中心
            center_x = targEnd_x / 2 + targBegin_x / 2
            center_y = targEnd_y / 2 + targBegin_y / 2
            
            # Update the global center coordinates
            # 更新全局中心坐标
            self.cx = center_x
            self.cy = center_y
            print(self.cx)
            print(self.cy)
    
    # Return the processed image
    # 返回处理后的图像
    return rgb_img
Color Block Tracking
Voice Control AprilTag ID Sorting

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文