星瞳实验室APP,快速收到回复
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 问题:TypeError:unsupported types for _and_:"float,int'该怎么解决?



    • 我在写程序时遇到问题TypeError:unsupported types for and:"float,int'该怎么解决?
      问题截图:
      0_1564407933123_59b55d1c-5804-43e8-80de-d26b8dfca830-image.png

      程序如下:

      
      import sensor, image, time, pyb
      import math
      
      from pyb import UART
      from pyb import LED
      
      Led1 = pyb.LED(3) # 蓝灯
                        # Ps:pyb.LED(1):Red; pyb.LED(1):Green; pyb.LED(4):IR
      
      enable_lens_corr = False # 使能畸变校正标志
                               # True: 允许畸变校正; False: 禁止畸变校正
      
      # 指示灯
      def LED_flicker():
          Led1.on()
          time.sleep(150)
          Led1.off()
          time.sleep(150)
      
      # 处理数据
      def ExceptionVar(var):
          data = []
          data.append(0)
          data.append(0)
      
          if var == -1:
              data[0] = 0
              data[1] = 0
          else:
              data[0] = var & 0xFF
              data[1] = var >> 8
          return data
      
      # 识别圆 -- 返回圆心坐标及半径(3)
      def Find_Circular(img):
          Formtype = 0xFF
          X = 0
          Y = 0
          R = 0
      
          enable_lens_corr = True
      
          if enable_lens_corr: img.lens_corr(1.8) # 适用于2.8mm镜头...
      
          for c in img.find_circles(threshold = 4000, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
              img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
      #        print(c.x())
              Circle_buffer = "[%d,%d,%d]"%(c.x(), c.y(), c.r())
      
              print(Circle_buffer)
              LED_flicker()
      
              X = c.x()
              Y = c.y()
              R = c.r()
              Formtype = 0
      
          return Formtype,X,Y,R
      
      # 识别矩形 -- 返回矩形中心点坐标(2)
      def Find_Rectangle(img):
          Formtype = 0xFF
          X = 0
          Y = 0
          W = 0
          H = 0
      
          for rects in img.find_rects():#roi=(0,0,80,120),threshold = 10000
              img.draw_rectangle(rects.rect(), color = (255, 0, 0))
              for p in rects.corners():
                  img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
      
              print(rects)
      
              Formtype = 1
              X = rects.x() + rects.w()/2
              Y = rects.y() + rects.h()/2
      #        W = rects.w()
              H = rects.h()
              rectBuffer = "[%d,%d]"%(X,Y)
              print(rectBuffer)
      
          return Formtype,X,Y,W
      
      # 识别直线 -- 返回端点坐标及角度angle(3)
      def Find_Lines(img):
          Formtype = 0xFF
          X = 0
          Y = 0
          Angle = 0
      
          min_degree = 0
          max_degree = 179
      
          if enable_lens_corr: img.lens_corr(1.8) # 适用于2.8mm镜头...
      
          for lines in img.find_lines(threshold = 1000, theta_margin = 25, rho_margin = 25):
              if (min_degree <= lines.theta()) and (lines.theta() <= max_degree):
                  img.draw_line(lines.line(), color = (255, 0, 0))
      
                  Formtype = 2
                  X = lines.x1()
                  Y = lines.y1()
                  Angle = lines.theta()
      
          return Formtype,X,Y,Angle
      
      # 识别线段 -- 返回端点坐标及角度angle(3)
      def Find_LineSegment(img):
          Formtype = 0xFF
          X = 0
          Y = 0
          Angle = 0
      
          if enable_lens_corr: img.lens_corr(1.8) # for 2.8mm lens...
      
          for linesegment in img.find_line_segments(merge_distance = 0, max_theta_diff = 5):
              img.draw_line(linesegment.line(), color = (255, 0, 0))
      
              print(linesegment)
      
              Formtype = 3
              X = linesegment.x1()
              Y = linesegment.y1()
              Angle = linesegment.theta()
      
          return Formtype,X,Y,Angle
      
      # 识别三角形
      def Find_Triangle(img):
      
          pass
      
      # 识别形状(圆,矩形,直线,线段,三角形(线段角度))
      def Find_Formtype(img):
      
          pass
      
      Frame_Cnt = 0
      fCnt_tmp = [0,0]
      
      # FormType : 所识别的形状 Ps: 0: Cricle      -> 圆心坐标 -> 半径
      #                           1: Rectangle   -> 中心坐标 ->
      #                           2: Lines       -> 端点坐标(靠近(0,0)) -> 角度
      #                           3: LineSegment -> 端点坐标 -> 角度angle
      # FormType_X,FormType_Y : 坐标
      # FormType_Other : 其他
      def UART_Send(FormType, FormType_X, FormType_Y, FormType_Other):
          global Frame_Cnt
          global fCnt_tmp
          Frame_Head = [170,170]
          Frame_End = [85,85]
          fFormType_tmp = [FormType]
          Frame_Cnt += 1
      
          if Frame_Cnt > 65534 :
              FrameCnt = 0
      
          fHead = bytes(Frame_Head)
      
          fCnt_tmp[0] = Frame_Cnt & 0xFF
          fCnt_tmp[1] = Frame_Cnt >> 8
          fCnt = bytes(fCnt_tmp)
      
          fFormType = bytes(fFormType_tmp)
      
          fFormType_X = bytes(ExceptionVar(FormType_X))
          fFormType_Y = bytes(ExceptionVar(FormType_Y))
          fFormType_Other = bytes(ExceptionVar(FormType_Other))
      
          fEnd = bytes(Frame_End)
      
          FrameBuffe = fHead + fCnt + fFormType + fFormType_X + fFormType_Y + fFormType_Other + fEnd
      
          return FrameBuffe
      
      
      sensor.reset()
      
      sensor.set_pixformat(sensor.RGB565) # grayscale is faster
      
      sensor.set_framesize(sensor.QQVGA)
      
      sensor.skip_frames(time = 2000)
      
      clock = time.clock()
      uart = UART(3, 115200)                         # i使用给定波特率初始化
      # uart.init(115200, bits=8, parity=None, stop=1, timeout_char=1000) # 使用给定参数初始化
      
      while(True):
      
          clock.tick()
      
          #lens_corr(1.8)畸变矫正
          img = sensor.snapshot()
      
      #    (FormType,X,Y,R) = Find_Circular(img)
          (FormType,X,Y,W) = Find_Rectangle(img)
      #    (FormType,X,Y,Angle) = Find_Lines(img)
      #    (FormType,X,Y,Angle) = Find_LineSegment(img)
      
          if(FormType == 0):
              print(FormType,X,Y,R)
              uart.write(UART_Send(FormType,X,Y,R))
          elif(FormType == 1):
              print(FormType,X,Y,W)
              uart.write(UART_Send(FormType,X,Y,W))
          elif(FormType == 2):
              print(FormType,X,Y,Angle)
              uart.write(UART_Send(FormType,X,Y,Angle))
          elif(FormType == 3):
              print(FormType,X,Y,Angle)
              uart.write(UART_Send(FormType,X,Y,Angle))
      
      
          print("FPS %f" % clock.fps())
      
      
      


    • 看上去你的var是一个浮点数,不是整数。



    • This post is deleted!


    • @kidswong999 应该怎么解决?把浮点型改为整型?



    • @kidswong999 刚刚我测了一下,var是int型