@kidswong999
# Untitled - By: Administrator - 周一 五月 13 2019
import sensor, image, time,pyb,utime
from pyb import UART
#初始化串口3
uart = UART(3,115200)
uart.init(115200,bits = 8,parity = None,stop = 1)
#初始化RGB
red_led = pyb.LED(1)
grean_led = pyb.LED(2)
blue_led = pyb.LED(3)
global flag_color #标志变量
flag_color = 0
global switch #标志变量
switch = 0
global timeout
timeout = 0
global start
start = 0
red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
green_color = (47, 64, -61, -48, 29, 51) # 一般情况下的绿色阈值
blue_color = (53 , 59 , 7 , 23 , -74 , -61) # 一般情况下的蓝色阈值
red_threshold = (30, 40, 40, 57, 18, 52)
green_threshold =(33, 47, -47, -28, 10, 36)
blue_threshold = (55, 59, 10, 22, -70, -65)
# 如果是红色,执行此函数
def doWithRed(img):
red_led.on()
grean_led.off()
blue_led.off()
uart.write("1")
# 如果是绿色,执行此函数
def doWithGreen(img):
grean_led.on()
red_led.off()
blue_led.off()
uart.write("2")
# 如果是蓝色,执行此函数
def doWithBlue(img):
blue_led.on()
red_led.off()
grean_led.off()
uart.write("3")
# 如果是红绿蓝三色,执行此函数
def RedAndGreenAndBlue(img):
uart.write("4")
i = 0
for i in range(5):
red_led.toggle()
blue_led.toggle()
grean_led.toggle()
time.sleep(100)
# 如果是二维码,执行此函数
def Qr_code(img):
blue_led.on()
red_led.on()
grean_led.on()
uart.write("5")
#判断单颜色中最大色块
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
def fun():
while(True):
global switch
switch = 1
if switch == 1:
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
sensor.skip_frames(30)
clock = time.clock()
global flag_color
flag_color = 1
global timeout
timeout = 1
global start
while(flag_color == 1): #先识别红色
sensor.skip_frames(30)
clock = time.clock()
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别红色开始运行时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
if flag_color == 1:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-1')
global timeout
timeout = 1
global flag_color
flag_color = 2
break
img = sensor.snapshot()
red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if red_blob:
max_blob = find_max(red_blob)
print(max_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithRed(img)
global flag_color
flag_color = 2
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 2): #识别绿色
sensor.skip_frames(30)
clock = time.clock()
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别绿色开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
if flag_color == 2:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-2')
global timeout
timeout = 1
global flag_color
flag_color = 3
break
green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if green_blob:
max_blob = find_max(green_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithGreen(img)
global flag_color
flag_color = 3
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 3): #识别蓝色
sensor.skip_frames(30)
clock = time.clock()
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别蓝色开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
if flag_color == 3:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write("-3")
global flag_color
flag_color = 4
global timeout
timeout = 1
break
blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if blue_blob:
max_blob = find_max(blue_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithBlue(img)
global flag_color
flag_color = 4
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 4): #识别红绿蓝
sensor.skip_frames(20)
clock = time.clock()
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别红绿蓝开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第五步,并向串口发送-4
if flag_color == 4:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write("-4")
global flag_color
flag_color = 5
global timeout
timeout = 1
global switch
switch = 2
break
img = sensor.snapshot()
red_blob = img.find_blobs([red_color], pixels_threshold=100, area_threshold=100, merge=True)
green_blob = img.find_blobs([green_color], pixels_threshold=100, area_threshold=100, merge=True)
blue_blob = img.find_blobs([blue_color], pixels_threshold=100, area_threshold=100, merge=True)
if red_blob and green_blob and blue_blob:
r_max_size=g_max_size=b_max_size=0
for r_blob,g_blob,b_blob in zip(red_blob,green_blob,blue_blob):
if r_blob[2]*r_blob[3] > r_max_size and g_blob[2]*g_blob[3] > g_max_size and b_blob[2]*b_blob[3] > b_max_size:
img.draw_rectangle(r_blob.rect())
img.draw_cross(r_blob.cx(), r_blob.cy()) # cx, cy
img.draw_rectangle(g_blob.rect())
img.draw_cross(g_blob.cx(), g_blob.cy()) # cx, cy
img.draw_rectangle(b_blob.rect())
img.draw_cross(b_blob.cx(), b_blob.cy()) # cx, cy
RedAndGreenAndBlue(img)
global flag_color
flag_color = 5
global timeout
timeout = 1
global switch
switch = 2
time.sleep(500)
if switch == 2:
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(30) # 修改sensor配置之后, 跳过30帧
sensor.set_auto_gain(False)
while(flag_color == 5): #识别二维码
if timeout == 1:
start = utime.ticks_ms() #记录当前二维码开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 10000: #如果超过10秒则跳出循环,并向串口发送-5
if flag_color == 5:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-5')
global timeout
timeout = 1
global flag_color
flag_color = 1
global switch
switch = 1
break
img = sensor.snapshot()
img.lens_corr(1.5)
for code in img.find_qrcodes():
print(code)
if code[4] == "hello":
Qr_code(img)
global flag_color
flag_color = 1
global switch
switch = 1
global timeout
timeout = 1
time.sleep(500)
if __name__=='__main__':
fun()我需要怎么操作才能出现“我第一次识别红色成功后第二次再识别屏幕的色彩变绿了好多”的现象?
sensor初始化应该放在一开始,而不应该循环重新设置。
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, 1500)#这里设置曝光时间@kidswong999 就放在fun这函数外面是吗
放在最开始,也就是import下面
@kidswong999 但是在while想改对于的参数呢
改参数和sensor初始化有什么关系?
@kidswong999 第一次识别到红色,然后继续识别图像以及卡主了
@13542749802 是不是需要重新初始化啊,我重新初始化就不会
和初始化没关系。感光元件只需要开机初始化一次。
@kidswong999 那为什么识别到红色,图像就停止了呢
@13542749802 你要提供你的代码。
@kidswong999
# Untitled - By: Administrator - 周一 五月 13 2019
import sensor, image, time,pyb,utime
from pyb import UART
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, 1500)#这里设置曝光时间
sensor.skip_frames(30)
clock = time.clock()
#初始化串口3
uart = UART(3,115200)
uart.init(115200,bits = 8,parity = None,stop = 1)
#初始化RGB
red_led = pyb.LED(1)
grean_led = pyb.LED(2)
blue_led = pyb.LED(3)
global flag_color #标志变量
flag_color = 0
global switch #标志变量
switch = 0
global timeout
timeout = 0
global start
start = 0
red_color = (32, 48, 54, 70, 31, 59) # 一般情况下的红色阈值
green_color = (47, 64, -61, -48, 29, 51) # 一般情况下的绿色阈值
blue_color = (53 , 59 , 7 , 23 , -74 , -61) # 一般情况下的蓝色阈值
red_threshold = (30, 40, 40, 57, 18, 52)
green_threshold =(33, 47, -47, -28, 10, 36)
blue_threshold = (55, 59, 10, 22, -70, -65)
# 如果是红色,执行此函数
def doWithRed(img):
red_led.on()
grean_led.off()
blue_led.off()
uart.write("1")
# 如果是绿色,执行此函数
def doWithGreen(img):
grean_led.on()
red_led.off()
blue_led.off()
uart.write("2")
# 如果是蓝色,执行此函数
def doWithBlue(img):
blue_led.on()
red_led.off()
grean_led.off()
uart.write("3")
# 如果是红绿蓝三色,执行此函数
def RedAndGreenAndBlue(img):
uart.write("4")
i = 0
for i in range(5):
red_led.toggle()
blue_led.toggle()
grean_led.toggle()
time.sleep(100)
# 如果是二维码,执行此函数
def Qr_code(img):
blue_led.on()
red_led.on()
grean_led.on()
uart.write("5")
#判断单颜色中最大色块
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
def fun():
while(True):
global switch
switch = 1
if switch == 1:
global flag_color
flag_color = 1
global timeout
timeout = 1
global start
if timeout == 1:
start = utime.ticks_ms() #记录当前识别红色开始运行时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第二步,并向串口发送-1
if flag_color == 1:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-1')
global timeout
timeout = 1
global flag_color
flag_color = 2
break
img = sensor.snapshot()
red_blob = img.find_blobs([red_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if red_blob:
max_blob = find_max(red_blob)
print(max_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithRed(img)
global flag_color
flag_color = 2
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 2): #识别绿色
if timeout == 1:
start = utime.ticks_ms() #记录当前识别绿色开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第三步,并向串口发送-2
if flag_color == 2:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-2')
global timeout
timeout = 1
global flag_color
flag_color = 3
break
green_blob = img.find_blobs([green_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if green_blob:
max_blob = find_max(green_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithGreen(img)
global flag_color
flag_color = 3
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 3): #识别蓝色
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别蓝色开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第四部,并向串口发送-3
if flag_color == 3:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write("-3")
global flag_color
flag_color = 4
global timeout
timeout = 1
break
blue_blob = img.find_blobs([blue_threshold], pixels_threshold=100, area_threshold=100, merge=True)
if blue_blob:
max_blob = find_max(blue_blob)
if(max_blob.w() > 30 and max_blob.h() > 20):
img.draw_rectangle(max_blob.rect())
img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy
doWithBlue(img)
global flag_color
flag_color = 4
global timeout
timeout = 1
time.sleep(500)
while(flag_color == 4): #识别红绿蓝
clock.tick()
if timeout == 1:
start = utime.ticks_ms() #记录当前识别红绿蓝开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 8000: #如果超过8秒则跳出该循环往下执行第五步,并向串口发送-4
if flag_color == 4:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write("-4")
global flag_color
flag_color = 5
global timeout
timeout = 1
global switch
switch = 2
break
img = sensor.snapshot()
red_blob = img.find_blobs([red_color], pixels_threshold=100, area_threshold=100, merge=True)
green_blob = img.find_blobs([green_color], pixels_threshold=100, area_threshold=100, merge=True)
blue_blob = img.find_blobs([blue_color], pixels_threshold=100, area_threshold=100, merge=True)
if red_blob and green_blob and blue_blob:
r_max_size=g_max_size=b_max_size=0
for r_blob,g_blob,b_blob in zip(red_blob,green_blob,blue_blob):
if r_blob[2]*r_blob[3] > r_max_size and g_blob[2]*g_blob[3] > g_max_size and b_blob[2]*b_blob[3] > b_max_size:
img.draw_rectangle(r_blob.rect())
img.draw_cross(r_blob.cx(), r_blob.cy()) # cx, cy
img.draw_rectangle(g_blob.rect())
img.draw_cross(g_blob.cx(), g_blob.cy()) # cx, cy
img.draw_rectangle(b_blob.rect())
img.draw_cross(b_blob.cx(), b_blob.cy()) # cx, cy
RedAndGreenAndBlue(img)
global flag_color
flag_color = 5
global timeout
timeout = 1
global switch
switch = 2
time.sleep(500)
if switch == 2:
#sensor.reset()
#sensor.set_pixformat(sensor.RGB565)
#sensor.set_framesize(sensor.QQVGA)
#sensor.skip_frames(30) # 修改sensor配置之后, 跳过30帧
#sensor.set_auto_gain(False)
while(flag_color == 5): #识别二维码
if timeout == 1:
start = utime.ticks_ms() #记录当前二维码开始识别时间
global timeout
timeout = 0
delta = utime.ticks_diff(utime.ticks_ms(), start)
print(delta)
if delta > 10000: #如果超过10秒则跳出循环,并向串口发送-5
if flag_color == 5:
blue_led.toggle()
red_led.toggle()
grean_led.toggle()
uart.write('-5')
global timeout
timeout = 1
global flag_color
flag_color = 1
global switch
switch = 1
break
img = sensor.snapshot()
img.lens_corr(1.5)
for code in img.find_qrcodes():
print(code)
if code[4] == "hello":
Qr_code(img)
global flag_color
flag_color = 1
global switch
switch = 1
global timeout
timeout = 1
time.sleep(500)
if __name__=='__main__':
fun()我运行你的代码没有停止
@kidswong999 这里不方便拍视频不然拍给你看
@13542749802 有QQ没我拍了发你
@kidswong999 要识别到才会帧缓冲区才会停止的
??????????????
你也有那种卡住的问题吗
你微信号多少,加你微信探讨一下。