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



    • 0_1557728506445_111.jpg



    • 请提供全部代码文本,请注意不要贴代码图片



    • @kidswong999

      请在这里粘贴代码
      red_fd = open('red_threshold.txt','r',encoding='utf-8') #读取red_threshold.txt的红色阈值
                          green_fd = open('green_threshold.txt','r',encoding='utf-8') #读取green_threshold.txt的红色阈值
                          blue_fd = open('blue_threshold.txt','r',encoding='utf-8') #读取blue_threshold.txt的红色阈值
                          red_threshold = tuple(eval(red_fd.read()))
                          green_threshold = tuple(eval(green_fd.read()))
                          blue_threshold = tuple(eval(blue_fd.read()))
                          red_fd.close()
                          green_fd.close()
                          blue_fd.close()
                          sensor.reset()
                          sensor.set_pixformat(sensor.RGB565)
                          sensor.set_framesize(sensor.QQVGA)
                          sensor.skip_frames(2000)
                          sensor.set_auto_gain(False)
                          sensor.set_auto_whitebal(False)
                          sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
                          clock = time.clock()
                          global flag_color
                          flag_color = 1
                          global timeout
                          timeout = 1
                          global start
                          while(flag_color == 1):             #先识别红色
                              sensor.skip_frames(time = 2000)
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()  #记录当前识别红色开始运行时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
                                  if flag_color == 1:
                                      uart.write('-1')
                                      global timeout
                                      timeout = 1
                                      global flag_color
                                      flag_color = 2
                                      break
                              img = sensor.snapshot()
                              red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if red_blob:
                                  max_blob = find_max(red_blob)
                                  print(max_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      uart.write("1")
                                      red_led.on()
                                      blue_led.off()
                                      grean_led.off()
                                      global flag_color
                                      flag_color = 2
                                      global timeout
                                      timeout = 1
                                      #time.sleep(500)
                          while(flag_color == 2):             #识别绿色
                              sensor.skip_frames(time = 2000)
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()        #记录当前识别绿色开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                   #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
                                  if flag_color == 2:
                                      uart.write('-2')
                                      global timeout
                                      timeout = 1
                                      global flag_color
                                      flag_color = 3
                                      break
                              green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if green_blob:
                                  max_blob = find_max(green_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      uart.write("2")
                                      red_led.off()
                                      blue_led.off()
                                      grean_led.on()
                                      global flag_color
                                      flag_color = 3
                                      global timeout
                                      timeout = 1
                                      #time.sleep(500)
                          while(flag_color == 3):         #识别蓝色
                              sensor.skip_frames(time = 2000)
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()    #记录当前识别蓝色开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
                                  if flag_color == 3:
                                      uart.write("-3")
                                      global flag_color
                                      flag_color = 4
                                      global timeout
                                      timeout = 1
                                      break
                              blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if blue_blob:
                                  max_blob = find_max(blue_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      uart.write("3")
                                      red_led.off()
                                      blue_led.on()
                                      grean_led.off()
                                      global flag_color
                                      flag_color = 4
                                      global timeout
                                      timeout = 1
                                      #time.sleep(500)
      


    • 你的代码我没办法运行。

      你把那几个阈值写到代码里,不要文件读取。



    • 此回复已被删除!


    • @kidswong999

      # Untitled - By: Administrator - 周一 五月 13 2019
      import sensor, image, time,pyb,utime
      
      from pyb import UART
      
      #初始化串口3
      uart = UART(3,115200)
      uart.init(115200,bits = 8,parity = None,stop = 1)
      
      #初始化RGB
      red_led = pyb.LED(1)
      grean_led = pyb.LED(2)
      blue_led = pyb.LED(3)
      
      
      
      global flag_color       #标志变量
      flag_color = 0
      global switch       #标志变量
      switch = 0
      global timeout
      timeout = 0
      
      global start
      start = 0
      
      red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
      green_color  = (47, 64, -61, -48, 29, 51) # 一般情况下的绿色阈值
      blue_color = (53 , 59 , 7 , 23 , -74 , -61) # 一般情况下的蓝色阈值
      
      red_threshold = (30, 40, 40, 57, 18, 52)
      green_threshold =(33, 47, -47, -28, 10, 36)
      blue_threshold = (55, 59, 10, 22, -70, -65)
      
      # 如果是红色,执行此函数
      def doWithRed(img):
      
          red_led.on()
          grean_led.off()
          blue_led.off()
          uart.write("1")
      
      # 如果是绿色,执行此函数
      def doWithGreen(img):
          grean_led.on()
          red_led.off()
          blue_led.off()
          uart.write("2")
      # 如果是蓝色,执行此函数
      def doWithBlue(img):
          blue_led.on()
          red_led.off()
          grean_led.off()
          uart.write("3")
      
      # 如果是红绿蓝三色,执行此函数
      def RedAndGreenAndBlue(img):
          uart.write("4")
          i = 0
          for i in range(5):
              red_led.toggle()
              blue_led.toggle()
              grean_led.toggle()
              time.sleep(100)
      # 如果是二维码,执行此函数
      def Qr_code(img):
          blue_led.on()
          red_led.on()
          grean_led.on()
          uart.write("5")
      
      #判断单颜色中最大色块
      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
      
      
      def fun():
          while(True):
              if uart.any():
                  uart_buf = uart.readline()
                  if uart_buf == b'0x01':
                      global switch
                      switch = 1
                      if switch == 1:
                          red_fd = open('red_threshold.txt','r',encoding='utf-8') #读取red_threshold.txt的红色阈值
                          green_fd = open('green_threshold.txt','r',encoding='utf-8') #读取green_threshold.txt的红色阈值
                          blue_fd = open('blue_threshold.txt','r',encoding='utf-8') #读取blue_threshold.txt的红色阈值
                          red_threshold = tuple(eval(red_fd.read()))
                          green_threshold = tuple(eval(green_fd.read()))
                          blue_threshold = tuple(eval(blue_fd.read()))
                          red_fd.close()
                          green_fd.close()
                          blue_fd.close()
                          sensor.reset()
                          sensor.set_pixformat(sensor.RGB565)
                          sensor.set_framesize(sensor.QQVGA)
                          sensor.skip_frames(time = 2000)
                          sensor.set_auto_gain(False)
                          sensor.set_auto_whitebal(False)
                          sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
                          sensor.skip_frames(30)
                          clock = time.clock()
                          global flag_color
                          flag_color = 1
                          global timeout
                          timeout = 1
                          global start
                          while(flag_color == 1):             #先识别红色
                              sensor.skip_frames(30)
                              clock = time.clock()
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()  #记录当前识别红色开始运行时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
                                  if flag_color == 1:
                                      blue_led.toggle()
                                      red_led.toggle()
                                      grean_led.toggle()
                                      uart.write('-1')
                                      global timeout
                                      timeout = 1
                                      global flag_color
                                      flag_color = 2
                                      break
                              img = sensor.snapshot()
                              red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if red_blob:
                                  max_blob = find_max(red_blob)
                                  print(max_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      doWithRed(img)
                                      global flag_color
                                      flag_color = 2
                                      global timeout
                                      timeout = 1
                                      time.sleep(500)
                          while(flag_color == 2):             #识别绿色
                              sensor.skip_frames(30)
                              clock = time.clock()
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()        #记录当前识别绿色开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                   #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
                                  if flag_color == 2:
                                      blue_led.toggle()
                                      red_led.toggle()
                                      grean_led.toggle()
                                      uart.write('-2')
                                      global timeout
                                      timeout = 1
                                      global flag_color
                                      flag_color = 3
                                      break
                              green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if green_blob:
                                  max_blob = find_max(green_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      doWithGreen(img)
                                      global flag_color
                                      flag_color = 3
                                      global timeout
                                      timeout = 1
                                      time.sleep(500)
                          while(flag_color == 3):         #识别蓝色
                              sensor.skip_frames(30)
                              clock = time.clock()
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()    #记录当前识别蓝色开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
                                  if flag_color == 3:
                                      blue_led.toggle()
                                      red_led.toggle()
                                      grean_led.toggle()
                                      uart.write("-3")
                                      global flag_color
                                      flag_color = 4
                                      global timeout
                                      timeout = 1
                                      break
                              blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                              if blue_blob:
                                  max_blob = find_max(blue_blob)
                                  if(max_blob.w() > 30 and max_blob.h() > 20):
                                      img.draw_rectangle(max_blob.rect())
                                      img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                                      doWithBlue(img)
                                      global flag_color
                                      flag_color = 4
                                      global timeout
                                      timeout = 1
                                      time.sleep(500)
                          while(flag_color == 4):             #识别红绿蓝
                              sensor.skip_frames(20)
                              clock = time.clock()
                              clock.tick()
                              if timeout == 1:
                                  start = utime.ticks_ms()    #记录当前识别红绿蓝开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 8000:                #如果超过8秒则跳出该循环往下执行第五步,并向串口发送-4
                                  if flag_color == 4:
                                      blue_led.toggle()
                                      red_led.toggle()
                                      grean_led.toggle()
                                      uart.write("-4")
                                      global flag_color
                                      flag_color = 5
                                      global timeout
                                      timeout = 1
                                      global switch
                                      switch = 2
                                      break
                              img = sensor.snapshot()
                              red_blob = img.find_blobs([red_color], pixels_threshold=100, area_threshold=100, merge=True)
                              green_blob = img.find_blobs([green_color], pixels_threshold=100, area_threshold=100, merge=True)
                              blue_blob = img.find_blobs([blue_color], pixels_threshold=100, area_threshold=100, merge=True)
                              if red_blob and green_blob and blue_blob:
                                  r_max_size=g_max_size=b_max_size=0
                                  for r_blob,g_blob,b_blob in zip(red_blob,green_blob,blue_blob):
                                      if r_blob[2]*r_blob[3] > r_max_size and g_blob[2]*g_blob[3] > g_max_size and b_blob[2]*b_blob[3] > b_max_size:
                                          img.draw_rectangle(r_blob.rect())
                                          img.draw_cross(r_blob.cx(), r_blob.cy()) # cx, cy
                                          img.draw_rectangle(g_blob.rect())
                                          img.draw_cross(g_blob.cx(), g_blob.cy()) # cx, cy
                                          img.draw_rectangle(b_blob.rect())
                                          img.draw_cross(b_blob.cx(), b_blob.cy()) # cx, cy
                                          RedAndGreenAndBlue(img)
                                          global flag_color
                                          flag_color = 5
                                          global timeout
                                          timeout = 1
                                          global switch
                                          switch = 2
                                          time.sleep(500)
                      if switch == 2:
                          sensor.reset()
                          sensor.set_pixformat(sensor.RGB565)
                          sensor.set_framesize(sensor.QQVGA)
                          sensor.skip_frames(30) # 修改sensor配置之后, 跳过30帧
                          sensor.set_auto_gain(False)
                          while(flag_color == 5):             #识别二维码
                              if timeout == 1:
                                  start = utime.ticks_ms()    #记录当前二维码开始识别时间
                                  global timeout
                                  timeout = 0
                              delta = utime.ticks_diff(utime.ticks_ms(), start)
                              print(delta)
                              if delta > 10000:           #如果超过10秒则跳出循环,并向串口发送-5
                                  if flag_color == 5:
                                      blue_led.toggle()
                                      red_led.toggle()
                                      grean_led.toggle()
                                      uart.write('-5')
                                      global timeout
                                      timeout = 1
                                      global flag_color
                                      flag_color = 1
                                      global switch
                                      switch = 1
                                      break
                              img = sensor.snapshot()
                              img.lens_corr(1.5)
                              for code in img.find_qrcodes():
                                  print(code)
                                  if code[4] == "hello":
                                      Qr_code(img)
                                      global flag_color
                                      flag_color = 1
                                      global switch
                                      switch = 1
                                      global timeout
                                      timeout = 1
                                      time.sleep(500)
                  if "alter" in uart_buf:             #拾取屏幕中的颜色阈值
                      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)
                      sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
                      sensor.skip_frames(30)
                      clock = time.clock()
      
                      r = [(320//2)-(30//2), (240//2)-(30//2), 30, 30]
      
                      for i in range(60):
                          img = sensor.snapshot()
                          img.draw_rectangle(r)
                      if "red_threshold" in uart_buf:         #拾取红色阈值
                          red_threshold = [30, 30, 0, 0, 0, 0] 
                          for i in range(60):
                              img = sensor.snapshot()
                              hist = img.get_histogram(roi=r)
                              lo = hist.get_percentile(0.01)
                              hi = hist.get_percentile(0.99)
                              red_threshold[0] = (red_threshold[0] + lo.l_value())
                              red_threshold[1] = (red_threshold[1] + hi.l_value())
                              red_threshold[2] = (red_threshold[2] + lo.a_value())
                              red_threshold[3] = (red_threshold[3] + hi.a_value())
                              red_threshold[4] = (red_threshold[4] + lo.b_value())
                              red_threshold[5] = (red_threshold[5] + hi.b_value())
                              for blob in img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
                                  img.draw_rectangle(blob.rect())
                                  img.draw_cross(blob.cx(), blob.cy())
                                  img.draw_rectangle(r)
                          red_fd = open('red_threshold.txt','w+') #识别红色阈值并写入到red_threshold.txt文件里
                          red_fd.write(str(red_threshold))
                          red_fd.close()
                          uart.write(str(red_threshold))
      
                      elif "green_threshold" in uart_buf:     #拾取绿色阈值
                          green_threshold = [30, 30, 0, 0, 0, 0]
                          for i in range(60):
                              img = sensor.snapshot()
                              hist = img.get_histogram(roi=r)
                              lo = hist.get_percentile(0.01)
                              hi = hist.get_percentile(0.99)
                              green_threshold[0] = (green_threshold[0] + lo.l_value())
                              green_threshold[1] = (green_threshold[1] + hi.l_value())
                              green_threshold[2] = (green_threshold[2] + lo.a_value())
                              green_threshold[3] = (green_threshold[3] + hi.a_value())
                              green_threshold[4] = (green_threshold[4] + lo.b_value())
                              green_threshold[5] = (green_threshold[5] + hi.b_value())
                              for blob in img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
                                  img.draw_rectangle(blob.rect())
                                  img.draw_cross(blob.cx(), blob.cy())
                                  img.draw_rectangle(r)
                          green_fd = open('green_threshold.txt','w+') #识别绿色阈值并写入到red_threshold.txt文件里
                          green_fd.write(str(green_threshold))
                          green_fd.close()
                          uart.write(str(green_threshold))
      
                      elif "blue_threshold" in uart_buf:       #拾取蓝色阈值
                          blue_threshold = [30, 30, 0, 0, 0, 0]
                          for i in range(60):
                              img = sensor.snapshot()
                              hist = img.get_histogram(roi=r)
                              lo = hist.get_percentile(0.01)
                              hi = hist.get_percentile(0.99)
                              blue_threshold[0] = (blue_threshold[0] + lo.l_value())
                              blue_threshold[1] = (blue_threshold[1] + hi.l_value())
                              blue_threshold[2] = (blue_threshold[2] + lo.a_value())
                              blue_threshold[3] = (blue_threshold[3] + hi.a_value())
                              blue_threshold[4] = (blue_threshold[4] + lo.b_value())
                              blue_threshold[5] = (blue_threshold[5] + hi.b_value())
                              for blob in img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
                                  img.draw_rectangle(blob.rect())
                                  img.draw_cross(blob.cx(), blob.cy())
                                  img.draw_rectangle(r)
      
                          blue_fd = open('blue_threshold.txt','w+') #识别蓝色阈值并写入到red_threshold.txt文件里
                          blue_fd.write(str(blue_threshold))
                          blue_fd.close()
                          uart.write(str(blue_threshold))
      
      if __name__=='__main__':
          fun()
      
      


    • 你的程序还需要串口输入数据才能运行?



    • 对啊你可以吧他删了也可以



    • 你给我一个可以运行出故障的代码。



    • @kidswong999

      # Untitled - By: Administrator - 周一 五月 13 2019
      import sensor, image, time,pyb,utime
      
      from pyb import UART
      
      #初始化串口3
      uart = UART(3,115200)
      uart.init(115200,bits = 8,parity = None,stop = 1)
      
      #初始化RGB
      red_led = pyb.LED(1)
      grean_led = pyb.LED(2)
      blue_led = pyb.LED(3)
      
      
      
      global flag_color       #标志变量
      flag_color = 0
      global switch       #标志变量
      switch = 0
      global timeout
      timeout = 0
      
      global start
      start = 0
      
      red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
      green_color  = (47, 64, -61, -48, 29, 51) # 一般情况下的绿色阈值
      blue_color = (53 , 59 , 7 , 23 , -74 , -61) # 一般情况下的蓝色阈值
      
      red_threshold = (30, 40, 40, 57, 18, 52)
      green_threshold =(33, 47, -47, -28, 10, 36)
      blue_threshold = (55, 59, 10, 22, -70, -65)
      
      # 如果是红色,执行此函数
      def doWithRed(img):
      
          red_led.on()
          grean_led.off()
          blue_led.off()
          uart.write("1")
      
      # 如果是绿色,执行此函数
      def doWithGreen(img):
          grean_led.on()
          red_led.off()
          blue_led.off()
          uart.write("2")
      # 如果是蓝色,执行此函数
      def doWithBlue(img):
          blue_led.on()
          red_led.off()
          grean_led.off()
          uart.write("3")
      
      # 如果是红绿蓝三色,执行此函数
      def RedAndGreenAndBlue(img):
          uart.write("4")
          i = 0
          for i in range(5):
              red_led.toggle()
              blue_led.toggle()
              grean_led.toggle()
              time.sleep(100)
      # 如果是二维码,执行此函数
      def Qr_code(img):
          blue_led.on()
          red_led.on()
          grean_led.on()
          uart.write("5")
      
      #判断单颜色中最大色块
      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
      
      
      def fun():
          while(True):
              global switch
              switch = 1
              if switch == 1:
                  red_fd = open('red_threshold.txt','r',encoding='utf-8') #读取red_threshold.txt的红色阈值
                  green_fd = open('green_threshold.txt','r',encoding='utf-8') #读取green_threshold.txt的红色阈值
                  blue_fd = open('blue_threshold.txt','r',encoding='utf-8') #读取blue_threshold.txt的红色阈值
                  red_threshold = tuple(eval(red_fd.read()))
                  green_threshold = tuple(eval(green_fd.read()))
                  blue_threshold = tuple(eval(blue_fd.read()))
                  red_fd.close()
                  green_fd.close()
                  blue_fd.close()
                  sensor.reset()
                  sensor.set_pixformat(sensor.RGB565)
                  sensor.set_framesize(sensor.QQVGA)
                  sensor.skip_frames(time = 2000)
                  sensor.set_auto_gain(False)
                  sensor.set_auto_whitebal(False)
                  sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
                  sensor.skip_frames(30)
                  clock = time.clock()
                  global flag_color
                  flag_color = 1
                  global timeout
                  timeout = 1
                  global start
                  while(flag_color == 1):             #先识别红色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()  #记录当前识别红色开始运行时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
                          if flag_color == 1:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-1')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 2
                              break
                      img = sensor.snapshot()
                      red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if red_blob:
                          max_blob = find_max(red_blob)
                          print(max_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithRed(img)
                              global flag_color
                              flag_color = 2
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 2):             #识别绿色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()        #记录当前识别绿色开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                   #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
                          if flag_color == 2:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-2')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 3
                              break
                      green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if green_blob:
                          max_blob = find_max(green_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithGreen(img)
                              global flag_color
                              flag_color = 3
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 3):         #识别蓝色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前识别蓝色开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
                          if flag_color == 3:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write("-3")
                              global flag_color
                              flag_color = 4
                              global timeout
                              timeout = 1
                              break
                      blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if blue_blob:
                          max_blob = find_max(blue_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithBlue(img)
                              global flag_color
                              flag_color = 4
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 4):             #识别红绿蓝
                      sensor.skip_frames(20)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前识别红绿蓝开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第五步,并向串口发送-4
                          if flag_color == 4:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write("-4")
                              global flag_color
                              flag_color = 5
                              global timeout
                              timeout = 1
                              global switch
                              switch = 2
                              break
                      img = sensor.snapshot()
                      red_blob = img.find_blobs([red_color], pixels_threshold=100, area_threshold=100, merge=True)
                      green_blob = img.find_blobs([green_color], pixels_threshold=100, area_threshold=100, merge=True)
                      blue_blob = img.find_blobs([blue_color], pixels_threshold=100, area_threshold=100, merge=True)
                      if red_blob and green_blob and blue_blob:
                          r_max_size=g_max_size=b_max_size=0
                          for r_blob,g_blob,b_blob in zip(red_blob,green_blob,blue_blob):
                              if r_blob[2]*r_blob[3] > r_max_size and g_blob[2]*g_blob[3] > g_max_size and b_blob[2]*b_blob[3] > b_max_size:
                                  img.draw_rectangle(r_blob.rect())
                                  img.draw_cross(r_blob.cx(), r_blob.cy()) # cx, cy
                                  img.draw_rectangle(g_blob.rect())
                                  img.draw_cross(g_blob.cx(), g_blob.cy()) # cx, cy
                                  img.draw_rectangle(b_blob.rect())
                                  img.draw_cross(b_blob.cx(), b_blob.cy()) # cx, cy
                                  RedAndGreenAndBlue(img)
                                  global flag_color
                                  flag_color = 5
                                  global timeout
                                  timeout = 1
                                  global switch
                                  switch = 2
                                  time.sleep(500)
              if switch == 2:
                  sensor.reset()
                  sensor.set_pixformat(sensor.RGB565)
                  sensor.set_framesize(sensor.QQVGA)
                  sensor.skip_frames(30) # 修改sensor配置之后, 跳过30帧
                  sensor.set_auto_gain(False)
                  while(flag_color == 5):             #识别二维码
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前二维码开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 10000:           #如果超过10秒则跳出循环,并向串口发送-5
                          if flag_color == 5:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-5')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 1
                              global switch
                              switch = 1
                              break
                      img = sensor.snapshot()
                      img.lens_corr(1.5)
                      for code in img.find_qrcodes():
                          print(code)
                          if code[4] == "hello":
                              Qr_code(img)
                              global flag_color
                              flag_color = 1
                              global switch
                              switch = 1
                              global timeout
                              timeout = 1
                              time.sleep(500)
              
      
      if __name__=='__main__':
          fun()
      


    • @13542749802 找个没串口了



    • 0_1557733381574_635c5c5a-c76f-4a69-ba4b-b09a74c12699-image.png



    • 请给我一个可以运行出你的故障的代码。我需要复现出你说的现象。



    • @kidswong999

      # Untitled - By: Administrator - 周一 五月 13 2019
      import sensor, image, time,pyb,utime
      
      from pyb import UART
      
      #初始化串口3
      uart = UART(3,115200)
      uart.init(115200,bits = 8,parity = None,stop = 1)
      
      #初始化RGB
      red_led = pyb.LED(1)
      grean_led = pyb.LED(2)
      blue_led = pyb.LED(3)
      
      
      
      global flag_color       #标志变量
      flag_color = 0
      global switch       #标志变量
      switch = 0
      global timeout
      timeout = 0
      
      global start
      start = 0
      
      red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
      green_color  = (47, 64, -61, -48, 29, 51) # 一般情况下的绿色阈值
      blue_color = (53 , 59 , 7 , 23 , -74 , -61) # 一般情况下的蓝色阈值
      
      red_threshold = (30, 40, 40, 57, 18, 52)
      green_threshold =(33, 47, -47, -28, 10, 36)
      blue_threshold = (55, 59, 10, 22, -70, -65)
      
      # 如果是红色,执行此函数
      def doWithRed(img):
      
          red_led.on()
          grean_led.off()
          blue_led.off()
          uart.write("1")
      
      # 如果是绿色,执行此函数
      def doWithGreen(img):
          grean_led.on()
          red_led.off()
          blue_led.off()
          uart.write("2")
      # 如果是蓝色,执行此函数
      def doWithBlue(img):
          blue_led.on()
          red_led.off()
          grean_led.off()
          uart.write("3")
      
      # 如果是红绿蓝三色,执行此函数
      def RedAndGreenAndBlue(img):
          uart.write("4")
          i = 0
          for i in range(5):
              red_led.toggle()
              blue_led.toggle()
              grean_led.toggle()
              time.sleep(100)
      # 如果是二维码,执行此函数
      def Qr_code(img):
          blue_led.on()
          red_led.on()
          grean_led.on()
          uart.write("5")
      
      #判断单颜色中最大色块
      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
      
      
      def fun():
          while(True):
              global switch
              switch = 1
              if switch == 1:
                  sensor.reset()
                  sensor.set_pixformat(sensor.RGB565)
                  sensor.set_framesize(sensor.QQVGA)
                  sensor.skip_frames(time = 2000)
                  sensor.set_auto_gain(False)
                  sensor.set_auto_whitebal(False)
                  sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
                  sensor.skip_frames(30)
                  clock = time.clock()
                  global flag_color
                  flag_color = 1
                  global timeout
                  timeout = 1
                  global start
                  while(flag_color == 1):             #先识别红色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()  #记录当前识别红色开始运行时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
                          if flag_color == 1:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-1')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 2
                              break
                      img = sensor.snapshot()
                      red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if red_blob:
                          max_blob = find_max(red_blob)
                          print(max_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithRed(img)
                              global flag_color
                              flag_color = 2
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 2):             #识别绿色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()        #记录当前识别绿色开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                   #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
                          if flag_color == 2:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-2')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 3
                              break
                      green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if green_blob:
                          max_blob = find_max(green_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithGreen(img)
                              global flag_color
                              flag_color = 3
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 3):         #识别蓝色
                      sensor.skip_frames(30)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前识别蓝色开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
                          if flag_color == 3:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write("-3")
                              global flag_color
                              flag_color = 4
                              global timeout
                              timeout = 1
                              break
                      blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
                      if blue_blob:
                          max_blob = find_max(blue_blob)
                          if(max_blob.w() > 30 and max_blob.h() > 20):
                              img.draw_rectangle(max_blob.rect())
                              img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
                              doWithBlue(img)
                              global flag_color
                              flag_color = 4
                              global timeout
                              timeout = 1
                              time.sleep(500)
                  while(flag_color == 4):             #识别红绿蓝
                      sensor.skip_frames(20)
                      clock = time.clock()
                      clock.tick()
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前识别红绿蓝开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 8000:                #如果超过8秒则跳出该循环往下执行第五步,并向串口发送-4
                          if flag_color == 4:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write("-4")
                              global flag_color
                              flag_color = 5
                              global timeout
                              timeout = 1
                              global switch
                              switch = 2
                              break
                      img = sensor.snapshot()
                      red_blob = img.find_blobs([red_color], pixels_threshold=100, area_threshold=100, merge=True)
                      green_blob = img.find_blobs([green_color], pixels_threshold=100, area_threshold=100, merge=True)
                      blue_blob = img.find_blobs([blue_color], pixels_threshold=100, area_threshold=100, merge=True)
                      if red_blob and green_blob and blue_blob:
                          r_max_size=g_max_size=b_max_size=0
                          for r_blob,g_blob,b_blob in zip(red_blob,green_blob,blue_blob):
                              if r_blob[2]*r_blob[3] > r_max_size and g_blob[2]*g_blob[3] > g_max_size and b_blob[2]*b_blob[3] > b_max_size:
                                  img.draw_rectangle(r_blob.rect())
                                  img.draw_cross(r_blob.cx(), r_blob.cy()) # cx, cy
                                  img.draw_rectangle(g_blob.rect())
                                  img.draw_cross(g_blob.cx(), g_blob.cy()) # cx, cy
                                  img.draw_rectangle(b_blob.rect())
                                  img.draw_cross(b_blob.cx(), b_blob.cy()) # cx, cy
                                  RedAndGreenAndBlue(img)
                                  global flag_color
                                  flag_color = 5
                                  global timeout
                                  timeout = 1
                                  global switch
                                  switch = 2
                                  time.sleep(500)
              if switch == 2:
                  sensor.reset()
                  sensor.set_pixformat(sensor.RGB565)
                  sensor.set_framesize(sensor.QQVGA)
                  sensor.skip_frames(30) # 修改sensor配置之后, 跳过30帧
                  sensor.set_auto_gain(False)
                  while(flag_color == 5):             #识别二维码
                      if timeout == 1:
                          start = utime.ticks_ms()    #记录当前二维码开始识别时间
                          global timeout
                          timeout = 0
                      delta = utime.ticks_diff(utime.ticks_ms(), start)
                      print(delta)
                      if delta > 10000:           #如果超过10秒则跳出循环,并向串口发送-5
                          if flag_color == 5:
                              blue_led.toggle()
                              red_led.toggle()
                              grean_led.toggle()
                              uart.write('-5')
                              global timeout
                              timeout = 1
                              global flag_color
                              flag_color = 1
                              global switch
                              switch = 1
                              break
                      img = sensor.snapshot()
                      img.lens_corr(1.5)
                      for code in img.find_qrcodes():
                          print(code)
                          if code[4] == "hello":
                              Qr_code(img)
                              global flag_color
                              flag_color = 1
                              global switch
                              switch = 1
                              global timeout
                              timeout = 1
                              time.sleep(500)
              
      
      if __name__=='__main__':
          fun()
      


    • 我需要怎么操作才能出现“我第一次识别红色成功后第二次再识别屏幕的色彩变绿了好多”的现象?



    • sensor初始化应该放在一开始,而不应该循环重新设置。

                  sensor.reset()
                  sensor.set_pixformat(sensor.RGB565)
                  sensor.set_framesize(sensor.QQVGA)
                  sensor.skip_frames(time = 2000)
                  sensor.set_auto_gain(False)
                  sensor.set_auto_whitebal(False)
                  sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
      


    • @kidswong999 就放在fun这函数外面是吗



    • 放在最开始,也就是import下面



    • @kidswong999 但是在while想改对于的参数呢



    • 改参数和sensor初始化有什么关系?