# -*- coding:utf-8 -*- # @Author len # @Create 2023/11/30 11:08 import sensor, lcd, time from machine import Timer, PWM, UART, Timer lcd.init(freq=15000000) # 初始化LCD sensor.reset() # 复位和初始化摄像头 sensor.set_vflip(1) # 将摄像头设置成后置方式(所见即所得) sensor.set_pixformat(sensor.RGB565) # 设置像素格式为彩色 RGB565 sensor.set_framesize(sensor.QVGA) # 设置帧大小为 QVGA (320x240) sensor.set_auto_gain(False) sensor.set_auto_whitebal(False) sensor.set_auto_gain(0,0) sensor.skip_frames(time = 200) # 等待设置生效 def Servo(angle): ''' 说明:舵机控制函数 功能:180度舵机:angle:-90至90 表示相应的角度 360连续旋转度舵机:angle:-90至90 旋转方向和速度值。 【duty】占空比值:0-100 ''' #PWM通过定时器配置,接到IO17引脚 tim_pwm = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM) S1 = PWM(tim_pwm, freq=50, duty=0, pin=17) S1.duty((angle+90)/180*10+2.5) Servo(25) # 阈值和颜色标签 # thresholds = [(35, 100, 6, 127, 0, 127, "红色"), # 红色阈值 # (25, 100, -6, 127, 5, 43, "黄色"), # 黄色阈值 # (52, 100, -128, -5, 5, 127, "绿色")] # 绿色阈值 # thresholds = [(51, 62, 33, 101, 14, 127, "红色"), # 红色阈值 # (39, 98, -17, 21, 17, 57, "黄色"), # 黄色阈值 # (44, 90, -61, -13, -3, 30, "绿色")] # 绿色阈值 thresholds = [(43, 68, 7, 74, 1, 46, "红色"), # 红色阈值 (48, 100, -23, 19, 4, 55, "黄色"), # 黄色阈值 (40, 93, -57, -7, -8, 29, "绿色")] # 绿色阈值 # thresholds = [(43, 68, 7, 74, 1, 46, "红色"), # 红色阈值 # (34, 98, -17, 50, 7, 67, "黄色"), # 黄色阈值 # (40, 93, -57, -7, -8, 29, "绿色")] # 绿色阈值 # thresholds = [(43, 68, 7, 74, 1, 46, "红色"), # 红色阈值 # (48, 100, -23, 19, 4, 55, "黄色"), # 黄色阈值 # (40, 93, -57, -7, -8, 29, "绿色")] # 绿色阈值 # thresholds = [ # (193, 213, 140, 160, 119, 139, "红色"), # 更强的红色区分 # (40, 80, -10, 30, 20, 60, a"黄色"), # 更强的黄色区分 # (40, 90, -50, -10, -20, 30, "绿色") # 保持绿色 # ] # 初始化 results = [] start_time = time.time() while time.time() - start_time < 3: # 循环持续三秒 img = sensor.snapshot() max_blob = None max_blob_size = 0 max_blob_color = "" max_blob_color_index = 0 # 找圆 circles = img.find_circles(threshold=3500, x_margin=10, y_margin=10, r_margin=10, r_min=2, r_max=100, r_step=2) rects = img.find_rects(threshold=3500, x_margin=10, y_margin=10, r_margin=10, r_min=2, r_max=100, r_step=2) for index, threshold in enumerate(thresholds): # 查找每种颜色的色块 blobs = img.find_blobs([threshold[:-1]], pixels_threshold=200, area_threshold=200, merge=True) if blobs: # 找到最大的色块 blob = max(blobs, key=lambda b: b.pixels()) if blob.pixels() > max_blob_size: max_blob = blob max_blob_size = blob.pixels() max_blob_color = threshold[-1] # 颜色标签 max_blob_color_index = index print("圆形:", circles) # 画圆 for c in circles: area = (c.x() - c.r(), c.y() - c.r(), 2 * c.r(), 2 * c.r()) img.draw_rectangle(area, color=(255, 255, 255)) # 画矩形 print("矩形:", rects) for c in rects: area = (c.x() - c.r(), c.y() - c.r(), 2 * c.r(), 2 * c.r()) img.draw_rectangle(area, color=(0, 0, 0)) # 绘制最大色块的矩形和中心十字,并输出颜色 if max_blob: results.append((max_blob_color, max_blob_color_index)) # 存储识别结果 img.draw_rectangle(max_blob[0:4]) img.draw_cross(max_blob[5], max_blob[6]) print("最大色块的颜色是:", max_blob_color, max_blob_color_index) # 初始化计数字典 color_count = {} for color, index in results: if (color, index) in color_count: color_count[(color, index)] += 1 else: color_count[(color, index)] = 1 # 找出出现次数最多的颜色和下标 if color_count: most_common_color, most_common_index = max(color_count, key=color_count.get) print("出现最多的颜色:", most_common_color, "在下标位置:", most_common_index) else: print("没有识别到有效的颜色块") while True: pass