logo

多色识别不同追踪

2018-08-19 14:04
V
啊彬

# 当收到“a”时开启颜色识别,识别到不同颜色执行不同程序,小车有不同的反应,但是一开启摄像头老是识别到不是你设点的颜色,不知道为啥?急用,请帮我看下,非常感谢

# This example shows off how to use the find_blobs function to find color

# blobs in the image. This example in particular looks for dark green objects.

from pyb import UART

uart = UART(3,9600)

import sensor, image, time

import car

from pid import PID

sensor.reset() # Initialize the camera sensor.

sensor.set_pixformat(sensor.RGB565) # use RGB565.

sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.

sensor.skip_frames(10) # Let new settings take affect.

sensor.set_auto_gain(False)

sensor.set_auto_whitebal(False) # turn this off.

clock = time.clock() # Tracks FPS.

# For color tracking to work really well you should ideally be in a very, very,

# very, controlled enviroment where the lighting is constant...

green_threshold =(63, 38, 15, -65, -11, -112)

red_threshold =(41, 13, -78, -13, -24, 72)

blue_threshold =(10, 36, -18, 17, -19, 13)

size_threshold = 10000

x_pid = PID(p=0.5, i=1, imax=100)

h_pid = PID(p=0.05, i=0.1, imax=50)

def find_max(blobs):

max_size=0

for blob in blobs:

if blob[2]*blob[3] > max_size:

max_blob=blob

max_size = blob[2]*blob[3]

return max_blob

# You may need to tweak the above settings for tracking green things...

# Select an area in the Framebuffer to copy the color settings.

while True:

if(uart.any()):

data = uart.readline()

print("reseived:",data)

if data.decode().startswith('a'):

while(True):

clock.tick() # Track elapsed milliseconds between snapshots().

img = sensor.snapshot() # Take a picture and return the image.

blob1 = img.find_blobs([green_threshold])

blob2 = img.find_blobs([red_threshold])

blob3 = img.find_blobs([blue_threshold])

if blob1:

max_blob = find_max(blob1)

x_error = max_blob[5]-img.width()/2

h_error = max_blob[2]*max_blob[3]-size_threshold

print("x error: ", x_error)

'''

for b in blobs:

# Draw a rect around the blob.

img.draw_rectangle(b[0:4]) # rect

img.draw_cross(b[5], b[6]) # cx, cy

'''

img.draw_rectangle(max_blob[0:4]) # rect

img.draw_cross(max_blob[5], max_blob[6]) # cx, cy

x_output=x_pid.get_pid(x_error,1)

h_output=h_pid.get_pid(h_error,1)

print("h_output",h_output)

car.run(-h_output-x_output,-h_output+x_output)

print("a")

elif blob2:

max_blob = find_max(blob2)

x_error = max_blob[5]-img.width()/2

h_error = max_blob[2]*max_blob[3]-size_threshold

print("x error: ", x_error)

'''

for b in blobs:

# Draw a rect around the blob.

img.draw_rectangle(b[0:4]) # rect

img.draw_cross(b[5], b[6]) # cx, cy

'''

img.draw_rectangle(max_blob[0:4]) # rect

img.draw_cross(max_blob[5], max_blob[6])

uart.write("b")

time.sleep(1000)

uart.write("c")

print("10")

time.sleep(3000)

print("11")

#for i in range(100000):

car.run(30,-30)

elif blob3:

max_blob = find_max(blob3)

#x_error = max_blob[5]-img.width()/2

#h_error = max_blob[2]*max_blob[3]-size_threshold

print("x error: ", x_error)

'''

for b in blobs:

# Draw a rect around the blob.

img.draw_rectangle(b[0:4]) # rect

img.draw_cross(b[5], b[6]) # cx, cy

'''

#img.draw_rectangle(max_blob[0:4]) # rect

#img.draw_cross(max_blob[5], max_blob[6])

print("T")

car.run(0,0)

break

else:

car.run(30,-30)

print("b")

else:

car.run(0,0)

else:

car.run(0,0)

2018-08-19 14:10
I
网红小智智 星瞳科技

如何设定颜色阈值的问题,请看教程

2018-08-19 14:17
V
啊彬

但是我,一个颜色就不会,多个颜色就会

V
啊彬

@kidswong999 你帮我看下程序可以吗,我怕程序写漏了什么,非常感谢,急用

I
网红小智智 星瞳科技

我没懂你是什么意思,你的问题是什么?

2018-08-19 14:28
V
啊彬

@kidswong999 就是它老是别别的颜色识别进去,你可以帮我看下,程序那里出错了吗?需要怎么改

I
网红小智智 星瞳科技

请回答以下问题

你的代码要实现什么功能,应该有什么现象?

你的代码现在有什么故障,什么现象现象?

你认为是代码的哪里有问题?

程序的逻辑是否用流程图设计过?

你又做了什么来检查问题?

2018-08-19 14:41
V
啊彬

我觉得应该是阈值的问题,我那程序阈值的设置和调用大概是这样子,你帮我看下阈值这样设,对不对

green_threshold =(63, 38, 15, -65, -11, -112)

red_threshold =(41, 13, -78, -13, -24, 72)

blue_threshold =(10, 36, -18, 17, -19, 13)

blob1 = img.find_blobs([green_threshold])

blob2 = img.find_blobs([red_threshold])

blob3 = img.find_blobs([blue_threshold])

if blob1:

程序1

elif blob2:

程序2

elif blob3:

程序3

else:

程序4

2018-08-19 14:47
I
网红小智智 星瞳科技

你说的没错,可是你也没回答我的问题

2018-08-19 15:03
V
啊彬

@kidswong999 让它识别到不同的颜色,执行不同的运动,当收到a时,开启颜色识别,当检测到绿色,跟着绿色走,红色的话,让他发送一个b,延时后再发送一个c,蓝色的话先停止然后结束,因为a只发了一次,所以这时跳出判断后,就不会再进行颜色识别,检测绿色是,打印一个a,可以在终端看执行程序那部分,同理蓝色时打印10,11也是,代码没有错误,但是运行的时候,你没把绿色,蓝色放在摄像头里,它老是把别的颜色识别进去。你可以把程序烧进去看下,就很快知道怎么回事了

I
网红小智智 星瞳科技

你应该自己调试,你可以print 来看你的逻辑执行的过程