logo

基于OpenMV和ESP32-S3的二维云台人像追踪实现需求

2026-01-18 18:58
7
7mnug387tmxe

基于OpenMV和ESP32-S3的二维云台人像追踪实现需求

用户希望实现一个基于OpenMV和ESP32-S3的二维云台人像追踪系统,使用数字舵机控制云台,结合PID算法实现对人脸的实时跟踪。

  • 摄像头安装方式:固定在二维云台上
  • 应用场景:复杂背景环境
  • 识别距离:1-3米
  • 追踪目标:支持单人及多人场景下的人脸识别与跟踪
  • 硬件配置:OpenMV、ESP32-S3、数字舵机(Servo 1 和 Servo 2)
  • 控制方法:采用PID算法进行水平(pan)和俯仰(tilt)方向的误差调节
  • 图像处理:使用Haar Cascade模型检测人脸,通过find_features方法定位,并绘制矩形框和中心十字
  • 图像设置:灰度格式(GRAYSCALE),分辨率QVGA(320x240),启用vflip,关闭自动白平衡
  • 未提供实际测试图像或视频资料
import sensor, image, time

from pid import PID
from pyb import Servo

pan_servo=Servo(1)
tilt_servo=Servo(2)

pan_servo.calibration(500,2500,500)
tilt_servo.calibration(500,2500,500)

red_threshold  = (13, 49, 18, 61, 6, 47)

pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID

sensor.reset() # Initialize the camera sensor.
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_pixformat(sensor.GRAYSCALE) # use RGB565.
sensor.set_framesize(sensor.QVGA) # use QQVGA for speed.
sensor.set_vflip(True)
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_whitebal(False) # turn this off.
clock = time.clock() # Tracks FPS.
face_cascade = image.HaarCascade("frontalface", stages=25)

def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob


while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().
    img = sensor.snapshot() # Take a picture and return the image.

    faces = img.find_features(face_cascade, threshold=0.75, scale=1.25)
    if faces:
        face = find_max(faces)
        cx = int(face[0]+face[2]/2)
        cy = int(face[1]+face[3]/2)
        pan_error = cx-img.width()/2
        tilt_error = cy-img.height()/2

        print("pan_error: ", pan_error)

        img.draw_rectangle(face) # rect
        img.draw_cross(cx, cy) # cx, cy

        pan_output=pan_pid.get_pid(pan_error,1)
        tilt_output=tilt_pid.get_pid(tilt_error,1)
        print("pan_output",pan_output)
        pan_servo.angle(int(pan_servo.angle()+pan_output))
        tilt_servo.angle(int(tilt_servo.angle()+tilt_output))
2026-01-19 17:06
Z
星小瞳xs

可以参考的资料和程序:

追小球的云台 · OpenMV中文入门教程