ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

openmv识别二维码,颜色识别

2021-11-30 22:05:34  阅读:279  来源: 互联网

标签:elif set return lista 白平衡 二维码 识别 sensor openmv


最近在学习openmv,顺便写点东西,也算是自己积累一下,不多说直接上代码

二维码识别并返回值

import sensor, image, time, pyb
import ujson
from pyb import UART


sensor.reset()# 初始化摄像头
sensor.set_pixformat(sensor.RGB565)# 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。
clock = time.clock() # 追踪帧率

uart = UART(3, 115200)


def Rec_NUM1(lista):
    if (lista[0]=='1' and lista[1]=='2' and lista[2]=='3'):
        return 1
    elif (lista[0]=='1' and lista[1]=='3' and lista[2]=='2'):
        return 2
    elif (lista[0]=='2' and lista[1]=='1' and lista[2]=='3'):
        return 3
    elif (lista[0]=='2' and lista[1]=='3' and lista[2]=='1'):
        return 4
    elif (lista[0]=='3' and lista[1]=='1' and lista[2]=='2'):
        return 5
    elif (lista[0]=='3' and lista[1]=='2' and lista[2]=='1'):
        return 6

def Rec_NUM2(lista):
    if (lista[4]=='1' and lista[5]=='2' and lista[6]=='3'):
        return 1
    elif (lista[4]=='1' and lista[5]=='3' and lista[6]=='2'):
        return 2
    elif (lista[4]=='2' and lista[5]=='1' and lista[6]=='3'):
        return 3
    elif (lista[4]=='2' and lista[5]=='3' and lista[6]=='1'):
        return 4
    elif (lista[4]=='3' and lista[5]=='1' and lista[6]=='2'):
        return 5
    elif (lista[4]=='3' and lista[5]=='2' and lista[6]=='1'):
        return 6
while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.8) # 1.8的强度参数对于2.8mm镜头来说是不错的。
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color = (255, 0, 0))#使用红色矩阵将二维码圈起来
        output_str="%s" % code.payload() #方式1
        renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
        uart.write(ujson.dumps(renum))
        print(renum)
        print(code.payload())#将二维码数据打印出来

颜色识别

import sensor, image, time, pyb
import ujson
import ustruct
from pyb import UART
import time
green_threshold   = (73, 96, -79, -22, -128, 127)  #1
#(0, 100, -128, -25, -128, 127)
red_threshold   = (41, 61, 42, 127, -128, 127)     #2
#(41, 61, 42, 127, -128, 127)
blue_threshold   = (22, 67, 9, 127, -128, -54)     #4


sensor.reset()# 初始化摄像头
sensor.set_pixformat(sensor.RGB565)# 格式为 RGB565.
sensor.set_framesize(sensor.QQVGA) # 使用 QQVGA 速度快一些
sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡
sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。
sensor.set_auto_whitebal(False)
#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。
clock = time.clock() # 追踪帧率

uart = UART(3, 115200)

def sending_data(color,qr):
    global uart;
    #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
    #data = bytearray(frame)
    data = ustruct.pack("<bbhhb",      #格式为俩个字符俩个短整型(2字节)
                   0x2C,                      #帧头1
                   0x12,                      #帧头2
                   int(color), # up sample by 4   #数据1
                   int(qr),
                   0x5B)
    uart.write(data);

while(True):
    clock.tick()
    img = sensor.snapshot()
    img.lens_corr(1.8) # 1.8的强度参数对于2.8mm镜头来说是不错的。
    blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
    for b in blobs:
        img.draw_rectangle(b.rect()) # rect
         #用矩形标记出目标颜色区域
        img.draw_cross(b.cx(), b.cy())
        print(b.code())
        sending_data(b.code())

        time.sleep(1)

都是看完别的大佬,然后掉掉掉,哈哈哈哈

标签:elif,set,return,lista,白平衡,二维码,识别,sensor,openmv
来源: https://blog.csdn.net/weixin_62793941/article/details/121643838

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有