import cv2

# Import numpy for matrices calculations
import numpy as np

import os 
import ardrone

drone = ardrone.ARDrone()
from PIL import Image

def assure_path_exists(path):
    dir = os.path.dirname(path)
    if not os.path.exists(dir):
        os.makedirs(dir)

# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()

assure_path_exists("trainer/")

# Load the trained mode
recognizer.read('trainer/trainer.yml')

# Load prebuilt model for Frontal Face
cascadePath = "face.xml"

# Create classifier from prebuilt model
faceCascade = cv2.CascadeClassifier(cascadePath);
def twillo():
    


    # Your Account Sid and Auth Token from twilio.com/console
    account_sid = 'AC12446ff63d16f5749cf9d3d59c0465dc'
    auth_token = '77672a18715f646025afea018ef1094d'
    client = Client(account_sid, auth_token)

    message = client.messages \
                    .create(
                         body="harsha, Male chicago.",
                         from_='+1(312) 471-0558',
                         to='+13123588613'
                     )

    print(message.sid)
# Loop
# Set the font style
font = cv2.FONT_HERSHEY_SIMPLEX

x=1
# Loop
while True:
    # Read the video frame
    frame = drone.image
    open_cv_image =np.array(frame)

    im =open_cv_image[:,:,::-1].copy()

    # Convert the captured frame into grayscale
    gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

    # Get all face from the video frame
    faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)

    # For each face in faces
    for(x,y,w,h) in faces:

        # Create rectangle around the face
        cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)

        # Recognize the face belongs to which ID
        Id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
        print(Id)
        # Check the ID if exist 
        if(Id == 1):
	    
            if(x==1):
		
                twillo()
                x=2
            print("hi")
            Id = "harsha {0:.2f}%".format(round(100 - confidence, 2))
        elif(Id == 2):
            print("hi")
            Id = "b {0:.2f}%".format(round(100 - confidence, 2))

        # Put text describe who is in the picture
        cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
        cv2.putText(im, str(Id), (x,y-40), font, 1, (255,255,255), 3)

    # Display the video frame with the bounded rectangle
    cv2.imshow('im',im) 

    # If 'q' is pressed, close program
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

drone.halt()
cv2.destroyAllWindows()


