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



    • import sensor, image, time,pyb,lcd
      led_R = pyb.LED(1)
      led_G = pyb.LED(2)
      led_B = pyb.LED(3)
      led_R.off()
      led_G.off()
      led_B.off()
      lcd.init()
      from pyb import Pin
      pin4 = Pin('P4', Pin.IN, Pin.PULL_DOWN)
      pin5 = Pin('P5', Pin.OUT, Pin.PULL_NONE)
      pin6 = Pin('P6', Pin.OUT, Pin.PULL_NONE)
      pin9 = Pin('P9', Pin.OUT, Pin.PULL_NONE)
      roi01 = (116,69,180,231)
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.VGA)
      sensor.set_windowing((0,0,500,400))
      sensor.skip_frames(time = 1000)
      sensor.set_auto_gain(False)
      sensor.set_auto_whitebal(False)
      while(True):
          img=sensor.snapshot()
          value4=pin4.value()
          if value4==1:
           import check
          else:
              img.draw_string(116,130," NO",color=(0,0,0),scale=2,mono_space=False)
              img.draw_string(116,150," NU",color=(0,0,0),scale=2,mono_space=False)
              img.draw_string(116,170," SKID",color=(0,0,0),scale=2,mono_space=False)
              lcd.display(img,roi=roi01)
              print('no skid')
              pin5.value(0)
              pin9.value(0)
              pin6.value(0)
              led_R.off()
              led_G.off()
              led_B.off()
      

      第一次import check是OK的
      循环到第二次再次调用的时候,check.py是一个运行完成的程序,无法执行检测操作。
      求教,如何让check.py程序循环执行,使上边的程序可以重复调用。我想达到的目的是pin4给信号后进行检测,检测NG后再次进行检测,循环三次完成后判断OK 还是 NG。 如果check程序增加While循环的话就会一直进行检测,我只需要重复3次就好。
      以下为check.py程序:

      import sensor, image, time,pyb,lcd
      thresholds = [(16, 38, -12, 15, -20, 12),(14, 49, -6, 13, -17, 19)]
      led_R = pyb.LED(1)
      led_G = pyb.LED(2)
      led_B = pyb.LED(3)
      led_R.off()
      led_G.off()
      led_B.off()
      lcd.init()
      from pyb import Pin
      pin4 = Pin('P4', Pin.IN, Pin.PULL_DOWN)
      pin5 = Pin('P5', Pin.OUT, Pin.PULL_NONE)
      pin6 = Pin('P6', Pin.OUT, Pin.PULL_NONE)
      pin9 = Pin('P9', Pin.OUT, Pin.PULL_NONE)
      Count_I = 0
      Count_ok = 0
      Count_ng = 0
      Count_num = 0
      roi01 = (116,69,180,231)
      for i in range(3):
         sensor.reset()
         sensor.set_pixformat(sensor.RGB565)
         sensor.set_framesize(sensor.VGA)
         sensor.set_windowing((0,0,500,400))
         sensor.skip_frames(time = 1000)
         sensor.set_auto_gain(False)
         sensor.set_auto_whitebal(False)
         img=sensor.snapshot()
         lcd.display(img,roi=roi01)
         pin6.value(1)
         Count_num += 1
         Count_I += 1
         i += 1
         if Count_ok >= 9:
          break
         for i in range(15):
             i += 1
             img=sensor.snapshot()
             lcd.display(img,roi=roi01)
             for blob in img.find_blobs(thresholds,roi=(165,175,175,60),pixels_threshold=50,area_threshold=100, merge=True,margin=500):
                 if blob.area()>5500 and blob.area()<11001:
                     img.draw_rectangle(blob.rect(),color=(255,0,0))
                     img.draw_cross(blob.cx(),blob.cy())
                     img.draw_string(116,145,"OK",color=(255,255,255),scale=3,mono_space=False)
                     lcd.display(img,roi=roi01)
                     area=blob.area()
                     print('OK')
                     print(area)
                     Count_ok += 1
                 else:
                     img.draw_rectangle(blob.rect(),color=(255,0,0))
                     img.draw_cross(blob.cx(),blob.cy())
                     img.draw_string(116,145,"NG",color=(255,255,255),scale=3,mono_space=False)
                     lcd.display(img,roi=roi01)
                     area=blob.area()
                     print('NG')
                     print(area)
                     Count_ng += 1
      if Count_ok > 8:
         led_G.on()
         print('total OK')
         pin6.value(0)
         pin5.value(1)
         img.draw_string(116,145," T OK",color=(0,0,255),scale=4,mono_space=False)
         lcd.display(img,roi=roi01)
      else:
         led_R.on()
         print('total ng')
         pin6.value(0)
         pin9.value(1)
         img.draw_string(116,145," T NG",color=(0,0,255),scale=4,mono_space=False)
         lcd.display(img,roi=roi01)
      


    • 或者如何import完成后取消import



    • 试过del了,但是会报Key Error



    • 能否reload啊!!!??!!



    • 所有的py文件叫做模块,调用的是函数,而不是模块。

      你应该在check.py里定义函数,然后在main.py里调用函数。



    • 感谢!!问题已解决!