OpenMV云台:实现20个物体的追踪功能

文章目录

  • 追踪人脸的云台
  • 追踪AprilTags的云台
  • 追踪圆形的云台
  • 和追踪小车的原理是一样的

    首先获得目标物体的x,y坐标,然后通过目标物体的xy坐标来控制我们云台的两个舵机的pid运动

    无论追踪什么物体,都是通过物体的x,y坐标来控制云台的运动,对于云台的舵机来说,它只知道传给它的是x,y坐标,并不知道OpenMV传给它的是小球的xy坐标还是人脸的xy坐标

    所以我们只需要修改main.py中的函数即可

    追踪人脸的云台

    搜索函数:objects = img.find_features(face_cascade,threshold ,scale)

    记得要载入haar算子,并且寻找小球的函数find_blob()和寻找人脸的函数find_feartures()的返回值是不一样的!

    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(1)  # 插入:设置对比度
    sensor.set_gainceiling(16)  # 插入:设置增益
    
    # 由于云台上的OpenMV是反着装的,而检测人脸的haar算子是正着看的,所以就必须把照片倒过来才能检测成功
        # 我们不用那么麻烦:直接设置图像为镜像模式,是垂直方向的翻转即可
    sensor.set_vflip(True)
    
    sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
    sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
    sensor.skip_frames(10) # Let new settings take affect.
    sensor.set_auto_whitebal(False) # turn this off.
    clock = time.clock() # Tracks FPS.
    
    
    # 加载人脸的haar算子
    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() # 截取一张图片
        
        # 截取一张图片后,对我们的图像进行find_features()
        # objects是返回的人脸矩形框(x,y,w,h)
        objects = img.find_features(face_cascade,threshold = 0.75,scale = 1.35)
        
        
        if objects: # 如果识别到人脸——>找到视野中最大的人脸
            max_object = find_max(objects)
            pan_error = max_object[0] +  max_object[2]/2 - img.width()/2
            tilt_error =  max_object[1] +  max_object[3]/2  - img.height()/2
    
            print("pan_error: ", pan_error)
    
            img.draw_rectangle( max_object.rect()) # rect
            img.draw_cross(int( max_object[0] +  max_object[2]/2),int( max_object[1] +  max_object[3]/2)) # cx, cy
    
            pan_output=pan_pid.get_pid(pan_error,1)/2
            tilt_output=tilt_pid.get_pid(tilt_error,1)
            print("pan_output",pan_output)
            pan_servo.angle(pan_servo.angle()+pan_output)
            tilt_servo.angle(tilt_servo.angle()-tilt_output)
    
    

    追踪AprilTags的云台

    搜索函数: objects = img.find_apriltags()

    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(1)  # 插入:设置对比度
    sensor.set_gainceiling(16)  # 插入:设置增益
    
    sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
    sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
    sensor.skip_frames(10) # Let new settings take affect.
    sensor.set_auto_whitebal(False) # turn this off.
    clock = time.clock() # Tracks FPS.
    
    
    # 加载人脸的haar算子
    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() # 截取一张图片
        
        # 截取一张图片后,对我们的图像进行find_apriltags()
        # 函数的返回值是一个AprilTag的对象列表
        objects = img.find_apriltags()
        
        
        if objects: # 如果识别到人脸——>找到视野中最大的人脸
            max_object = find_max(objects)
            pan_error = max_object[0] +  max_object[2]/2 - img.width()/2
            tilt_error =  max_object[1] +  max_object[3]/2  - img.height()/2
    
            print("pan_error: ", pan_error)
    
            img.draw_rectangle( max_object.rect()) # rect
            img.draw_cross(int( max_object[0] +  max_object[2]/2),int( max_object[1] +  max_object[3]/2)) # cx, cy
    
            pan_output=pan_pid.get_pid(pan_error,1)/2
            tilt_output=tilt_pid.get_pid(tilt_error,1)
            print("pan_output",pan_output)
            pan_servo.angle(pan_servo.angle()+pan_output)
            tilt_servo.angle(tilt_servo.angle()-tilt_output)
    
    

    追踪圆形的云台

    搜索函数:objects = img.find_circles()

    注意要用到畸变矫正,加在我们的sensor.snapshot()之后img = sensor.snapshot().lens_corr(1,8)

    对于圆来说,比较大小就不用if blob[2]*blob[3] > max_size:了,而是直接比较半径cirle[2]即可

    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(1)  # 插入:设置对比度
    sensor.set_gainceiling(16)  # 插入:设置增益
    
    sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
    sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
    sensor.skip_frames(10) # Let new settings take affect.
    sensor.set_auto_whitebal(False) # turn this off.
    clock = time.clock() # Tracks FPS.
    
    
    
    
    def find_max(blobs):
        max_size=0
        for blob in blobs:
            if blob[2]*blob[2] > max_size:
                max_blob=blob
                max_size = blob[2]*blob[2]
        return max_blob
    
    
    while(True):
        clock.tick() # Track elapsed milliseconds between snapshots().
        img = sensor.snapshot().lens_corr(1,8)  # 截取一张图片的同时进行畸变矫正 
        
        # 截取一张图片后,对我们的图像进行find_apriltags()
        # 函数的返回值是一个圆的圆心在摄像头里的xy坐标以及半径(x,y,r)
        objects = img.find_circles(threshold = 3500,x_margin = 10,y_margin = 10,r_margin = 10,r_min = 2,r_max = 100,r_step = 2)
        
        
        if objects: # 如果识别到人脸——>找到视野中最大的人脸
            max_object = find_max(objects)
            pan_error = max_object[0]  - img.width()/2    # 圆心的x坐标即为object[0]
            tilt_error =  max_object[1]   - img.height()/2    # 圆心的y坐标即为object[1]
    
            print("pan_error: ", pan_error)
    
            img.draw_rectangle( max_object.rect()) # rect
            # 直接改为画圆而非矩形
            img.draw_circle(max_object[0],max_object[1],max_object[2],color = (255,0,0))
            img.draw_cross(int(max_object[0]),int(max_object[1]) # 在圆形(x,y)处画十字
    
            pan_output=pan_pid.get_pid(pan_error,1)/2
            tilt_output=tilt_pid.get_pid(tilt_error,1)
            print("pan_output",pan_output)
            pan_servo.angle(pan_servo.angle()+pan_output)
            tilt_servo.angle(tilt_servo.angle()-tilt_output)
    
    
    物联沃分享整理
    物联沃-IOTWORD物联网 » OpenMV云台:实现20个物体的追踪功能

    发表评论