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



    • 0_1625917655912_QQ截图20210710194619.png
      想再限定一下矩形框宽度高度,为什么这样报错呢



    • 如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片



    • import sensor, image, time, math
      import os, nncu
      import pyb
      
      from machine import UART
      
      
      uart = UART(1, baudrate=115200)     # 初始化串口 波特率设置为115200 TX是B12 RX是B13
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QQVGA)
      sensor.set_brightness(2000) # 设置图像亮度 越大越亮
      sensor.skip_frames(time = 200)
      sensor.set_auto_gain(False)
      sensor.set_auto_whitebal(False)
      clock = time.clock()
      
      
      flag_tag = 0
      flag_OK = 0
      flag_num = 0
      flag_ani = 0
      flag_shoot  = 0
      animal_fruit = 0
      center_x = 0
      center_y = 0
      num = 0
      cnt = 0
      get = b'\x03'
      net_path = "model1.nncu"                          # 定义模型的路径
      labels = [line.rstrip() for line in open("/sd/labels_number.txt")]  # 加载标签
      net = nncu.load(net_path, load_to_fb=True)                          # 加载模型
      
      net_path2 = "78animal_fruit.nncu"    # _qm_model_18_0.7524_4033                             # 定义模型的路径
      labels2 = [line.rstrip() for line in open("/sd/labels_animal_fruits.txt")]   # 加载标签
      net2 = nncu.load(net_path2, load_to_fb=True)
      
      
      def family_name(tag):
          if(tag.family() == image.TAG16H5):
              return "TAG16H5"
          if(tag.family() == image.TAG25H7):
              return "TAG25H7"
          if(tag.family() == image.TAG25H9):
              return "TAG25H9"
          if(tag.family() == image.TAG36H10):
              return "TAG36H10"
          if(tag.family() == image.TAG36H11):
              return "TAG36H11"
          if(tag.family() == image.ARTOOLKIT):
              return "ARTOOLKIT"
      
      def find_max_ani(rects):
          max_size = 0
          min_w = 20
          max_w = 40
          min_h = 20
          max_h = 40
          abs_x = 5   
          for rect in rects:
              if( rect.h() > 5 ): #and rect.w() > min_w and  rect.w() < max_w and rect.h() > min_h and rect.h() < max_h and abs(rect.w()-rect.h()) < abs_x ):
                  max_rect = rect
                  max_size = rect.magnitude()
          return max_rect
       
      def find_max_num(rects):
          max_size = 0
          min_w = 0
          max_w = 0
          min_h = 0
          max_h = 0
          abs_x = 0  
          for rect in rects:
              if (rect.magnitude() > max_size and rect.w() > min_w  and  rect.w() < max_w and rect.h() > min_h and rect.h() < max_h and abs(rect.w()-rect.h()) < abs_x ):
                  max_rect = rect
                  max_size = rect.magnitude()
          return max_rect
          
      
      while(True):
      
          clock.tick()
          img = sensor.snapshot()
          #get = uart.read(1)
          img.draw_rectangle(((15,20,126,70)), color = (0, 0, 255))
      
          if(get==b'\x01'):
              rects = img.find_rects(roi=(10,30,140,80),threshold = 20000) #roi=(30,50,100,)
              for tag in img.find_apriltags(families=image.TAG25H9): # 指定apriltag类型为TAG25H9
                img.draw_rectangle(tag.rect(), color = (255, 0, 0))# 找到apriltag后绘制矩形框,将apriltag框起来
                img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))# 在apriltag中间绘制十字
                print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
                # 打印apriltag的类型、ID、旋转角度
                print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
                flag_tag = 1
      
          elif(get==b'\x02'):
              rects = img.find_rects(roi=(35,30,100,60),threshold = 2000)  #roi=(20,30,130,80)  threshold = 5000
              if(rects):
                  max_rect = find_max_num(rects)
                  if( max_rect.w() >10 and max_rect.w() < 50  and  max_rect.h() < 73 and ((max_rect.w()-max_rect.h() < 6 and max_rect.w()-max_rect.h() > 0) or (max_rect.w()-max_rect.h() > -6 and max_rect.w()-max_rect.h() < 0))     ):
                      img.draw_rectangle(max_rect.rect(), color = (255, 0, 0))   # 绘制矩形外框,便于在IDE上查看识别到的矩形位置
                      img1 = img.copy(max_rect.rect())                           # 拷贝矩形框内的图像
                      for obj in nncu.classify(net , img1, min_scale=1.0, scale_mul=0.5, x_overlap=0.0, y_overlap=0.0):
                        #print("**********\nTop 1 Detections at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
                        sorted_list = sorted(zip(labels, obj.output()), key = lambda x: x[1], reverse = True)
                        # 打印准确率最高的结果
                        for i in range(1):
                            #print("%s = %f" % (sorted_list[i][0], sorted_list[i][1]))
                            flag_num = 1
                            num = sorted_list[i][0]
      
          elif(get == b'\x03'):
              rects = img.find_rects(roi=(15,20,126,70),threshold = 2000)
              if(rects):
                  max_rect = find_max_ani(rects)
                  img.draw_rectangle(max_rect.rect(), color = (255, 0, 0))   # 绘制矩形外框,便于在IDE上查看识别到的矩形位置
                  img2 = img.copy(max_rect.rect())                           # 拷贝矩形框内的图像
                  for obj in nncu.classify(net2 , img2, min_scale=1.0, scale_mul=0.5, x_overlap=0.0, y_overlap=0.0):
                      #print("**********\nTop 1 Detections at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
                      sorted_list = sorted(zip(labels2, obj.output()), key = lambda x: x[1], reverse = True)
                      # 打印准确率最高的结果
                      for i in range(1):
                          #print("%s = %f" % (sorted_list[i][0], sorted_list[i][1]))
                          flag_ani = 1
                          animal_fruit = sorted_list[i][0]
          elif(get == b'\x04'):
               flag_shoot = 0
               flag_OK = 0
          elif(get == b'\x05'):
              rects = img.find_rects(roi=(15,30,126,70),threshold = 2000)
              if(rects):
                  max_rect = find_max(rects)
                  if(max_rect.w() >25 and ((max_rect.w()-max_rect.h() < 5 and max_rect.w()-max_rect.h() > 0) or (max_rect.w()-max_rect.h() > -5 and max_rect.w()-max_rect.h() < 0)) and max_rect.h() > 20 and max_rect.h() < 40):
                      img.draw_rectangle(max_rect.rect(), color = (255, 0, 0))   # 绘制矩形外框,便于在IDE上查看识别到的矩形位置
                      flag_ani = 1
                      animal_fruit = 'apple'
      
          if(flag_tag == 1 and (tag.id() ==0 or tag.id() ==2 or tag.id() ==4 or tag.id() ==6 or tag.id() ==8)):
                uart.write("M")
                uart.writechar(2)
          elif(flag_tag == 1 and (tag.id() == 1 or tag.id() == 3 or tag.id() == 5 or tag.id() == 7 or tag.id() == 9)):
                uart.write("M")
                uart.writechar(1)
          elif(flag_num == 1 and (num == '1' or num == '3' or num == '5' or num == '7' or num == '9')):
                center_x = max_rect.x() + max_rect.w()/2
                center_y = max_rect.y() + max_rect.h()/2
                uart.write("M")
                uart.writechar(3)
                uart.write("W")
                uart.writechar((int)(center_x))
                uart.write("H")
                uart.writechar((int)(center_x))
                print(num)
          elif(flag_num == 1 and (num == '2' or num == '4' or num == '6' or num == '8' or num == '0')):
                uart.write("M")
                uart.writechar(4)
                print(num)
          elif(flag_ani == 1 and (animal_fruit == 'cat' or animal_fruit == 'dog' or animal_fruit == 'horse' or animal_fruit == 'pig' or animal_fruit == 'castle')):
                uart.write("M")
                uart.writechar(5)
                print("是个动物")
          elif(flag_ani == 1 and flag_OK == 0 and (animal_fruit == 'apple' or animal_fruit == 'banana' or animal_fruit == 'orange' or animal_fruit == 'durian' or animal_fruit == 'grape')):
                uart.write("M")
                uart.writechar(6)
                print("是个水果")
                flag_shoot = 1
          else :
                if(flag_OK == 0):
                    print("没找到了")
                    uart.write("M")
                    uart.writechar(0)
      
          if(flag_shoot == 1):
              center_x = max_rect.x() + max_rect.w()/2
              center_y = max_rect.y() + max_rect.h()/2
              uart.write("X")
              uart.writechar((int)(center_x))
              #uart.write("Y")
              #uart.writechar((int)(center_y))
      
              #print("正在打靶子")
              #print(center_x)
              #print(center_y)
      
              if((center_x - 80 < 5 and center_x -80>0) or  (center_x - 80 > -5 and center_x - 80 <0)):
                  flag_OK = 1
                  #print("打靶完成")
                  #uart.write("M")
                  #uart.writechar(7)
      
      
      
          flag_tag = 0
          flag_num = 0
          flag_ani = 0
      
          num = 0
          #print( "横坐标")
          #print(center_x)
          #print("纵坐标")
      
          #print(clock.fps())
      


    • 我没法运行啊,你这个nncu是啥。



    • 其实就是在定义找最大的矩形框函数里面加一个对框宽度和其他条件的限定,一加就报错



    • 你把nncu的代码发上来,或者提供一个不需要nncu的代码。

      没有代码我没办法调试。



    • @kidswong999

      import sensor, image, time, math
      import os, nncu
      import pyb
      
      from machine import UART
      
      
      uart = UART(1, baudrate=115200)     # 初始化串口 波特率设置为115200 TX是B12 RX是B13
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QQVGA)
      sensor.set_brightness(2000) # 设置图像亮度 越大越亮
      sensor.skip_frames(time = 200)
      sensor.set_auto_gain(False)
      sensor.set_auto_whitebal(False)
      clock = time.clock()
      
      m = 0
      flag_tag = 0
      flag_OK = 0
      flag_num = 0
      flag_ani = 0
      flag_shoot  = 0
      animal_fruit = 0
      center_x = 0
      center_y = 0
      num = 0
      cnt = 0
      get = b'\x05'
      flag_5 = 0
      
      
      def find_max(rects):
          max_size = 0
          min_w = 20
          max_w = 40
          min_h = 20
          max_h = 40
          abs_x = 5   
          for rect in rects:
              if( rect.h() > 5 and rect.w() > min_w): # and  rect.w() < max_w and rect.h() > min_h and rect.h() < max_h and abs(rect.w()-rect.h()) < abs_x ):
                  max_rect = rect
                  max_size = rect.magnitude()
          return max_rect
      
      
      def family_name(tag):
          if(tag.family() == image.TAG16H5):
              return "TAG16H5"
          if(tag.family() == image.TAG25H7):
              return "TAG25H7"
          if(tag.family() == image.TAG25H9):
              return "TAG25H9"
          if(tag.family() == image.TAG36H10):
              return "TAG36H10"
          if(tag.family() == image.TAG36H11):
              return "TAG36H11"
          if(tag.family() == image.ARTOOLKIT):
              return "ARTOOLKIT"
      
      
      while(True):
          clock.tick()
          img = sensor.snapshot()
          #get = uart.read(1)
          img.draw_rectangle(((15,20,126,70)), color = (0, 0, 255))
      
          if(get==b'\x01'):
              rects = img.find_rects(roi=(10,30,140,80),threshold = 20000) #roi=(30,50,100,)
              for tag in img.find_apriltags(families=image.TAG25H9): # 指定apriltag类型为TAG25H9
                #img.draw_rectangle(tag.rect(), color = (255, 0, 0))# 找到apriltag后绘制矩形框,将apriltag框起来
                #img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))# 在apriltag中间绘制十字
                #print_args = (family_name(tag), tag.id(), (180 * tag.rotation()) / math.pi)
                # 打印apriltag的类型、ID、旋转角度
                #print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
                flag_tag = 1
      
          elif(get == b'\x04'):
               flag_shoot = 0
               flag_OK = 0
          elif(get == b'\x05'):
              rects = img.find_rects(roi=(15,30,126,70),threshold = 2000)
              if(rects):
                  max_rect = find_max(rects)
                  if(max_rect.w() >25 and ((max_rect.w()-max_rect.h() < 5 and max_rect.w()-max_rect.h() > 0) or (max_rect.w()-max_rect.h() > -5 and max_rect.w()-max_rect.h() < 0)) and max_rect.h() > 20 and max_rect.h() < 40):
                      img.draw_rectangle(max_rect.rect(), color = (255, 0, 0))   # 绘制矩形外框,便于在IDE上查看识别到的矩形位置
                      flag_ani = 1
                      animal_fruit = 'apple'
                      
      
          if(flag_tag == 1 and (tag.id() ==0 or tag.id() ==2 or tag.id() ==4 or tag.id() ==6 or tag.id() ==8)):
                uart.write("M")
                uart.writechar(2)
          elif(flag_tag == 1 and (tag.id() == 1 or tag.id() == 3 or tag.id() == 5 or tag.id() == 7 or tag.id() == 9)):
                uart.write("M")
                uart.writechar(1)
          elif(flag_num == 1 and (num == '1' or num == '3' or num == '5' or num == '7' or num == '9')):
                uart.write("M")
                uart.writechar(3)
                #print(num)
          elif(flag_num == 1 and (num == '2' or num == '4' or num == '6' or num == '8' or num == '0')):
                uart.write("M")
                uart.writechar(4)
                #print(num)
          elif(flag_ani == 1 and (animal_fruit == 'cat' or animal_fruit == 'dog' or animal_fruit == 'horse' or animal_fruit == 'pig' or animal_fruit == 'castle')):
                uart.write("M")
                uart.writechar(5)
                #print("是个动物")
          elif(flag_ani == 1  and (animal_fruit == 'apple' or animal_fruit == 'banana' or animal_fruit == 'orange' or animal_fruit == 'durian' or animal_fruit == 'grape')):
                if(flag_5 == 0):
                    uart.write("M")
                    uart.writechar(6)
               # print("是个水果")
                flag_shoot = 1
          else :
                if(flag_OK == 0):
                    #print("没找到了")
                    uart.write("M")
                    uart.writechar(0)
      
          if(flag_shoot == 1):
              uart.write("X")
              uart.writechar((int)(center_x))
              #uart.write("Y")
              #uart.writechar((int)(center_y))
      
              #print("正在打靶子")
              #print(center_x)
              #print(center_y)
      
              if((center_x - 80 < 5 and center_x -80>0) or  (center_x - 80 > -5 and center_x - 80 <0)):
                  flag_OK = 1
                  #print("打靶完成")
                  #uart.write("M")
                  #uart.writechar(7)
      
      
          m = 0
          flag_tag = 0
          flag_num = 0
          flag_ani = 0
          flag_5 = 0
          num = 0
          #print( "横坐标")
          #print(center_x)
          #print("纵坐标")
      
          #print(clock.fps())
      


    • 第45行不对。当rects不为空列表,但是没有满足42行的条件时,会导致max_rect没有定义,所以就错了。

      解决办法:41行,添加max_rect = None。
      但是又会引起81行的错误,所以81行要加一句: if max_rect

      总结,你的代码逻辑是有问题的。