DJI Tello won't follow me

37 Views Asked by At

I am trying to use a code for a dji tello drone for face tracking, which I got from https://github.com/Shreyas-dotcom/DJITello_FaceTracking/blob/main/Code%3A%20V2. I had modified it for my needs and the drone flies, streams and everything expect it does not follow mw as I need it to do. Can anyone see why the code is not working for me?

This is the code:

import cv2
import numpy as np
from djitellopy import tello
import time

me = tello.Tello()
me.connect()
# Getting the drones battery
print(me.get_battery())

me.streamon()
me.takeoff()
me.send_rc_control(0, 0, 0, 0)
time.sleep(4.6)
w, h = 360, 240
fbRange = [6200, 6800]
 pid = [0.4, 0.4, 0]
 pError = 0

def findFace(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(imgGray, 1.2, 8)
myFaceListC = []
myFaceListArea = []

for (x, y, w, h) in faces:

    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cx = x + w // 2
    cy = y + h // 2
    area = w * h
    cv2.circle(img, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
    myFaceListC.append([cx, cy])
    myFaceListArea.append(area)

    command1 = x
    command2 = y
    command3 = w
    command4 = h

    if command1 > 0:
        print("Drone Movement: Right")
    else:
        print("Drone Movement: Left")
    if command2 > 0:
        print("Drone Movement: Forward")
    else:
        print("Drone Movement: Backward")
    if command3 > 0:
        print(" Drone Movement: Up")
    else:
        print("Drone Movement: Down")
    if command4 > 0:
        print("Drone Movement: Yaw Right")
    else:
        print("Drone Movement: Yaw Left")


  if len(myFaceListArea) != 0:
    i = myFaceListArea.index(max(myFaceListArea))

    return img, [myFaceListC[i], myFaceListArea[i]]
   else:
    return img, [[0, 0], 0]


 def trackFace( info, w, pid, pError):
 area = info[1]
 x, y = info[0]
 fb = 0
 error = x - w // 2
 speed = pid[0] * error + pid[1] * (error - pError)
speed = int(np.clip(speed, -100, 100))
if area > fbRange[0] and area < fbRange[1]:
    fb = 0
elif area > fbRange[1]:
    fb = -20
elif area < fbRange[0] and area != 0:
    fb = 20
if x == 0:
    speed = 0
    error = 0
print(speed, fb)
me.send_rc_control(0, fb, 0, speed)
return error

cap = cv2.VideoCapture(1)

while True:

_, img = cap.read()

img = me.get_frame_read().frame
img = cv2.resize(img, (w, h))
img, info = findFace(img)
pError = trackFace( info, w, pid, pError)
#print(“Center”, info[0], “Area”, info[1])
cv2.imshow('person', img)

if cv2.waitKey(1) & 0xFF == ord('q'):
    me.land()
    break
0

There are 0 best solutions below