多颜色识别框选代码
'''
实验名称:串口通信
版本: v1.0
日期: 2019.12
作者: 01Studio
说明:通过编程实现串口通信,跟电脑串口助手实现数据收发。
'''
import sensor,lcd,time
import utime
from machine import UART,Timer
from fpioa_manager import fm
#摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_vflip(1) #后置模式,所见即所得
#lcd初始化
lcd.init()
clock=time.clock()
# 颜色识别阈值 (L Min, L Max, A Min, A Max, B Min, B Max) LAB模型
# 下面的阈值元组是用来识别 红、绿、蓝三种颜色,当然你也可以调整让识别变得更好。
thresholds = [(20, 60, 35, 65, 10, 40), # 红色阈值
(15, 65, -45, -10, 0, 40), # 绿色阈值
(35, 80, -30, 2, -30, 2)] # 蓝色阈值
#映射串口引脚
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
#初始化串口
uart = UART(UART.UART1, 9600, read_buf_len=4096)
while True:
clock.tick()
img=sensor.snapshot()
utime.sleep(0.1)
blobs = img.find_blobs(thresholds,merge=1,pixels_threshold=900,
x_stride=2, y_stride=1) # 0,1,2分别表示红,绿,蓝色。
if blobs:
for b in blobs:
if(b[8]==1):
#识别到红色
uart.write('RED\r\n') #数据回传
print('RED\r\n')
tmp=img.draw_rectangle(b[0:4],color = (255, 0, 0),
thickness = 3, fill = False)
elif(b[8]==2):
#识别到绿色
uart.write('GREEN\r\n') #数据回传
print('GREEN\r\n')
tmp=img.draw_rectangle(b[0:4],color = (0, 255, 0),
thickness = 3, fill = False)
elif(b[8]==4):
#识别到蓝色
uart.write('BLUE\r\n') #数据回传
print('BLUE\r\n')
tmp=img.draw_rectangle(b[0:4],color = (0, 0, 255),
thickness = 3, fill = False)
print(b[8])
else:
uart.write('WHITE\r\n') #数据回传
lcd.display(img) #LCD显示图片
判断颜色返回数字值
'''
实验名称:串口通信
版本: v1.0
日期: 2019.12
作者: 01Studio
说明:通过编程实现串口通信,跟电脑串口助手实现数据收发。
'''
import sensor,lcd,time
import utime
from machine import UART,Timer
from fpioa_manager import fm
#摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_vflip(1) #后置模式,所见即所得
from maix import GPIO
from fpioa_manager import fm
#注册IO,蓝灯-->IO35,KEY-->IO16
fm.register(35, fm.fpioa.GPIO0)
fm.register(8, fm.fpioa.GPIO1)
#lcd初始化
lcd.init()
clock=time.clock()
LED_B = GPIO(GPIO.GPIO0, GPIO.OUT)
LED_A = GPIO(GPIO.GPIO1, GPIO.OUT)
# 颜色识别阈值 (L Min, L Max, A Min, A Max, B Min, B Max) LAB模型
# 下面的阈值元组是用来识别 红、绿、蓝三种颜色,当然你也可以调整让识别变得更好。
thresholds = [(20, 60, 35, 65, 10, 40), # 红色阈值
(15, 65, -45, -10, 0, 40), # 绿色阈值
(35, 80, -30, 2, -30, 2)] # 蓝色阈值
#映射串口引脚
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
#初始化串口
uart = UART(UART.UART1, 9600, read_buf_len=4096)
while True:
clock.tick()
img=sensor.snapshot()
utime.sleep(0.1)
blobs = img.find_blobs(thresholds,merge=1,pixels_threshold=900,
x_stride=2, y_stride=1) # 0,1,2分别表示红,绿,蓝色。
if blobs:
for b in blobs:
if(b[8]==1):
#识别到红色
uart.write('RED\r\n') #数据回传
LED_B.value(1) #XIMIELED,蓝灯
LED_A.value(1)
print('RED\r\n')
tmp=img.draw_rectangle(b[0:4],color = (255, 0, 0),
thickness = 3, fill = False)
elif(b[8]==2):
#识别到绿色
uart.write('GREEN\r\n') #数据回传
LED_B.value(0) #点亮LED,蓝灯
LED_A.value(0)
print('GREEN\r\n')
tmp=img.draw_rectangle(b[0:4],color = (0, 255, 0),
thickness = 3, fill = False)
elif(b[8]==4):
#识别到蓝色
uart.write('BLUE\r\n') #数据回传
LED_B.value(1) #XIMIELED,蓝灯
LED_A.value(1)
print('BLUE\r\n')
tmp=img.draw_rectangle(b[0:4],color = (0, 0, 255),
thickness = 3, fill = False)
print(b[8])
else:
uart.write('WHITE\r\n') #数据回传
LED_B.value(1) #XIMIELED,蓝灯
LED_A.value(1)
lcd.display(img) #LCD显示图片
显示物体坐标
import sensor, time, image,lcd
from machine import Timer,UART,PWM
from maix import GPIO
from fpioa_manager import fm
Angle = 0
Angle2 = 0
Servo1_Pin = 1
Servo2_Pin = 2
#sensor.reset()
#sensor.reset(freq=24000000, dual_buff=True)
#sensor.set_pixformat(sensor.RGB565)
#sensor.set_framesize(sensor.QVGA)
#sensor.set_auto_exposure(1)
#sensor.set_auto_gain(0, gain_db=12)
#sensor.set_auto_whitebal(0, rgb_gain_db=(0, 0, 0))
#sensor.set_vflip(1)
#sensor.set_hmirror(1)
#sensor.skip_frames(time=2000)
#摄像头初始化
sensor.reset()
sensor.reset(freq=24000000, dual_buff=True)
sensor.set_auto_gain(1, gain_db=12)
sensor.set_auto_whitebal(0, rgb_gain_db=(0, 0, 0))
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_vflip(1) #后置模式,所见即所得
sensor.run(1)
sensor.skip_frames(30)
#映射串口引脚
fm.register(15 , fm.fpioa.UART1_RX, force=True)
fm.register(16, fm.fpioa.UART1_TX, force=True)
#初始化串口
uart = UART(UART.UART1, 9600, read_buf_len=4096)
#lcd初始化
lcd.init()
clock=time.clock()
class PID_property():
Kp = 0
Ki = 0
Kd = 0
P = 0
I = 0
D = 0
now_error = 0
last_error = 0
now_error_LeiJi = 0
last_last_error = 0
Out_posPID = 0
OUT_PWM = 0
S1_PD = PID_property() ###############上面 舵机
S1_PD.Kp = 0.555
S1_PD.Ki = 0.00005
S1_PD.Kd = 0.0005
S2_PD = PID_property() ###############下面 舵机
S2_PD.Kp = 0.739
S2_PD.Ki = 0.00009
S2_PD.Kd = 0.0008
def PID_Pos(Sx_PD,erro):
Sx_PD.now_error = erro #误差
Sx_PD.now_error_LeiJi = Sx_PD.now_error + Sx_PD.now_error_LeiJi#累计误差_求和
Sx_PD.P = Sx_PD.Kp * Sx_PD.now_error
Sx_PD.I = Sx_PD.Ki * Sx_PD.now_error_LeiJi
Sx_PD.D = Sx_PD.Kd * (Sx_PD.now_error - Sx_PD.last_error)
Sx_PD.Out_posPID = Sx_PD.P + Sx_PD.I + Sx_PD.D
Sx_PD.last_error = Sx_PD.now_error
Sx_PD.OUT_PWM = Sx_PD.Out_posPID
return Sx_PD.OUT_PWM
class color_property():
cx = 0
cy = 0
flag = 0
color = 0
density = 0
pixels_max = 0
led_flag = 0
color_threshold = (0, 0, 0, 0, 0, 0)
color_roi = (0, 0, 320, 240)
color_x_stride = 1
color_y_stride = 1
color_pixels_threshold = 10
color_area_threshold = 10
color_merge = True
color_margin = 1
black = color_property()
black.color_threshold = (51, 99, -128, 127, -128, 127)
black.color_roi = (0, 0, 320, 240)
black.color_x_stride = 1
black.color_y_stride = 1
black.color_pixels_threshold = 10
black.color_area_threshold = 10
black.color_merge = True
black.color_margin = 1
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
S1 = PWM(tim, freq=50, duty=0, pin=Servo1_Pin)
tim2 = Timer(Timer.TIMER2, Timer.CHANNEL2, mode=Timer.MODE_PWM)
S2 = PWM(tim2, freq=50, duty=0, pin=Servo2_Pin)
def Servo(S_num):
global Angle
if Angle > 90:
Angle = 90
elif Angle < -90:
Angle = -90
S_num.duty((Angle+90)/180*10+2.5) # 占空比范围从0到1
def Servo2(S_num):
global Angle2
if Angle2 > 90:
Angle2 = 90
elif Angle2 < -90:
Angle2 = -90
S_num.duty((Angle2+90)/180*10+2.5) # 占空比范围从0到1
def find_max(blobs):
max_size = 0
max_blob = None
for blob in blobs:
if blob[2] * blob[3] > max_size:
max_blob = blob
max_size = blob[2] * blob[3]
return max_blob
while True:
err = 0
img = sensor.snapshot()
blobs = img.find_blobs([black.color_threshold], area_threshold=50, pixels_threshold=50)
if blobs:
b = find_max(blobs)
img.draw_rectangle(b[0:4])
img.draw_cross(b[5], b[6], color=(0, 255, 0), size=15)
x_pos = b[5] # 中心位置
y_pos = b[6] # 中心位置
width = b[2] # 宽度
height = b[3] # 高度
area = width * height # 面积
X = '%03d' % x_pos
Y = '%05d' % y_pos
DATA = '@' + X + Y + 'A' + 'F'
uart.write(DATA)
print(DATA)
Angle = PID_Pos(S1_PD, x_pos - 160)
Angle2 = PID_Pos(S2_PD, y_pos - 120)
Servo2(S2)
Servo(S1)
print("矩形面积:", area)
print("矩形长度:", width)
print("矩形宽度:", height)
print("中心坐标X:", x_pos)
print("中心坐标Y:", y_pos)
# 打印占空比
servo1_duty = ((Angle+90)/180*10+2.5)
servo2_duty = ((Angle2+90)/180*10+2.5)
print("舵机1占空比:", servo1_duty)
print("舵机2占空比:", servo2_duty)
print(Angle)
print(Angle2)
# 在LCD上显示数据
# lcd.clear()
img.draw_string(2, 5, "Servo1 Duty:{:.2f}".format(servo1_duty), scale=2, color=(255, 0, 0))
img.draw_string(2, 30, "Servo2 Duty:{:.2f}".format(servo2_duty), scale=2, color=(255, 0, 0))
img.draw_string(2, 55, "x_pos: {}".format(x_pos), scale=2, color=(255, 0, 0))
img.draw_string(2, 80, "y_pos: {}".format(y_pos), scale=2, color=(255, 0, 0))
else:
# 如果没有检测到色块,则输出默认值
#print("矩形面积:", 0)
#print("矩形长度:", 0)
#print("矩形宽度:", 0)
print("中心坐标X:", 160)
print("中心坐标Y:", 120)
x_pos=160
y_pos=120
X = '%03d' % x_pos
Y = '%05d' % y_pos
DATA = '@' + X + Y + 'A' + 'F'
print(DATA)
uart.write(DATA)
# 在LCD上显示默认值
#lcd.clear()
img.draw_string(2, 5, "NO", scale=3, color=(255, 0, 0))
img.draw_string(2, 35, "NO", scale=3, color=(255, 0, 0))
img.draw_string(2, 65, "x_pos:160", scale=3, color=(255, 0, 0))
img.draw_string(2, 95, "y_pos:120", scale=3, color=(255, 0, 0))
lcd.display(img) #LCD显示图片
目标效果代码: