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