OpenMV:20追踪其他物体的云台

[复制链接]
查看838 | 回复0 | 2023-8-23 11:47:05 | 显示全部楼层 |阅读模式
和追踪小车的原理是一样的
起首得到目标物体的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()的返回值是不一样的!
  1. import sensor, image, time
  2. from pid import PID
  3. from pyb import Servo
  4. pan_servo=Servo(1)
  5. tilt_servo=Servo(2)
  6. pan_servo.calibration(500,2500,500)
  7. tilt_servo.calibration(500,2500,500)
  8. red_threshold  = (13, 49, 18, 61, 6, 47)
  9. pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  10. tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  11. #pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  12. #tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  13. sensor.reset() # Initialize the camera sensor.
  14. sensor.set_contrast(1)  # 插入:设置对比度
  15. sensor.set_gainceiling(16)  # 插入:设置增益
  16. # 由于云台上的OpenMV是反着装的,而检测人脸的haar算子是正着看的,所以就必须把照片倒过来才能检测成功
  17.     # 我们不用那么麻烦:直接设置图像为镜像模式,是垂直方向的翻转即可
  18. sensor.set_vflip(True)
  19. sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
  20. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  21. sensor.skip_frames(10) # Let new settings take affect.
  22. sensor.set_auto_whitebal(False) # turn this off.
  23. clock = time.clock() # Tracks FPS.
  24. # 加载人脸的haar算子
  25. face_cascade = image.HaarCascade("frontalface" , stages = 25)
  26. def find_max(blobs):
  27.     max_size=0
  28.     for blob in blobs:
  29.         if blob[2]*blob[3] > max_size:
  30.             max_blob=blob
  31.             max_size = blob[2]*blob[3]
  32.     return max_blob
  33. while(True):
  34.     clock.tick() # Track elapsed milliseconds between snapshots().
  35.     img = sensor.snapshot() # 截取一张图片
  36.    
  37.     # 截取一张图片后,对我们的图像进行find_features()
  38.     # objects是返回的人脸矩形框(x,y,w,h)
  39.     objects = img.find_features(face_cascade,threshold = 0.75,scale = 1.35)
  40.    
  41.    
  42.     if objects: # 如果识别到人脸——>找到视野中最大的人脸
  43.         max_object = find_max(objects)
  44.         pan_error = max_object[0] +  max_object[2]/2 - img.width()/2
  45.         tilt_error =  max_object[1] +  max_object[3]/2  - img.height()/2
  46.         print("pan_error: ", pan_error)
  47.         img.draw_rectangle( max_object.rect()) # rect
  48.         img.draw_cross(int( max_object[0] +  max_object[2]/2),int( max_object[1] +  max_object[3]/2)) # cx, cy
  49.         pan_output=pan_pid.get_pid(pan_error,1)/2
  50.         tilt_output=tilt_pid.get_pid(tilt_error,1)
  51.         print("pan_output",pan_output)
  52.         pan_servo.angle(pan_servo.angle()+pan_output)
  53.         tilt_servo.angle(tilt_servo.angle()-tilt_output)
复制代码

追踪AprilTags的云台

搜刮函数: objects = img.find_apriltags()
  1. import sensor, image, time
  2. from pid import PID
  3. from pyb import Servo
  4. pan_servo=Servo(1)
  5. tilt_servo=Servo(2)
  6. pan_servo.calibration(500,2500,500)
  7. tilt_servo.calibration(500,2500,500)
  8. red_threshold  = (13, 49, 18, 61, 6, 47)
  9. pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  10. tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  11. #pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  12. #tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  13. sensor.reset() # Initialize the camera sensor.
  14. sensor.set_contrast(1)  # 插入:设置对比度
  15. sensor.set_gainceiling(16)  # 插入:设置增益
  16. sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
  17. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  18. sensor.skip_frames(10) # Let new settings take affect.
  19. sensor.set_auto_whitebal(False) # turn this off.
  20. clock = time.clock() # Tracks FPS.
  21. # 加载人脸的haar算子
  22. face_cascade = image.HaarCascade("frontalface" , stages = 25)
  23. def find_max(blobs):
  24.     max_size=0
  25.     for blob in blobs:
  26.         if blob[2]*blob[3] > max_size:
  27.             max_blob=blob
  28.             max_size = blob[2]*blob[3]
  29.     return max_blob
  30. while(True):
  31.     clock.tick() # Track elapsed milliseconds between snapshots().
  32.     img = sensor.snapshot() # 截取一张图片
  33.    
  34.     # 截取一张图片后,对我们的图像进行find_apriltags()
  35.     # 函数的返回值是一个AprilTag的对象列表
  36.     objects = img.find_apriltags()
  37.    
  38.    
  39.     if objects: # 如果识别到人脸——>找到视野中最大的人脸
  40.         max_object = find_max(objects)
  41.         pan_error = max_object[0] +  max_object[2]/2 - img.width()/2
  42.         tilt_error =  max_object[1] +  max_object[3]/2  - img.height()/2
  43.         print("pan_error: ", pan_error)
  44.         img.draw_rectangle( max_object.rect()) # rect
  45.         img.draw_cross(int( max_object[0] +  max_object[2]/2),int( max_object[1] +  max_object[3]/2)) # cx, cy
  46.         pan_output=pan_pid.get_pid(pan_error,1)/2
  47.         tilt_output=tilt_pid.get_pid(tilt_error,1)
  48.         print("pan_output",pan_output)
  49.         pan_servo.angle(pan_servo.angle()+pan_output)
  50.         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]即可
  1. import sensor, image, time
  2. from pid import PID
  3. from pyb import Servo
  4. pan_servo=Servo(1)
  5. tilt_servo=Servo(2)
  6. pan_servo.calibration(500,2500,500)
  7. tilt_servo.calibration(500,2500,500)
  8. red_threshold  = (13, 49, 18, 61, 6, 47)
  9. pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  10. tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
  11. #pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  12. #tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
  13. sensor.reset() # Initialize the camera sensor.
  14. sensor.set_contrast(1)  # 插入:设置对比度
  15. sensor.set_gainceiling(16)  # 插入:设置增益
  16. sensor.set_pixformat(sensor.GRAYSCALE) # 人脸识别最好采用灰度图模式
  17. sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
  18. sensor.skip_frames(10) # Let new settings take affect.
  19. sensor.set_auto_whitebal(False) # turn this off.
  20. clock = time.clock() # Tracks FPS.
  21. def find_max(blobs):
  22.     max_size=0
  23.     for blob in blobs:
  24.         if blob[2]*blob[2] > max_size:
  25.             max_blob=blob
  26.             max_size = blob[2]*blob[2]
  27.     return max_blob
  28. while(True):
  29.     clock.tick() # Track elapsed milliseconds between snapshots().
  30.     img = sensor.snapshot().lens_corr(1,8)  # 截取一张图片的同时进行畸变矫正
  31.    
  32.     # 截取一张图片后,对我们的图像进行find_apriltags()
  33.     # 函数的返回值是一个圆的圆心在摄像头里的xy坐标以及半径(x,y,r)
  34.     objects = img.find_circles(threshold = 3500,x_margin = 10,y_margin = 10,r_margin = 10,r_min = 2,r_max = 100,r_step = 2)
  35.    
  36.    
  37.     if objects: # 如果识别到人脸——>找到视野中最大的人脸
  38.         max_object = find_max(objects)
  39.         pan_error = max_object[0]  - img.width()/2    # 圆心的x坐标即为object[0]
  40.         tilt_error =  max_object[1]   - img.height()/2    # 圆心的y坐标即为object[1]
  41.         print("pan_error: ", pan_error)
  42.         img.draw_rectangle( max_object.rect()) # rect
  43.         # 直接改为画圆而非矩形
  44.         img.draw_circle(max_object[0],max_object[1],max_object[2],color = (255,0,0))
  45.         img.draw_cross(int(max_object[0]),int(max_object[1]) # 在圆形(x,y)处画十字
  46.         pan_output=pan_pid.get_pid(pan_error,1)/2
  47.         tilt_output=tilt_pid.get_pid(tilt_error,1)
  48.         print("pan_output",pan_output)
  49.         pan_servo.angle(pan_servo.angle()+pan_output)
  50.         tilt_servo.angle(tilt_servo.angle()-tilt_output)
复制代码
来源:https://blog.csdn.net/m0_59466249/article/details/125370966
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则