我的创作纪念日

文章作者分享了在实战项目中关于颜色识别和二维码检测的经验,通过Python代码展示了如何使用传感器和图像处理技术来识别红、绿、蓝三种颜色以及解析二维码信息。同时,文章体现了作者在学习过程中的思考和交流,以及参与比赛的经历。
摘要由CSDN通过智能技术生成

机缘

  1. 实战项目中的经验分享
  2. 日常学习过程中的记录
  3. 通过文章进行技术交流
  4. 记录自己的学习过程

收获

学到了更多的知识,也有了一个平台去发泄自己的想法


日常

一直在考研途中,工创赛也结束了。


 
import sensor, image, time,pyb
import ustruct
from pyb import Pin
from pyb import UART
from pyb import LED
 
left=0
middle=0
right=0
red_threshold=(0, 69, 78, 53, 19, 121)#红色颜色阈值
green_threshold=(73, 11, -66, -34, 33, -20)#绿色颜色阈值(100, 9, -74, -23, 29, -128)
blue_threshold=(89, 39, -96, 44, -118, -42)#蓝色颜色阈值(7, 42, -4, 52, -89, 3)
flag_color=2
red_color_code=1
green_color_code=2
blue_color_code=4
black_color_code=8
 
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_whitebal(False)
sensor.set_auto_gain(False)
 
while(True):
    if uart.any():
        data=uart.readline()
        print(data)
        led1.on()
        if (data==b'\xa1'):
           at=0
           while(at==0):
               led2.on()
 
               sensor.set_framesize(sensor.VGA)
               sensor.set_windowing((300, 300))
               img = sensor.snapshot()
               for code in img.find_qrcodes():
                   if(code.payload()=='红色蓝色'):
                       at=1
                   if(code.payload()=='蓝色红色'):
                       at=2
                   if(code.payload()=='红色绿色'):
                       at=3
                   if(code.payload()=='绿色红色'):
                       at=4
                   if(code.payload()=='蓝色绿色'):
                       at=5
                   if(code.payload()=='绿色蓝色'):
                       at=6
                   sending_data(at)
                   print(at)
                   led2.off()
                   flag_color=0
        if(data==b'\xa2'):
            sensor.set_framesize(sensor.QVGA)
            sensor.set_auto_whitebal(False)
            sensor.set_auto_gain(False)
            zhong=0
            #p_out.high()
            led2.on()
            led3.on()
            while(zhong==0):
                clock.tick()
                img = sensor.snapshot()
                img.draw_rectangle((20,110,70,64),color=(0x00,0x00,0x00))
                img.draw_rectangle((114,110,70,64),color=(0x00,0x00,0x00))
                img.draw_rectangle((215,110,78,64),color=(0x00,0x00,0x00))
 
                blob_left=img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200,merge=True,roi=(10,110,70,67))
                blob_middle=img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200,merge=True,roi=(114,110,70,64))
                blob_right=img.find_blobs([red_threshold,green_threshold,blue_threshold],area_threshold=200,merge=True,roi=(215,110,70,66))
 
                left=0
                middle=0
                right=0
 
                if blob_left:
                    for blob in blob_left:
                        led3.on()
                        x=blob[0]
                        y=blob[1]
                        width=blob[2]
                        height=blob[3]
                        center_x=blob[5]
                        center_y=blob[6]
                        color_code =blob[8]
 
                        if color_code==red_color_code:
                            img.draw_string(x,y-10,"red",color=(0xFF,0x00,0x00))
                            left=1
 
                        elif color_code==green_color_code:
                            img.draw_string(x,y-10,"green",color=(0x00,0xFF,0x00))
                            left=2
                        elif color_code==blue_color_code:
                            img.draw_string(x,y-10,"blue",color=(0x00,0x00,0xFF))
                            left=3
                        else:
                            left=0
                        color=color_code
                        img.draw_rectangle([x,y,width,height])
                        img.draw_cross(center_x,center_y)
 
                if blob_middle:
                    for blob in blob_middle:
                        x=blob[0]
                        y=blob[1]
                        width=blob[2]
                        height=blob[3]
                        center_x=blob[5]
                        center_y=blob[6]
                        color_code =blob[8]
 
                        if color_code==red_color_code:
                            img.draw_string(x,y-10,"red",color=(0xFF,0x00,0x00))
                            middle=1
 
                        elif color_code==green_color_code:
                            img.draw_string(x,y-10,"green",color=(0x00,0xFF,0x00))
                            middle=2
                        elif color_code==blue_color_code:
                            img.draw_string(x,y-10,"blue",color=(0x00,0x00,0xFF))
                            middle=3
                        else:
                            middle=0
                        color=color_code
                        img.draw_rectangle([x,y,width,height])
                        img.draw_cross(center_x,center_y)
 
                if blob_right:
                    for blob in blob_right:
                        x=blob[0]
                        y=blob[1]
                        width=blob[2]
                        height=blob[3]
                        center_x=blob[5]
                        center_y=blob[6]
                        color_code =blob[8]
 
                        if color_code==red_color_code:
                            img.draw_string(x,y-10,"red",color=(0xFF,0x00,0x00))
                            right=1
 
                        elif color_code==green_color_code:
                            img.draw_string(x,y-10,"green",color=(0x00,0xFF,0x00))
                            right=2
                        elif color_code==blue_color_code:
                            img.draw_string(x,y-10,"blue",color=(0x00,0x00,0xFF))
                            right=3
                        else:
                            right=0
                        color=color_code
                        img.draw_rectangle([x,y,width,height])
                        img.draw_cross(center_x,center_y)
                        led2.off()
                        led3.off()
 
 
                if left !=0 and middle !=0 and right !=0 and left != middle and middle != right and left != right :
                    led2.on()
                    if left ==1 and middle ==2 and right ==3 :
                        print("%d%d%d" % (left,middle,right))
                        colour1 = [left,middle,right]
                        sending_data1(colour1)
                        print(colour1)
                        break
 
 

憧憬

希望能上岸电子科大

Tips

  1. 您发布的文章将会展示至 里程碑专区 ,您也可以在 专区 内查看其他创作者的纪念日文章
  2. 优质的纪念文章将会获得神秘打赏哦
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值