• 免费好用的星瞳AI云服务上线!简单标注,云端训练,支持OpenMV H7和OpenMV H7 Plus。可以替代edge impulse。 https://forum.singtown.com/topic/9519
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 如何解决NameError: local variable referenced before assignment



    • import sensor, image, time,math,pyb
      from pyb import UART,LED
      import json
      import ustruct
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      sensor.set_auto_gain(False) # must be turned off for color tracking
      sensor.set_auto_whitebal(False) # must be turned off for color tracking
      red_threshold_01=(10, 100, 127, 32, -43, 67)
      clock = time.clock()
      
      uart = UART(3,115200)   #定义串口3变量
      uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
      
      def find_max(blobs):    #定义寻找色块面积最大的函数
          max_size=0
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob=blob
                  max_size = blob.pixels()
          return max_blob
      
      
      def sending_data(cx,cy,cw,ch):
          global uart;
          #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
          #data = bytearray(frame)
          data = ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                         0x2C,                      #帧头1
                         0x12,                      #帧头2
                         int(cx), # up sample by 4   #数据1
                         int(cy), # up sample by 4    #数据2
                         int(cw), # up sample by 4    #数据1
                         int(ch), # up sample by 4    #数据2
                         0x5B)
          uart.write(data);   #必须要传入一个字节数组
      
      
      while(True):
          clock.tick()
          img = sensor.snapshot()
          blobs = img.find_blobs([red_threshold_01])
          max_b = find_max(blobs)
          cx=0;cy=0;
          if blobs:
                  #如果找到了目标颜色
                  cx=max_b[5]
                  cy=max_b[6]
                  cw=max_b[2]
                  ch=max_b[3]
                  img.draw_rectangle(max_b[0:4]) # rect
                  img.draw_cross(max_b[5], max_b[6]) # cx, cy
                  FH = bytearray([0x2C,0x12,cx,cy,cw,ch,0x5B])
                  #sending_data(cx,cy,cw,ch)
                  uart.write(FH)
                  print(cx,cy,cw,ch)
      

      0_1672457242908_openmv.png



    • 改为

      def find_max(blobs):    #定义寻找色块面积最大的函数
          max_size=0
          max_blob=None
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob=blob
                  max_size = blob.pixels()
          return max_blob