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



    • 请在这里粘贴代码# Edge Impulse - OpenMV Object Detection Example
      
      import sensor, image, time, os, tf, math, uos, gc
      from pyb import UART
      import chess
      
      sensor.reset()                         # Reset and initialize the sensor.
      sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)
      sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
      sensor.set_windowing((240, 240))       # Set 240x240 window.
      sensor.skip_frames(time=2000)          # Let the camera adjust.
      sensor.set_auto_gain(True)
      sensor.set_auto_whitebal(False)
      uart = UART(3, 115200)  #OpenMV RT 用UART(1)这行,注释掉上一行UART(3)
      
      
      
      #生成九宫格的区域位置
      def generate_centered_rois(width, height, b, k):
          rois = []
      
          # 计算每个ROI中心的位置偏移
          offset = (b - k) // 2
      
          # 计算整个3x3矩阵的宽度和高度
          total_width = 3 * b
          total_height = 3 * b
      
          # 计算左上角的起始点,使矩阵居中
          start_x = (width - total_width) // 2
          start_y = (height - total_height) // 2
      
          for i in range(3):
              row = []
              for j in range(3):
                  x_center = start_x + j * b + b // 2
                  y_center = start_y + i * b + b // 2
                  x = x_center - k // 2
                  y = y_center - k // 2
                  row.append((x, y, k, k))
              rois.append(row)
      
          return rois
      
      
      distance = 53
      block = 18
      # 九宫格的区域位置
      rois = generate_centered_rois(sensor.width(), sensor.height(), distance, block)
      
      # 棋盘数组
      # 黑子:X
      # 白子:O
      # 没有棋子:空字符串
      board = [
           [" "," "," "],
           [" "," "," "],
           [" "," "," "],
      ]
      
      clock = time.clock()
      while(True):
          clock.tick()
      
          img = sensor.snapshot()
      
      
          # 图像识别得到棋盘数组
          for y in range(len(rois)):
              for x in range(len(rois[y])):
                  gray = img.get_statistics(roi=rois[y][x]).mean()
                  if gray < 45:
                      board[y][x] = "X"
                  elif gray > 60:
                      board[y][x] = "O"
                  else:
                      board[y][x] = " "
      
          # 打印当前棋盘数组
          for line in board:
              print(line)
          print()
      
      
          # 画棋盘数组
          for y in range(len(rois)):
              for x in range(len(rois[y])):
                  if board[y][x] == "X":
                      color = 255
                  elif board[y][x] == "O":
                      color = 0
                  elif board[y][x] == " ":
                      color = 127
                  img.draw_rectangle(rois[y][x], color=color)
      
      #第四题   数据接收  接收1发送坐标
          if uart.any():
              data=uart.read(1)
              if  data==b'0' :
                  if chess.check_win(board, 'O'):
                      print("你赢啦!")
                  elif chess.check_win(board, 'X'):
                      print("我赢啦!")
                  elif chess.check_draw(board):
                      print("平局啦!")
                  elif chess.check_turn(board) == "X":
                      # 计算下一步棋子放在哪里
                      line,row =chess.computer_move(board)
                      sensor.flush()
                  elif chess.check_turn(board) == "O":
                      print("该你下了!")
      
      
                  if line==0 and row ==0 :
                      uart.writechar(1)
                  elif line==0 and row==1:
                      uart.writechar(2)
                  elif line==0 and row==2:
                      uart.writechar(3)
                  elif line==1 and row==0:
                      uart.writechar(4)
                  elif line==1 and row==1:
                      uart.writechar(5)
                  elif line==1 and row==2:
                      uart.writechar(6)
                  elif line==2 and row==0:
                      uart.writechar(7)
                  elif line==2 and row==1:
                      uart.writechar(8)
                  elif line==2 and row==2:
                      uart.writechar(9)
      
      
      
      #第五题   数据接收  接收1发送坐标
          if uart.any():
              data=uart.read(1)
              if  data==b'1' :
                  if chess.check_win(board, 'O'):
                      print("你赢啦!")
                  elif chess.check_win(board, 'X'):
                      print("我赢啦!")
                  elif chess.check_draw(board):
                      print("平局啦!")
                  elif chess.check_turn(board) == "O":
                      # 计算下一步棋子放在哪里
                      line,row =chess.computer_move(board)
                      sensor.flush()
                  elif chess.check_turn(board) == "X":
                      print("该你下了!")
      
      
                  if line==0 and row ==0 :
                      uart.writechar(1)
                  elif line==0 and row==1:
                      uart.writechar(2)
                  elif line==0 and row==2:
                      uart.writechar(3)
                  elif line==1 and row==0:
                      uart.writechar(4)
                  elif line==1 and row==1:
                      uart.writechar(5)
                  elif line==1 and row==2:
                      uart.writechar(6)
                  elif line==2 and row==0:
                      uart.writechar(7)
                  elif line==2 and row==1:
                      uart.writechar(8)
                  elif line==2 and row==2:
                      uart.writechar(9)
      

      模块化代码

      chess.py

      SIZE = 3

      检查赢了吗

      def check_win(board, player):
      # Check rows and columns
      for i in range(SIZE):
      if all(board[i][j] == player for j in range(SIZE)) or
      all(board[j][i] == player for j in range(SIZE)):
      return True
      # Check diagonals
      if all(board[i][i] == player for i in range(SIZE)) or
      all(board[i][SIZE - 1 - i] == player for i in range(SIZE)):
      return True
      return False

      检查平局了吗

      def check_draw(board):
      return all(board[i][j] != ' ' for i in range(SIZE) for j in range(SIZE))

      计算策略得分

      def minimax(board, depth, is_maximizing):
      computer = 'X'
      player = 'O'

      if check_win(board, computer):
          return 10 - depth
      if check_win(board, player):
          return depth - 10
      if check_draw(board):
          return 0
      
      if is_maximizing:
          best_score = float('-inf')
          for i in range(SIZE):
              for j in range(SIZE):
                  if board[i][j] == ' ':
                      board[i][j] = computer
                      score = minimax(board, depth + 1, False)
                      board[i][j] = ' '
                      best_score = max(score, best_score)
          return best_score
      else:
          best_score = float('inf')
          for i in range(SIZE):
              for j in range(SIZE):
                  if board[i][j] == ' ':
                      board[i][j] = player
                      score = minimax(board, depth + 1, True)
                      board[i][j] = ' '
                      best_score = min(score, best_score)
          return best_score
      

      计算下一步位置

      def computer_move(board):
      if board == [
      [" "," "," "],
      [" "," "," "],
      [" "," "," "]
      ]:
      return 1,1
      best_score = float('-inf')
      move = (-1, -1)
      for i in range(SIZE):
      for j in range(SIZE):
      if board[i][j] == ' ':
      board[i][j] = 'X'
      score = minimax(board, 0, False)
      board[i][j] = ' '
      if score > best_score:
      best_score = score
      move = (i, j)
      if move != (-1, -1):
      # board[move[0]][move[1]] = 'X'
      print(f"Computer places X at ({move[0]}, {move[1]})")
      return move[0], move[1]

      检查该谁走了

      def check_turn(board):
      x_count = sum(row.count("X") for row in board)
      o_count = sum(row.count("O") for row in board)
      return "X" if x_count == o_count else "O"