from picamera import PiCamera
from time import sleep
import mediapipe
import cv2



def takePhoto(filename):
    camera = PiCamera()
    camera.start_preview()
    sleep(5)
    camera.capture(filename+'.jpg')
    camera.stop_preview()

def showPreview(time):
    camera.start_preview()
    sleep(time)
    camera.stop_preview()
    
def recogniseHand():
    #Use MediaPipe to draw the hand framework over the top of hands it identifies in Real-Time
    drawingModule = mediapipe.solutions.drawing_utils
    handsModule = mediapipe.solutions.hands

    #Use CV2 Functionality to create a Video stream and add some values
    cap = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')

#Add confidence values and extra settings to MediaPipe hand tracking. As we are using a live video stream this is not a static
#image mode, confidence values in regards to overall detection and tracking and we will only let two hands be tracked at the same time
#More hands can be tracked at the same time if desired but will slow down the system
    with handsModule.Hands(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7, max_num_hands=2) as hands:

#Create an infinite loop which will produce the live feed to our desktop and that will search for hands
         while True:
               ret, frame = cap.read()
           #Unedit the below line if your live feed is produced upsidedown
           #flipped = cv2.flip(frame, flipCode = -1)
           
           #Determines the frame size, 640 x 480 offers a nice balance between speed and accurate identification
               frame1 = cv2.resize(frame, (640, 480))
           
           #Produces the hand framework overlay ontop of the hand, you can choose the colour here too)
               results = hands.process(cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB))
           
           #In case the system sees multiple hands this if statment deals with that and produces another hand overlay
               if results.multi_hand_landmarks != None:
                  for handLandmarks in results.multi_hand_landmarks:
                      drawingModule.draw_landmarks(frame1, handLandmarks, handsModule.HAND_CONNECTIONS)
                  
                              
           #Below shows the current frame to the desktop 
               cv2.imshow("Frame", frame1);
               key = cv2.waitKey(1) & 0xFF
           
           #Below states that if the |q| is press on the keyboard it will stop the system
               if key == ord("q"):
                  break

    

