logo

列表索引超出范围

2019-02-23 18:51
V
-

import sensor, image, time

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QVGA)

sensor.skip_frames(time=200)

sensor.set_auto_gain(False)

sensor.set_auto_whitebal(False)

clock=time.clock()

threshold=[(23, 73, 19, 93, 8, 54),(70, 27, -71, -18, -21, 41), (32, 61, -66, 58, -99, -58)] #红色 绿色 蓝色

red_threshold=[(23, 73, 19, 93, 8, 54)]

green_threshold=[(70, 27, -71, -18, -21, 41)]

blue_threshold=[(32, 61, -66, 58, -99, -58)]

while(True):

clock.tick()

img=sensor.snapshot()

for blob in img.find_blobs(threshold,pixels_threshold=400,area_threshold=400):

img.draw_rectangle(blob.rect())

img.draw_cross(blob.cx(),blob.cy())

red_blobs=img.find_blobs(red_threshold)

green_blobs=img.find_blobs(green_threshold)

blue_blobs=img.find_blobs(blue_threshold)

red_blob=red_blobs[0]

green_blob=green_blobs[0]

blue_blob=blue_blobs[0]

redX=red_blob.cx()

greenX=green_blob.cx()

blueX=blue_blob.cx()

if(redX>=greenX):

if(redX<=200):

print('red',redX)

else:

print('green',greenX)

5f6fd3a1-2449-4830-9b92-e200976482db.png
I
网红小智智 星瞳科技

你没找到色块的时候

red_blobs就是空的。

red_blobs[0]就错误了。

2019-02-23 20:13
V
-

@kidswong999 如何解决呢?

2019-02-24 13:15
I
网红小智智 星瞳科技
if red_blobs:
    red_blob = red_blobs[0]

但是可能没有red_blob的时候,又会出现red_blob未定义。

这个逻辑你应该自己解决。