openmv識別二維碼,顏色識別與串列埠通訊
阿新 • • 發佈:2020-12-11
技術標籤:機器視覺
openmv識別二維碼,顏色識別與串列埠通訊
目的
1.給二維碼指定內容:“xxx+xxx”,識別並返回及傳送對應序號到串列埠;
2.根據二維碼的內容按順序識別物料顏色
3.待機時亮起綠燈,執行動作時,亮起藍燈,動作結束後關閉LED。
二維碼識別
while(True):
img = sensor.snapshot()
img.lens_corr(1.8)
for code in img.find_qrcodes():
output_str="%s" % code. payload()
#output_str為二維碼內容
附(草料二維碼生成器)
顏色識別
#顏色閾值
green_threshold = (73, 96, -79, -22, -128, 127)
red_threshold = (41, 61, 42, 127, -128, 127)
blue_threshold = (22, 67, 9, 127, -128, -54)
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[0:4]) # rect
#用矩形標記出目標顏色區域
img.draw_cross(b[5], b[6]) # cx, cy
#在目標顏色區域的中心畫十字形標記
print(b[8])#b[8]為顏色代號,red=2,green=1,blue=4
串列埠通訊
if uart.any():#判斷是否接收到資料
getrx = uart.readline()#讀取資料
uart.write('abc')#傳送資料
完整程式碼
import sensor, image, time, pyb
import ujson
from pyb import UART
green_threshold = (73, 96, -79, -22, -128, 127)
#(0, 100, -128, -25, -128, 127)
red_threshold = (41, 61, 42, 127, -128, 127)
#(41, 61, 42, 127, -128, 127)
blue_threshold = (22, 67, 9, 127, -128, -54)
#(15, 100, -128, 127, -128, -41)
#red=2,green=1,blue=4
getrx = 0
getcmd = 0
renum = 0
colornum = 0
ptrposition = 0
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() # 追蹤幀率
led = pyb.LED(1) # Red LED = 1, Green LED = 1, Blue LED = 2, IR LEDs = 4.
uart = UART(3, 115200, timeout_char = 1000)
led.on()
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() # Track elapsed milliseconds between snapshots().
if uart.any():
led.off()
getrx = uart.readline()
time.sleep(150) #延時150ms
led = pyb.LED(2)
led.on()
getcmd = int(getrx)
print(getcmd)
# print(img.find_qrcodes())
img = sensor.snapshot()# 從感光晶片獲得一張影象
img.lens_corr(1.8) # strength of 1.8 is good for the 2.8mm lens.
blobs = img.find_blobs([green_threshold,red_threshold,blue_threshold],x_stride=25,y_stride=50,area_threshold=1000)
if(getcmd==2):
for code in img.find_qrcodes():
output_str="%s" % code.payload() #方式1
renum = int(Rec_NUM1(output_str)*10 + Rec_NUM2(output_str))
uart.write(ujson.dumps(renum))
getcmd = 0
led.off()
if blobs and getcmd==3:
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
#用矩形標記出目標顏色區域
img.draw_cross(b[5], b[6]) # cx, cy
#在目標顏色區域的中心畫十字形標記
#print(b[5], b[6], b[8])
#uart.write(ujson.dumps(b[8]))
#將顏色序號轉為比賽序號
if b[8]==1:
colornum=2
elif b[8]==2:
colornum=1
elif b[8]==4:
colornum=3
print('colornum=',colornum,'output_str[ptrposition]=',output_str[ptrposition])
#若任務碼對應
if (int(output_str[ptrposition]) == colornum):
uart.write('t')
ptrposition+=1
if ptrposition==4:
ptrposition+=1
getcmd=0