資源簡介
工程訓(xùn)練大賽openmv二維碼、色環(huán)、色塊識別 可通過串口與單片機通信實現(xiàn)openmv的任務(wù)切換
代碼片段和文件信息
?#?Untitled?-?By:?zxs?-?周日?9月?2?2018
import?sensor?image?time?lcd?jsonmathujson
from?pyb?import?LED
from?pyb?import?UART
led=LED(4)
obj?=?[[120][1012][2210][9911]]
threshold_index?=?0?#?0?for?red?1?for?green?2?for?blue
#?Color?Tracking?Thresholds?(L?Min?L?Max?A?Min?A?Max?B?Min?B?Max)
#?The?below?thresholds?track?in?general?red/green/blue?things.?You?may?wish?to?tune?them...
#(31?61?28?56?-4?42)
#(24?66?15?74?-63?71)
#(32?65?-32?-6?-3?21)
#(51?71?-21?-4?-35?-19)
thresholds?=?[(19?73?36?65?3?42)?#?generic_red_thresholds
??????????????(26?76?-50?-18?-12?33)?#?generic_green_thresholds
??????????????(24?77?-31?13?-50?-17)]?#?generic_blue_thresholds
objthresholds?=?[(47?80?20?46?-10?24)?#?generic_red_thresholds
????????????(65?82?-13?-9?-4?8)?#?generic_green_thresholds
????????????(58?72?1?5?-19?-9)]?#?generic_blue_thresholds
#(47?82?-26?-5?-7?7)
#(66?75?-20?-7?-3?1)
#(50?65?-1?13?-18?-1)
#(50?72?2?19?-30?-5)
graythreshold=[(100255)]
K=5000
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)?#?Special?128x160?framesize?for?LCD?Shield.
sensor.skip_frames(time?=?2000)
#sensor.set_auto_gain(False)?#?must?turn?this?off?to?prevent?image?washout...
uart?=?UART(3?115200)
uart.init(115200?bits=8?parity=None?stop=1)
#?比較兩個色塊大小的函數(shù)
def?compareBlob(blob1?blob2):
????#?這里我們選擇了pixels作為指標(biāo)比對二者的代碼
????#?你也可以換用其它指標(biāo)?例如?blob.area()
????tmp?=?blob1.pixels()?-?blob2.pixels()
????if?tmp?==?0:
????????return?0;
????elif?tmp?>?0:
????????return?1;
????else:
????????return?-1;
#串口字符串指令切換功能:
#???{“RGB_blocks“:“find_R“}???查找紅色色塊
#???{“RGB_blocks“:“find_G“}???查找綠色色塊
#???{“RGB_blocks“:“find_B“}???查找藍(lán)色色塊
#???{“RGB_blocks“:“find_RGB_blocks“}?查找最近色塊顏色
#???{“Code_block“:“find_Code“}??查找二維碼
#???{“RGB_circles“:“find_R“}???查找紅色色環(huán)
#???{“RGB_circles“:“find_G“}???查找綠色色環(huán)
#???{“RGB_circles“:“find_B“}???查找藍(lán)色色環(huán)
#???{“circle“:“RGB“}?查找當(dāng)前色環(huán)顏色
#???{“LED“:“ON“}?????打開LED
#???{“LED“:“OFF“}?????關(guān)閉LED
lcd.init()?#?Initialize?the?lcd?screen.
A=5
B=0
while(True):
????exsit=0;
????data=[]
????img?=?sensor.snapshot()
????img.lens_corr(1.0)?#?strength?of?1.8?is?good?for?the?2.8mm?lens.
????if(uart.any()):
????????time.sleep(200)
????????d=uart.read()
????????da=str(d“utf-8“)
????????jsobj=ujson.loads(da)
????????for?key?in?jsobj.keys():
????????????print(‘key:%s?value:%s‘%(keyjsobj.get(key)))
????????if(jsobj.get(“RGB_blocks“)==“find_R“):
????????????A=1
????????else:
????????????if(jsobj.get(“RGB_blocks“)==“find_G“):
?????????????????A=2
????????????else:
????????????????if(jsobj.get(“RGB_blocks“)==“find_B“):
?????????????????????A=3
????????????????else:
????????????????????if(jsobj.get(“RGB_blocks“)==“find_RGB_blocks“):
?????????????????????????A=4
????????????????????else:
????????????????????????if(jsobj.get(“Code_block“)==“find_Code“):
??????????????????????
評論
共有 條評論