翻譯|使用教程|編輯:黃竹雯|2019-06-10 14:20:52.040|閱讀 662 次
概述:
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
如果你想快速創(chuàng)建一個(gè)步行機(jī)器人,你可以使用Lego Boost(機(jī)器人編程入門神器)。在這篇文章中,小編將分享如何使用Webcam(網(wǎng)絡(luò)攝像頭),Lego Boost和Dynamsoft Barcode Reader SDK來制作用于掃描條形碼的機(jī)器人。本文中使用的編程語(yǔ)言是Python。
要構(gòu)建機(jī)器人,請(qǐng)安裝Boost app并按照相關(guān)教程進(jìn)行操作。
下載Dynamsoft Barcode Reader for Linux。解壓縮包后,將libDynamsoftBarcodeReader.so 復(fù)制 到 / usr / lib。
獲取Dynamsoft Barcode SDK的30天免費(fèi)試用許可證。
獲取 Dynamsoft Python條形碼模塊的,并按照步驟進(jìn)行構(gòu)建和安裝。
安裝一個(gè)藍(lán)牙后端:
pip install pygatt pip install gatt pip install gattlib pip install bluepy
安裝pylgbst與Lego Boost Move Hub進(jìn)行交互:
pip install //github.com/undera/pylgbst/archive/1.0.tar.gz
安裝OpenCV Python:
pip install opencv-python # or pip3 install opencv-python
使用OpenCV創(chuàng)建攝像機(jī)視圖窗口:
vc = cv2.VideoCapture(0) vc.set(3, 640) #set width vc.set(4, 480) #set height if vc.isOpened(): # try to get the first frame rval, frame = vc.read() else: return windowName = "Robot View" try: while True: rval, frame = vc.read() cv2.imshow(windowName, frame)
由于GIL(Python Global Interpreter Lock)全局鎖,我們需要在另一個(gè)進(jìn)程而不是線程中運(yùn)行藍(lán)牙連接代碼和條形碼解碼算法。所有數(shù)據(jù)(包括關(guān)鍵事件,網(wǎng)絡(luò)攝像頭幀和條形碼結(jié)果)都通過隊(duì)列傳輸:
num = Value('i', 1) result_queue = Queue(1) key_queue = Queue(1) frame_queue = Queue(1) cond = Condition() dbr_proc = Process(target=dbr_run, args=( frame_queue, key_queue, cond, num, result_queue)) dbr_proc.start()
以下是關(guān)鍵定義:
控制樂高升力機(jī)器人的代碼非常簡(jiǎn)單。小編用Gatt Backend:
conn GattConnection() try: conn.connect() hub = MoveHub(conn) print('Robot connected') speed = 0.5 if key == ord('a'): # left hub.motor_AB.angled(90, speed * -1, speed) elif key == ord('d'): # right hub.motor_AB.angled(90, speed, speed * -1) elif key == ord('w'): # up hub.motor_AB.start_speed(speed) elif key == ord('s'): # down hub.motor_AB.start_speed(speed * -1) elif key == ord('p'): hub.motor_AB.stop() finally: conn.disconnect()
閱讀QR碼并將結(jié)果放入隊(duì)列:
conn GattConnection() dbr.initLicense('LICENSE-KEY') if key == ord('c'): inputframe = frame_queue.get() results = dbr.decodeBuffer(inputframe, 0x4000000) if (len(results) > 0): for result in results: print("Type: " + result[0]) print("Value: " + result[1] + "\n") result_queue.put(Result(inputframe, results))
使用OpenCV繪制條形碼位置和解碼文本結(jié)果:
ret = result_queue.get_nowait() results = ret.results image = ret.image thickness = 2 color = (0,255,0) for result in results: print("barcode format: " + result[0]) print("barcode value: " + result[1]) x1 = result[2] y1 = result[3] x2 = result[4] y2 = result[5] x3 = result[6] y3 = result[7] x4 = result[8] y4 = result[9] cv2.line(image, (x1, y1), (x2, y2), color, thickness) cv2.line(image, (x2, y2), (x3, y3), color, thickness) cv2.line(image, (x3, y3), (x4, y4), color, thickness) cv2.line(image, (x4, y4), (x1, y1), color, thickness) cv2.putText(image, result[1], (min([x1, x2, x3, x4]), min([y1, y2, y3, y4])), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), thickness) cv2.imshow("Localization", image)
運(yùn)行app
python3 app.py
購(gòu)買Dynamsoft Barcode Reader正版授權(quán)的朋友可以點(diǎn)擊""哦~~~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn