• 免费好用的星瞳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, image, time,math,pyb
      from pyb import UART,LED
      import jsonimport sensor, image, time,math,pyb
      from pyb import UART,LED
      import json
      import ustruct
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      sensor.set_auto_gain(False) # must be turned off for color tracking
      sensor.set_auto_whitebal(False) # must be turned off for color tracking
      wine_red_threshold = (52, 27, 8, 127, -30, 25)  # 酒红色阈值示例
      pomegranate_red_threshold = (0, 63, 41, 127, -128, 127) # 石榴红阈值示例
      blue_threshold = (48, 64, -50, 2, -65, -15)             # 蓝色阈值
      green_threshold = (92, 59, -75, -14, 2, 35)           # 绿色阈值
      clock = time.clock()
      
      uart = UART(3,19200)   #定义串口3变量
      uart.init(19200, bits=8, parity=None, stop=1) # init with given parameters
      
      def find_max(blobs):    #定义寻找色块面积最大的函数
          max_size=0
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob=blob
                  max_size = blob.pixels()
          return max_blob
      
      
      def sending_data(cx,cy,cw,ch):
          global uart;
          #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
          #data = bytearray(frame)
          data = ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                         0x2C,                      #帧头1
                         0x12,                      #帧头2
                         int(cx), # up sample by 4   #数据1
                         int(cy), # up sample by 4    #数据2
                         int(cw), # up sample by 4    #数据1
                         int(ch), # up sample by 4    #数据2
                         0x5B)
          uart.write(data);   #必须要传入一个字节数组
          # 主循环
          while(True):
              img = sensor.snapshot()
      
              # 检测酒红色并输出坐标
              blobs_wine_red = img.find_blobs([wine_red_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_wine_red:
                  for blob in blobs_wine_red:
                      rect = blob.rect()
                      top_center_x = rect[0] + rect[2] // 2
                      top_center_y = rect[1]
                      print(f"Wine Red Top Center: ({top_center_x}, {top_center_y})")
              # 检测石榴红、蓝色和绿色并输出对应数字
              detected_colors = []
      
              # 检测石榴红
              blobs_pomegranate_red = img.find_blobs([pomegranate_red_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_pomegranate_red:
                  detected_colors.append(3)
      
              # 检测蓝色
              blobs_blue = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_blue:
                  detected_colors.append(2)
      
              # 检测绿色
              blobs_green = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_green:
                  detected_colors.append(1)
      
              # 输出检测到的颜色编号
              if detected_colors:
                  print("Detected Colors (Numbers):", detected_colors)
      
              # 延迟以减少处理速度
              time.sleep_ms(10000)
      
      import ustruct
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      sensor.set_auto_gain(False) # must be turned off for color tracking
      sensor.set_auto_whitebal(False) # must be turned off for color tracking
      wine_red_threshold = (52, 27, 8, 127, -30, 25)  # 酒红色阈值示例
      pomegranate_red_threshold = (0, 63, 41, 127, -128, 127) # 石榴红阈值示例
      blue_threshold = (48, 64, -50, 2, -65, -15)             # 蓝色阈值
      green_threshold = (92, 59, -75, -14, 2, 35)           # 绿色阈值
      clock = time.clock()
      
      uart = UART(3,19200)   #定义串口3变量
      uart.init(19200, bits=8, parity=None, stop=1) # init with given parameters
      
      def find_max(blobs):    #定义寻找色块面积最大的函数
          max_size=0
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob=blob
                  max_size = blob.pixels()
          return max_blob
      
      
      def sending_data(cx,cy,cw,ch):
          global uart;
          #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
          #data = bytearray(frame)
          data = ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                         0x2C,                      #帧头1
                         0x12,                      #帧头2
                         int(cx), # up sample by 4   #数据1
                         int(cy), # up sample by 4    #数据2
                         int(cw), # up sample by 4    #数据1
                         int(ch), # up sample by 4    #数据2
                         0x5B)
          uart.write(data);   #必须要传入一个字节数组
          # 主循环 
          img = sensor.snapshot()
          while(True):
             
      
              # 检测酒红色并输出坐标
              blobs_wine_red = img.find_blobs([wine_red_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_wine_red:
                  for blob in blobs_wine_red:
                      rect = blob.rect()
                      top_center_x = rect[0] + rect[2] // 2
                      top_center_y = rect[1]
                      print(f"Wine Red Top Center: ({top_center_x}, {top_center_y})")
              
              # 检测石榴红
              blobs_pomegranate_red = img.find_blobs([pomegranate_red_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_pomegranate_red:
                  detected_colors.append(3)
      
              # 检测蓝色
              blobs_blue = img.find_blobs([blue_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_blue:
                  detected_colors.append(2)
      
              # 检测绿色
              blobs_green = img.find_blobs([green_threshold], pixels_threshold=200, area_threshold=200)
              if blobs_green:
                  detected_colors.append(1)
      
              # 输出检测到的颜色编号
              if detected_colors:
                  print("Detected Colors (Numbers):", detected_colors)
      
              # 延迟以减少处理速度
              time.sleep_ms(10000)
              # 检测石榴红、蓝色和绿色并输出对应数字
              detected_colors = []
      
      


    • 你把代码整理好了再发。