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



    • 主要是监测图形输出的,这边是我的代码

      import sensor,time,pyb,math,time,utime,image
      from pyb import Pin, Timer, UART
      clock = time.clock() # Tracks FPS.
      LINE_WIDTH = 8
      THRESHOLD = 40
      #灞忓箷灏哄
      WIDTH = 160
      HEIGHT = 120
      ROI = (int((WIDTH-HEIGHT)/2),0,HEIGHT,HEIGHT)
      XO = int((WIDTH-HEIGHT)/2)
      CENTRAL_AREA = 20
      
      #楂樺害鏁版嵁
      highcnt = 0.0
      high = 0
      #xy骞抽潰璇樊鏁版嵁
      err_x = 0
      err_y = 0
      #瓒呭0娉㈠洖璋冨鐞嗗嚱鏁?
      def Ultrasound(line):
         if(Echo.value()==1):
              tim_count.init(prescaler=1799, period=1176)#鎵撳紑瀹氭椂鍣?
         if(Echo.value()==0):
              global highcnt
              tim_count.deinit()
              highcnt = tim_count.counter()#璁℃暟
              uart.writechar(0x55)
              uart.writechar(0xAA)
              uart.writechar(0x10)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(high>>8)
              uart.writechar(high)
              uart.writechar(err_x>>8)
              uart.writechar(err_x)
              uart.writechar(err_y>>8)
              uart.writechar(err_y)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0xAA)
              print(high)
      
      timpwm = Timer(4, freq=60) #瓒呭0娉?0璧吂鍙戝皠棰戠巼
      ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us鍙戝皠瑙?
      #瓒呭0娉㈡帴鏀剁鍙i厤缃?
      tim_count = pyb.Timer(1) #瀹氭椂鍣ㄨ鏁?
      extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#寮€鍚閮ㄤ腑鏂?
      Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
      #涓插彛涓夐厤缃?
      uart = UART(3, 115200)#115200
      uart.init(115200, bits=8, parity=None, stop=1)
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)#璁剧疆鐏板害淇℃伅
      sensor.set_framesize(sensor.QQVGA)#璁剧疆鍥惧儚澶у皬
      #sensor.skip_frames(20)#鐩告満鑷鍑犲紶鍥剧墖
      sensor.set_auto_whitebal(False)#鍏抽棴鐧藉钩琛?
      
      while(True):
          high = int(1.7*highcnt)
          line_x1 = [0,0,0,0]
          line_x2 = [0,0,0,0]
          line_y1 = [0,0,0,0]
          line_y2 = [0,0,0,0]
          line_H = [0,0,0,0]
          line_V = [0,0,0,0]
          line_temp = [0,0,0,0]
          line_num = 0
          Flag = None
          weight = 0
          clock.tick() # Track elapsed milliseconds between snapshots().
          img = sensor.snapshot() # Take a picture and return the image.
          #print(img.height())
          
          for i in range(int(HEIGHT/3)):
              for j in range(int(HEIGHT/3)):
                  if img.get_pixel(i*3+XO,j*3) < 127 :
                      weight += 1
                  #print(img.get_pixel(i,j))
          #print(weight)
          
          img.find_edges(image.EDGE_CANNY, threshold=(50, 80))  # Find edges
          lines = img.find_lines(roi = ROI,threshold = THRESHOLD) # Find lines.
      #    lines = img.find_lines(threshold = THRESHOLD)
          img.draw_rectangle(ROI)
          #for l in lines: img.draw_line(l, color=(127)) # Draw lines
          #print(lines) 
          if len(lines) != 0:
              #print(len(lines),lines)
              for i in range(len(lines)):
                  #print(i)
                  if lines[i][0] == 0:                    
                      if line_x1 == [0,0,0,0]:                                             
                          line_x1 = list(lines[i])
                      elif abs(line_x1[1]-lines[i][1]) < LINE_WIDTH:                  
                          for j in range(4):
                              line_x1[j] = (i * line_x1[j] + lines[i][j])/(i+1)
      
                      else:                                                          
                          if line_x2 == [0,0,0,0]:                                         
                              line_x2 = list(lines[i])
                          elif abs(line_x2[3]-lines[i][3]) < LINE_WIDTH:              
                              for j in range(4):
                                  line_x2[j] = (i * line_x2[j] + lines[i][j])/(i+1)
                                                                                      
                  elif lines[i][1] == 0:                  
                      if line_y1 == [0,0,0,0]:                                             
                          line_y1 = list(lines[i])
                      elif abs(line_y1[0]-lines[i][0]) < LINE_WIDTH:                  
                          for j in range(4):
                              line_y1[j] = (i * line_y1[j] + lines[i][j])/(i+1)
                      else:                                                           
                          if line_y2 == [0,0,0,0]:                                         
                              line_y2 = list(lines[i])
                          elif abs(line_y2[2]-lines[i][2]) < LINE_WIDTH:             
                              for j in range(4):
                                  line_y2[j] = (i * line_y2[j] + lines[i][j])/(i+1)                                                                                 
      
          line_all = [line_x1,line_y1,line_x2,line_y2]
      
          for k in range(len(line_all)):
              if line_all[k] == [0,0,0,0]:
                  line_num += 1
          #print(line_x1)
          if (line_x1 != [0,0,0,0]) and (line_x2 != [0,0,0,0]) :
              if line_x1[1] > line_x2[1]:
                  line_temp = line_x1
                  line_x1 = line_x2
                  line_x2 = line_temp
          elif (line_x1 == [0,0,0,0]) and (line_x2 == [0,0,0,0]):
              line_x1 = [0,HEIGHT/2,(WIDTH-XO),HEIGHT/2]
              line_x2 = line_x1
          elif line_x1 == [0,0,0,0]:
              line_x1 = line_x2
          else :
              line_x2 = line_x1
          if (line_y1 != [0,0,0,0]) and (line_y2 != [0,0,0,0]) :
              if line_y1[0] > line_y2[0]:
                  line_temp = line_y1
                  line_y1 = line_y2
                  line_y2 = line_temp
          elif (line_y1 == [0,0,0,0]) and (line_y2 == [0,0,0,0]):
              line_y1 = [(XO+HEIGHT/2),0,(XO+HEIGHT/2),HEIGHT]
              line_y2 = line_y1
          elif line_y1 == [0,0,0,0]:
              line_y1 = line_y2
          else :
              line_y2 = line_y1
          line_all = [line_x1,line_y1,line_x2,line_y2]
          print(line_num)
          print(line_all)
          print(weight)
          for i in range(4):
              line_H[i] = int((line_all[0][i] + line_all[2][i])/2)
      
          for i in range(4):
              line_V[i] = int((line_all[1][i] + line_all[3][i])/2)
          img.draw_line(line_H,127)
          img.draw_line(line_V,127)
          print(line_H)
          print(line_V)
          if line_V[2] != line_V[0]:
              central_x = -(line_V[0]*HEIGHT/(line_V[2]-line_V[0])+line_H[1])/((line_H[3]-line_H[1])/(WIDTH-XO)-HEIGHT/(line_V[2]-line_V[0]))
          else:
              central_x =line_V[0]
          central_y = (line_H[3]-line_H[1])/(WIDTH-XO)*central_x+line_H[1]
          #central position
          img.draw_cross(int(central_x),int(central_y),color=191)
          print(central_x)
          print(central_y)
          err_x = central_x - WIDTH/2
          err_y = central_y - HEIGHT/2
      
          #FLAG
          if (abs((central_y - (HEIGHT/2)) < CENTRAL_AREA) and (abs(central_x - (WIDTH/2)) < CENTRAL_AREA)):
              if line_num >= 3 :
                  if weight > 400 :
                      Flag = 'T'
                  else:
                      Flag = 'L'
              else :
                  Flag = 'I'
          else:
              Flag = 'D'
          print(Flag)
      

      以上这部分代码中,通过串口输出超声波得到的high = int(1.7*highcnt)将图像经过处理后得到的err_x = central_x - WIDTH/2 err_y = central_y - HEIGHT/2
      后两个去掉的话串口是能正常输出high的,但是加上那两个就输出不出来



    • 这边是嫁接过去的关于串口和超声波部分的代码,将其中处理图像的部分改掉了

      # gray_blob - By: jinyuying - 鍛ㄤ簲 浜旀湀 12 2017
      
      import sensor,time,pyb,math,time,utime
      from pyb import Pin, Timer, LED, UART
      #榛戣壊鐐归槇鍊?
      #black_threshold = [(0, 64)]
      black_threshold = [(0, 84)]
      #楂樺害鏁版嵁
      highcnt = 0.0
      high = 0
      #xy骞抽潰璇樊鏁版嵁
      err_x = 0
      err_y = 0
      #瓒呭0娉㈠洖璋冨鐞嗗嚱鏁?
      def Ultrasound(line):
         if(Echo.value()==1):
              tim_count.init(prescaler=1799, period=1176)#鎵撳紑瀹氭椂鍣?
         if(Echo.value()==0):
              global highcnt
              tim_count.deinit()
              highcnt = tim_count.counter()#璁℃暟
              uart.writechar(0x55)
              uart.writechar(0xAA)
              uart.writechar(0x10)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(high>>8)
              uart.writechar(high)
              uart.writechar(err_x>>8)
              uart.writechar(err_x)
              uart.writechar(err_y>>8)
              uart.writechar(err_y)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0xAA)
              print(high)
      
      ##閫夋嫨鎸夐敭鍥炶皟澶勭悊鍑芥暟
      #def selection(IRQ_RISING):
          #if(selectpin.value()==1) :
              #pyb.delay(100)
              #if(selectpin.value()==1):
      
      ##纭鎸夐敭鍥炶皟澶勭悊鍑芥暟
      #def enter(IRQ_RISING):
          #if(enterpin.value()==1) :
              #pyb.delay(100)
              #if(enterpin.value()==1):
      
      
      #瓒呭0娉㈠彂灏勭鍙i厤缃?
      timpwm = Timer(4, freq=60) #瓒呭0娉?0璧吂鍙戝皠棰戠巼
      ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us鍙戝皠瑙?
      #瓒呭0娉㈡帴鏀剁鍙i厤缃?
      tim_count = pyb.Timer(1) #瀹氭椂鍣ㄨ鏁?
      extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#寮€鍚閮ㄤ腑鏂?
      Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
      #鎸夐敭閰嶇疆
      #select_Key = pyb.ExtInt('P2', pyb.ExtInt.IRQ_RISING,  pyb.Pin.PULL_UP, selection)
      #selectpin = Pin('P2', Pin.IN, Pin.PULL_UP)#閫夋嫨鎸夐敭
      #enter_Key = pyb.ExtInt('P3', pyb.ExtInt.IRQ_RISING,  pyb.Pin.PULL_UP, enter)
      #enterpin = Pin('P3', Pin.IN, Pin.PULL_UP)#纭鎸夐敭
      #涓插彛涓夐厤缃?
      uart = UART(3, 115200)
      uart.init(115200, bits=8, parity=None, stop=1)
               #115200
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)#璁剧疆鐏板害淇℃伅
      sensor.set_framesize(sensor.QQVGA)#璁剧疆鍥惧儚澶у皬
      sensor.skip_frames(20)#鐩告満鑷鍑犲紶鍥剧墖
      sensor.set_auto_whitebal(False)#鍏抽棴鐧藉钩琛?
      clock = time.clock()#鎵撳紑鏃堕挓
      while(True):
          clock.tick()
          img = sensor.snapshot()
          high = int(1.7*highcnt)
          blobs = img.find_blobs(black_threshold)
          if blobs:
              most_pixels = 0
              largest_blob = 0
              for i in range(len(blobs)):
                  #鐩爣鍖哄煙鎵惧埌鐨勯鑹插潡鍙兘涓嶆涓€涓紝鎵惧埌鏈€澶х殑涓€涓?
                  if blobs[i].pixels() > most_pixels:
                      most_pixels = blobs[i].pixels()
                      largest_blob = i
                      #浣嶇疆鐜敤鍒扮殑鍙橀噺
                      err_x = int(80 - blobs[largest_blob].cx())
                      err_y = int(60 - blobs[largest_blob].cy())
              print(blobs[largest_blob])
              img.draw_rectangle(blobs[largest_blob].rect())
              img.draw_cross(blobs[largest_blob].cx(),blobs[largest_blob].cy())#璋冭瘯浣跨敤
          else:
             err_x = 0
             err_y = 0
      
      


    • 这么长,看不下去,你也不说重点



    • 改了一下算法,大致是因为类型是float才输出不了的,将err_x转成int型就行了。但是Flag还是无法输出。
      用write(Flag)也不行,改成数据用writechar(Flag)也不行

      
      import sensor,time,pyb,math,time,utime,image
      from pyb import Pin, Timer, UART
      clock = time.clock() # Tracks FPS.
      LINE_WIDTH = 10
      THRESHOLD = 40
      #屏幕尺寸
      WIDTH = 160
      HEIGHT = 120
      ROI = (int((WIDTH-HEIGHT)/2),0,HEIGHT,HEIGHT)
      XO = int((WIDTH-HEIGHT)/2)
      CENTRAL_AREA = 20
      
      #高度数据
      highcnt = 0.0
      high = 0
      #xy平面误差数据
      err_x = 0
      err_y = 0
      #超声波回调处理函数
      def Ultrasound(line):
         if(Echo.value()==1):
              tim_count.init(prescaler=1799, period=1176)#打开定时器
         if(Echo.value()==0):
              global highcnt
              tim_count.deinit()
              highcnt = tim_count.counter()#计数
              uart.writechar(0x55)
              uart.writechar(0xAA)
              uart.writechar(0x10)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(high>>8)
              uart.writechar(high)
              uart.writechar(err_x>>8)
              uart.writechar(err_x)
              uart.writechar(err_y>>8)
              uart.writechar(err_y)
              uart.writechar(Flag)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0x00)
              uart.writechar(0xAA)
              print(high)
      
      
      #超声波发射端口配置
      timpwm = Timer(4, freq=60) #超声波60赫兹发射频率
      ch1 = timpwm.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width=80) #100us发射角
      #超声波接收端口配置
      tim_count = pyb.Timer(1) #定时器计数
      extint = pyb.ExtInt('P0', pyb.ExtInt.IRQ_RISING_FALLING, pyb.Pin.PULL_DOWN,Ultrasound)#开启外部中断
      Echo = Pin('P0', Pin.IN, Pin.PULL_DOWN)
      #串口三配置
      uart = UART(3, 115200)#115200
      uart.init(115200, bits=8, parity=None, stop=1)
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)#设置灰度信息
      sensor.set_framesize(sensor.QQVGA)#设置图像大小
      #sensor.skip_frames(20)#相机自检几张图片
      sensor.set_auto_whitebal(False)#关闭白平衡
      
      while(True):
          high = int(1.7*highcnt)
          line_x1 = [0,0,0,0]
          line_x2 = [0,0,0,0]
          line_y1 = [0,0,0,0]
          line_y2 = [0,0,0,0]
          line_H = [0,0,0,0]
          line_V = [0,0,0,0]
          line_temp = [0,0,0,0]
          line_num = 0
          line_num_y = 0
          line_num_x = 0
          num_H = 0
          num_V = 0
          Flag = int(0)
          weight = 0
          clock.tick() # Track elapsed milliseconds between snapshots().
          img = sensor.snapshot() # Take a picture and return the image.
          #print(img.height())
          #检测图形
      #    img_2=img.binary([(0,127)],invert = 1)
              #image.binary(thresholds, invert=False)此函数将在thresholds内的
              #图像部分的全部像素变为1白,将在阈值外的部分全部像素变为0黑。invert将图像
              #的0 1(黑 白)进行反转,默认为false不反转。
      #    img_2.median(1, percentile=0.5)
      #    img_2.erode(2)
          for i in range(int(HEIGHT/3)):
              for j in range(int(HEIGHT/3)):
                  if img.get_pixel(i*3+XO,j*3) < 127 :
                      weight += 1
                  #print(img.get_pixel(i,j))
          print(weight)
      
          #下面开始检测特征线数(可去?)
          img.find_edges(image.EDGE_CANNY, threshold=(50, 80))  # Find edges
          lines = img.find_lines(roi = ROI,threshold = THRESHOLD) # Find lines.
      #    lines = img.find_lines(threshold = THRESHOLD)
          img.draw_rectangle(ROI)
          #for l in lines: img.draw_line(l, color=(127)) # Draw lines
          #print(lines) # Note: Your OpenMV Cam runs about half as fast while
          if len(lines) != 0:
              #print(len(lines),lines)
              for i in range(len(lines)):
                  #print(i)
                  if lines[i][0] == 0:                    #为横线时
                      if line_x1 == [0,0,0,0]:                                             #横一无值时
                          line_x1 = list(lines[i])
                      elif abs(line_x1[1]-lines[i][1]) < LINE_WIDTH:                  #为标准横一线时
                          for j in range(4):
                              line_x1[j] = (i * line_x1[j] + lines[i][j])/(i+1)
      
                      else:                                                           #不为标准横一线时
                          if line_x2 == [0,0,0,0]:                                         #横二无值时
                              line_x2 = list(lines[i])
                          elif abs(line_x2[3]-lines[i][3]) < LINE_WIDTH:              #为标准横二线时
                              for j in range(4):
                                  line_x2[j] = (i * line_x2[j] + lines[i][j])/(i+1)
                                                                                      #为其他横线时舍弃
                  elif lines[i][1] == 0:                  #为竖线时
                      if line_y1 == [0,0,0,0]:                                             #竖一无值时
                          line_y1 = list(lines[i])
                      elif abs(line_y1[0]-lines[i][0]) < LINE_WIDTH:                  #为标准竖一线时
                          for j in range(4):
                              line_y1[j] = (i * line_y1[j] + lines[i][j])/(i+1)
                      else:                                                           #不为标准竖一线时
                          if line_y2 == [0,0,0,0]:                                         #竖二无值时
                              line_y2 = list(lines[i])
                          elif abs(line_y2[2]-lines[i][2]) < LINE_WIDTH:              #为标准竖二线时
                              for j in range(4):
                                  line_y2[j] = (i * line_y2[j] + lines[i][j])/(i+1)   #为其他竖线时舍弃
                                                                                      #为其他线时舍弃
      
          line_all = [line_x1,line_y1,line_x2,line_y2]
      
          for k in range(len(line_all)):
              if line_all[k][2] == 140:
                  line_num_x += 1
              if line_all[k][3] == 120:
                  line_num_y += 1
          line_num = line_num_y + line_num_x
          if line_num == 2:
              if (line_num_y == 2) or (line_num_x == 2):
                  Flag_0 = '-'
              else:
                  Flag_0 = '+'
          elif line_num < 2:
              Flag_0 = '-'
          else:
              Flag_0 = '+'
          print(line_num)
          #line_all = [line_x1,line_y1,line_x2,line_y2]
          #print(line_all)
          #下面开始检测横纵中线
          if len(lines) != 0:
              for i in range(len(lines)):
              #print(len(lines),lines)
                  if lines[i][0] == 0:                                                    #为横线时
                      if line_H == [0,0,0,0]:                                             #横一无值时
                          line_H = list(lines[i])
                          num_H += 1
                      elif abs(line_H[1]-lines[i][1]) < 3*LINE_WIDTH and abs(line_H[3]-lines[i][3]) < 3*LINE_WIDTH:
                          for j in range(4):
                              line_H[j] = (num_H * line_H[j] + lines[i][j])/(num_H+1)
                          num_H += 1
                  elif lines[i][1] == 0:                                                  #为竖线时
                      if line_V == [0,0,0,0]:                                                 #横一无值时
                          line_V = list(lines[i])
                          num_V += 1
                      elif abs(line_V[0]-lines[i][0]) < 3*LINE_WIDTH and abs(line_V[2]-lines[i][2]) < 3*LINE_WIDTH:
                          for j in range(4):
                              line_V[j] = (num_V * line_V[j] + lines[i][j])/(num_V+1)
                          num_V += 1
          #print(line_x1)
          if line_H == [0,0,0,0]:
              line_H = [0,HEIGHT/2,(WIDTH-XO),HEIGHT/2]
          if line_V == [0,0,0,0]:
              line_V = [(XO+HEIGHT/2),0,(XO+HEIGHT/2),HEIGHT]
          line_H = [ int(x) for x in line_H ]
          line_V = [ int(x) for x in line_V ]
      
          img.draw_line(line_H,127)
          img.draw_line(line_V,127)
          print(line_H)
          print(line_V)
          #找直线的交点(已完成)
          if line_V[2] != line_V[0]:
              central_x = -(line_V[0]*HEIGHT/(line_V[2]-line_V[0])+line_H[1])/((line_H[3]-line_H[1])/(WIDTH-XO)-HEIGHT/(line_V[2]-line_V[0]))
          else:
              central_x = line_V[0]
          central_y = (line_H[3]-line_H[1])/(WIDTH-XO)*central_x+line_H[1]
          img.draw_cross(int(central_x),int(central_y),color=191)
          print(central_x)
          print(central_y)
          err_x = int(central_x - WIDTH/2)
          err_y = int(central_y - HEIGHT/2)
      
          #接下来是FLAG
          if (abs((central_y - (HEIGHT/2)) < CENTRAL_AREA) and (abs(central_x - (WIDTH/2)) < CENTRAL_AREA)):
              if Flag_0 == '+' :
                  if weight > 300 :
                      Flag = 0xFF     #T
                  else:
                      Flag = 0x7F     #L
              elif Flag_0 =='-' :
                  Flag = 0x3F         #I
          else:
              Flag = 0x00             #D
          print(Flag)