tello-手掌控制無人機

 tello-手掌控制無人機



實驗流程

  • 使用cvzone找出手掌並能分辨手指頭數目
  • 用條件式控制       1-前進 //2-後退 //3-左偏航 //4-右偏航

使用到的函式庫

pycharm IDE之下安裝

先找出手和節點

from cvzone.HandTrackingModule import HandDetector
import cv2
from time import sleep

#先建立影像視窗
cap = cv2.VideoCapture(0)
detector = HandDetector()
while True:
success, img = cap.read()
detector.findHands(img)
lmList, bboxInfo = detector.findPosition(img)
print(bboxInfo)
cv2.imshow("img", img)
cv2.waitKey(1000)


算出手指數

在程式碼裡加上以下檢測手指數並在畫面上印出手指數
if lmList:
bbox = bboxInfo['bbox']
fingers = detector.fingersUp()
total = fingers.count(1)
cv2.putText(img, f'Fingers: {total}', (bbox[0], bbox[1]-30),
cv2.FONT_HERSHEY_COMPLEX,1, (255,0,0))
print(total)

然後寫手指數1,2,3,4,5,0的條件式

if totalFingers == 1:
mytello.send_rc_control(0, 20, 0, 0)
elif totalFingers == 2:
mytello.send_rc_control(0, -20, 0, 0)
elif totalFingers == 3:
mytello.send_rc_control(20, 0, 0, 0)
elif totalFingers == 4:
mytello.send_rc_control(-20, 0, 0, 0)
elif totalFingers == 5:
mytello.send_rc_control(0, 0, 0, 0)
elif totalFingers == 0:
mytello.send_rc_control(0, 0, 0, 0)
print(totalFingers)

和tello控制合併一起後的完整程式碼

from cvzone.HandTrackingModule import HandDetector
import cv2
from djitellopy import tello
from time import sleep

mytello = tello.Tello()
mytello.connect()
mytello.takeoff()
mytello.streamon()
print("電池電量: {}".format(mytello.get_battery()))
mytello.send_rc_control(0, 0, 25, 0)
sleep(2)
# cap = cv2.VideoCapture(0)
# cap.set(3, 680)
# cap.set(4, 480)
detector = HandDetector(detectionCon=0.5, maxHands=1, minTrackCon=0.5)
t = 0
while True:
#Get image frame
# success, img = cap.read()
img = mytello.get_frame_read().frame
img = cv2.resize(img, (600, 400))
# Find the hand and its landmarks
img = detector.findHands(img)
lmList, bboxInfo = detector.findPosition(img)
if lmList:
bbox = bboxInfo['bbox']
fingers = detector.fingersUp()
totalFingers = fingers.count(1)
if totalFingers == 1:
mytello.send_rc_control(0, 20, 0, 0)
elif totalFingers == 2:
mytello.send_rc_control(0, -20, 0, 0)
elif totalFingers == 3:
mytello.send_rc_control(20, 0, 0, 0)
elif totalFingers == 4:
mytello.send_rc_control(-20, 0, 0, 0)
elif totalFingers == 5:
mytello.send_rc_control(0, 0, 0, 0)
elif totalFingers == 0:
mytello.send_rc_control(0, 0, 0, 0)
print(totalFingers)
cv2.putText(img, f'Fingers:{totalFingers}', (bbox[0] + 200, bbox[1] - 30),
cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
elif bboxInfo == []:
mytello.send_rc_control(0, 0, 0, 0)
print(t)
# Display
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
# break
mytello.land()
# cap.release()
# cv2.destroyAllWindows()


















留言

這個網誌中的熱門文章

初體驗tello-大疆無人機用python控制前進/後退/左/右飛