翻譯|使用教程|編輯:黃竹雯|2019-06-10 14:20:52.040|閱讀 662 次
概述:
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
如果你想快速創建一個步行機器人,你可以使用Lego Boost(機器人編程入門神器)。在這篇文章中,小編將分享如何使用Webcam(網絡攝像頭),Lego Boost和Dynamsoft Barcode Reader SDK來制作用于掃描條形碼的機器人。本文中使用的編程語言是Python。
要構建機器人,請安裝Boost app并按照相關教程進行操作。
下載Dynamsoft Barcode Reader for Linux。解壓縮包后,將libDynamsoftBarcodeReader.so 復制 到 / usr / lib。
獲取Dynamsoft Barcode SDK的30天免費試用許可證。
獲取 Dynamsoft Python條形碼模塊的,并按照步驟進行構建和安裝。
安裝一個藍牙后端:
pip install pygatt pip install gatt pip install gattlib pip install bluepy
安裝pylgbst與Lego Boost Move Hub進行交互:
pip install //github.com/undera/pylgbst/archive/1.0.tar.gz
安裝OpenCV Python:
pip install opencv-python # or pip3 install opencv-python
使用OpenCV創建攝像機視圖窗口:
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)全局鎖,我們需要在另一個進程而不是線程中運行藍牙連接代碼和條形碼解碼算法。所有數據(包括關鍵事件,網絡攝像頭幀和條形碼結果)都通過隊列傳輸:
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()
以下是關鍵定義:
控制樂高升力機器人的代碼非常簡單。小編用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碼并將結果放入隊列:
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繪制條形碼位置和解碼文本結果:
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)
運行app
python3 app.py
購買Dynamsoft Barcode Reader正版授權的朋友可以點擊""哦~~~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn