• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • openmv脱机运行到一半时会卡死,能帮忙看下什么问题吗?



    • import sensor, image, time, math,pyb,utime
      from pyb import UART #引入串口通信
      from pyb import LED  #提示
      
      red_led = LED(1)
      green_led = LED(2)
      blue_led = LED(3)
      
      
      
      global left,middle,right,VAL
      global color
      global flag_color  #标志变量
      
      
      left = 0
      middle = 0
      right = 0
      
      flag_color = 0
      
      red_threshold = (37, 64, 44, 127, -128, 127)
      green_threshold = (18, 68, -62, -28, -128, 127)
      blue_threshold = (46, 13, -33, 14, -52, -9) #红、绿、蓝颜色阈值
      
      red_color_code = 1
      green_color_code = 2
      blue_color_code = 4
      black_color_code = 8
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      sensor.set_auto_gain(False) # 关闭自动增益
      sensor.set_auto_whitebal(False) # 关闭白平衡
      clock = time.clock()
      
      uart = UART(3,9600) #初始化串口通信
      
      def Light():
          red_led.on()
          green_led.on()
          blue_led.on()
      
      if flag_color == 0:
         flag_color = 1
      
      #二维码读取
      while(flag_color == 1):
          clock.tick()
          img0 = sensor.snapshot()
          img0.lens_corr(1.8)
          for QR in img0.find_qrcodes():
              code = QR.payload()
              img0.draw_rectangle(QR.rect(),color = (255,48,48))
              if code == '绿色蓝色':
                  utime.sleep_ms(2000)
                  uart.write('23')
                  uart.write('0')
                  print('23')
                  flag_color = 2
                  break
              if code == '绿色红色':
                  utime.sleep_ms(2000)
                  uart.write('21')
                  uart.write('0')
                  print('21')
                  flag_color = 2
                  break
              if code == '蓝色绿色':
                  utime.sleep_ms(2000)
                  uart.write('32')
                  uart.write('0')
                  print('32')
                  flag_color = 2
                  break
              if code == '蓝色红色':
                  utime.sleep_ms(2000)
                  uart.write('31')
                  uart.write('0')
                  print('31')
                  flag_color = 2
                  break
              if code == '红色绿色':
                  utime.sleep_ms(2000)
                  uart.write('12')
                  uart.write('0')
                  print('12')
                  flag_color = 2
                  break
              if code == '红色蓝色':
                  utime.sleep_ms(2000)
                  uart.write('13')
                  uart.write('0')
                  print('13')
                  flag_color = 2
                  break
      
      
      #标志变量为2时
      while(flag_color == 2):
          clock.tick()
          img = sensor.snapshot()
      
          #在三个框里识别物体
          img.draw_rectangle((18,80,82,102),color=(0xff,0xff,0xff))
          img.draw_rectangle((118,74,100,106),color=(0xff,0xff,0xff))
          img.draw_rectangle((238,70,78,102),color=(0xff,0xff,0xff))
      
          #识别左、中、右三个物块
          blob_left = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (18,80,82,102))
          blob_middle = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (118,74,100,106))
          blob_right = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (238,70,78,102))
      
          left = 0
          middle = 0
          right = 0
      
          #判断左侧物体颜色
          if blob_left:
              for blob in blob_left:
                  x = blob[0]
                  y = blob[1]
                  width = blob[2]
                  height = blob[3]
                  center_x = blob[5]
                  center_y = blob[6]
                  color_code = blob[8]
      
              #添加颜色说明
              if color_code == red_color_code:
                  img.draw_string(x, y,  'red',color = (0xff,0x00,0x00))
                  left = 1
              elif color_code == green_color_code:
                  img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
                  left = 2
              elif color_code == blue_color_code:
                  img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
                  left = 3
              color = color_code
              img.draw_rectangle(x,y,width,height)
              img.draw_cross(center_x,center_y)
      
          #判断中间物体颜色
          if blob_middle:
              for blob in blob_middle:
                  x = blob[0]
                  y = blob[1]
                  width = blob[2]
                  height = blob[3]
                  center_x = blob[5]
                  center_y = blob[6]
                  color_code = blob[8]
      
              #添加颜色说明
              if color_code == red_color_code:
                  img.draw_string(x,y-10,"red",color = (0xff,0x00,0x00))
                  middle = 1
              elif color_code == green_color_code:
                  img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
                  middle = 2
              elif color_code == blue_color_code:
                  img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
                  middle = 3
              color = color_code
              img.draw_rectangle(x,y,width,height)
              img.draw_cross(center_x,center_y)
      
          #判断右侧物体颜色
          if blob_right:
             for blob in blob_right:
                 x = blob[0]
                 y = blob[1]
                 width = blob[2]
                 height = blob[3]
                 center_x = blob[5]
                 center_y = blob[6]
                 color_code = blob[8]
      
          #添加颜色说明
          if color_code == red_color_code:
             img.draw_string(x,y-10,"red",color = (0xff,0x00,0x00))
             right = 1
          elif color_code == green_color_code:
             img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
             right = 2
          elif color_code == blue_color_code:
             img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
             right = 3
          color = color_code
          img.draw_rectangle(x,y,width,height)
          img.draw_cross(center_x,center_y)
      
          if left!=0 and middle != 0 and right != 0 and left != middle and left != right and right != middle:
              #123
              if left == 1 and middle == 2 and right == 3:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('123')
                  utime.sleep_ms(5000)
                  flag_color =3
              #132
              if left == 1 and middle == 3 and right == 2:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('132')
                  utime.sleep_ms(5000)
                  flag_color =3
              #213
              if left == 2 and middle == 1 and right == 3:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('213')
                  utime.sleep_ms(5000)
                  flag_color =3
              #231
              if left == 2 and middle == 3 and right == 1:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('231')
                  utime.sleep_ms(5000)
                  flag_color =3
              #312
              if left == 3 and middle == 1 and right == 2:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('312')
                  utime.sleep_ms(5000)
                  flag_color =3
              #321
              if left == 3 and middle == 2 and right == 1:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('321')
                  utime.sleep_ms(5000)
                  flag_color =3
      
      #当标志变量变为3时:
      while(flag_color == 3):
      
          clock.tick()
          img = sensor.snapshot()
      
          #在三个框里识别物体
          img.draw_rectangle((18,80,82,102),color=(0x00,0x00,0x00))
          img.draw_rectangle((118,74,100,106),color=(0x00,0x00,0x00))
          img.draw_rectangle((238,70,78,102),color=(0x00,0x00,0x00))
      
          #识别左、中、右三个物块
          blob_left = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (18,80,82,102))
          blob_middle = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (118,74,100,106))
          blob_right = img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold = 200,merge = True,roi = (238,70,78,102))
      
          left = 0
          middle = 0
          right = 0
      
          #判断左侧物体颜色
          if blob_left:
              for blob in blob_left:
                  x = blob[0]
                  y = blob[1]
                  width = blob[2]
                  height = blob[3]
                  center_x = blob[5]
                  center_y = blob[6]
                  color_code = blob[8]
      
              #添加颜色说明
              if color_code == red_color_code:
                  img.draw_string(x, y,  'red',color = (0xff,0x00,0x00))
                  left = 1
              elif color_code == green_color_code:
                  img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
                  left = 2
              elif color_code == blue_color_code:
                  img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
                  left = 3
              color = color_code
              img.draw_rectangle(x,y,width,height)
              img.draw_cross(center_x,center_y)
      
          #判断中间物体颜色
          if blob_middle:
              for blob in blob_middle:
                  x = blob[0]
                  y = blob[1]
                  width = blob[2]
                  height = blob[3]
                  center_x = blob[5]
                  center_y = blob[6]
                  color_code = blob[8]
      
              #添加颜色说明
              if color_code == red_color_code:
                  img.draw_string(x,y-10,"red",color = (0xff,0x00,0x00))
                  middle = 1
              elif color_code == green_color_code:
                  img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
                  middle = 2
              elif color_code == blue_color_code:
                  img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
                  middle = 3
              color = color_code
              img.draw_rectangle(x,y,width,height)
              img.draw_cross(center_x,center_y)
      
          #判断右侧物体颜色
          if blob_right:
             for blob in blob_right:
                 x = blob[0]
                 y = blob[1]
                 width = blob[2]
                 height = blob[3]
                 center_x = blob[5]
                 center_y = blob[6]
                 color_code = blob[8]
      
          #添加颜色说明
          if color_code == red_color_code:
             img.draw_string(x,y-10,"red",color = (0xff,0x00,0x00))
             right = 1
          elif color_code == green_color_code:
             img.draw_string(x,y-10,"green",color = (0x00,0xff,0x00))
             right = 2
          elif color_code == blue_color_code:
             img.draw_string(x,y-10,"blue",color = (0x00,0x00,0xff))
             right = 3
          color = color_code
          img.draw_rectangle(x,y,width,height)
          img.draw_cross(center_x,center_y)
      
          if left!=0 and middle != 0 and right != 0 and left != middle and left != right and right != middle:
              #123
              if left == 1 and middle == 2 and right == 3:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('123')
                  flag_color =4
              #132
              if left == 1 and middle == 3 and right == 2:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('132')
                  flag_color =4
              #213
              if left == 2 and middle == 1 and right == 3:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('213')
                  flag_color =4
              #231
              if left == 2 and middle == 3 and right == 1:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('231')
                  flag_color =4
              #312
              if left == 3 and middle == 1 and right == 2:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('312')
                  flag_color =4
              #321
              if left == 3 and middle == 2 and right == 1:
                  print("%d%d%d" % (left,middle,right))
                  uart.write('321')
                  flag_color =4
      #####完成扫描
      while(flag_color == 4):
          green_led.on()
      


    • color_code定义过但是他会报错,
      color_code' isn't defined
      而且摄像头非常的黑,换了一个摄像头就是正常亮度,



    • 0_1648.png
      color code 定义过了但是还是会报错



    • 就是代码有问题,和脱机没关系。

      面条代码太复杂,我不能完全给你重写。

      1,

      color_code定义过但是他会报错,

      原因:事实上你没定义过
      解决办法:在25行加color_code=0

      2,
      你的代码没有死循环,运行到最后就退出了。



    • 我已经按照你说的修改了代码,但是还是在脱机运行的时候会卡死,无法脱机运行。



    • @oxu2 你具体的现象是什么,有什么报错,你要重新发你的代码。

      “卡死”是什么现象,是连不上电脑,还是OpenMV闪灯。



    • 串口有发送数据,但是用串口调试助手看是空的



    • @oxu2 你要重新发你的代码

      而且我说了很多遍了,你的代码没有死循环,运行到最后就退出了。

      你在最后添加

      while True:
          uart.write("finishd\r\n")
          time.sleep_ms(100)
      

      就可以看到他是不是结束了