出现TypeError: extra positional arguments given问题,怎么解决?
-
代码如下:
import sensor, time, display import image # 摄像头初始化 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.set_vflip(0) # 摄像头后置模式 # LCD初始化 lcd = display.SPIDisplay() clock = time.clock() # 获取中心区域的坐标和尺寸 def get_center_grid_coords(): screen_width = lcd.width() screen_height = lcd.height() step = 200 # 设定为200x200的区域 x = (screen_width - step) // 2 y = (screen_height - step) // 2 return (x, y, step, step) # 获取9个小方格区域的坐标和尺寸 def get_subgrid_coords(x, y, w, h): subgrid_size = w // 3 # 每个小方格的尺寸 subgrid_coords = [] for j in range(3): for i in range(3): sub_x = x + i * subgrid_size sub_y = y + j * subgrid_size subgrid_coords.append((sub_x, sub_y, subgrid_size, subgrid_size)) return subgrid_coords # 计算灰度值 def get_gray_value(image, x, y): return image.get_pixel(x, y) # 获取灰度值 # 计算每个小方格中心的颜色 def classify_subgrids(img, subgrid_coords): result = [[0] * 3 for _ in range(3)] # 转换为灰度图像 gray_img = img.to_grayscale() for i, (x, y, w, h) in enumerate(subgrid_coords): subgrid_img = gray_img.copy().crop((x, y, w, h)) # 计算小方格中心点的坐标 center_x = w // 2 center_y = h // 2 # 获取中心点的灰度值 center_gray_value = get_gray_value(subgrid_img, center_x, center_y) # 判断灰度值 if center_gray_value < 50: # 判断是否为黑子 result[i // 3][i % 3] = 1 # 黑子 img.draw_rectangle(x, y, w, h, color=(255, 255, 255)) # 白色矩形标记 img.draw_string(x + 10, y + 10, 'Black', color=(255, 255, 255)) # 白色文字 elif center_gray_value > 200: # 判断是否为白子 result[i // 3][i % 3] = 0 # 白子 img.draw_rectangle(x, y, w, h, color=(0, 0, 0)) # 黑色矩形标记 img.draw_string(x + 10, y + 10, 'White', color=(0, 0, 0)) # 黑色文字 else: result[i // 3][i % 3] = 3 # 无子 img.draw_rectangle(x, y, w, h, color=(0, 0, 0)) # 黑色矩形标记 img.draw_string(x + 10, y + 10, 'None', color=(0, 0, 0)) # 黑色文字 return result # 主循环 while True: clock.tick() img = sensor.snapshot() # 获取中心区域的坐标和尺寸 center_grid_coords = get_center_grid_coords() # 获取9个小方格的坐标 subgrid_coords = get_subgrid_coords(*center_grid_coords) # 识别颜色并分类棋子 grid_result = classify_subgrids(img, subgrid_coords) # 打印棋盘状态 for row in grid_result: print(row) # LCD显示图片 lcd.write(img) # 打印FPS print(clock.fps())
其中,在
subgrid_img = gray_img.copy().crop((x, y, w, h))
报错
-
subgrid_img = gray_img.copy().crop((x, y, w, h))
改为
subgrid_img = gray_img.copy().crop(roi=(x, y, w, h))