#Using cvzone
import cvzone
from cvzone.FaceDetectionModule import FaceDetector
import cv2
import keyboard
from time import sleep

# Initialize the webcam
# '2' means the third camera connected to the computer, usually 0 refers to the built-in webcam
cap = cv2.VideoCapture(1)

# Initialize the FaceDetector object
# minDetectionCon: Minimum detection confidence threshold
# modelSelection: 0 for short-range detection (2 meters), 1 for long-range detection (5 meters)
detector = FaceDetector(minDetectionCon=0.5, modelSelection=0)

# Run the loop to continually get frames from the webcam
while True:
    # Read the current frame from the webcam
    # success: Boolean, whether the frame was successfully grabbed
    # img: the captured frame
    success, img = cap.read()

    img = cv2.flip(img, 1) #Flip the frame
    
    # Detect faces in the image
    # img: Updated image
    # bboxs: List of bounding boxes around detected faces
    img, bboxs = detector.findFaces(img, draw=False)

    # Check if any face is detected
    if bboxs:
        # Loop through each bounding box
        for bbox in bboxs:
            # bbox contains 'id', 'bbox', 'score', 'center'

            # ---- Get Data  ---- #
            center = bbox["center"]
            x, y, w, h = bbox['bbox']
            #score = int(bbox['score'][0] * 100)

            # ---- Draw Data  ---- #
            #cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
            #cvzone.putTextRect(img, f'{score}%', (x, y - 10))
            cvzone.cornerRect(img, (x, y, w, h))

            # Draw 2 straight lines one on the left side and one on the right side
            cv2.line(img,(150,0),(150,500),(0,0,255),2)
            cv2.line(img,(500,0),(500,500),(0,0,255),2)

            #if the x coordinate of the face is less than 150, then turn the car right
            if x < 150:
                keyboard.press('Left')
                keyboard.release('Left')
                #keyboard.send('Left')
                direction = 'left'
            elif (x+w) > 500:  #if the x coordinate of the face is greater than 500, then turn the car left
                keyboard.press('Right')
                keyboard.release('Right')
                #keyboard.send('Right')
                direction = 'right'
            else: #else go straight
                keyboard.release('Left')
                keyboard.release('Right')
                direction = 'straight'

            sleep(0.2)

            img = cv2.putText(img, "Direction: {}".format(direction),(200,20),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),2) #display the direction which the car is going


    # Display the image in a window named 'Image'
    cv2.imshow("Image", img)
    # Wait for 1 millisecond, and keep the window open
    cv2.waitKey(1)
