import cv2
import mediapipe as mp
import subprocess

previous_digit = None
current_digit = None
frame_count = 0
current_process = None

def switch_task():
    print(f"Switching task based on gesture: {current_digit}")
    play_song(current_digit)

def play_song(song_number):
    global current_process
    if current_process is not None:
        # 如果上一首歌曲还在播放, 终止它
        current_process.terminate()

    mp3_file = f"/home/root/Music/{song_number}.mp3"
    play_command = ["gst-play-1.0", "-q", mp3_file]

    current_process = subprocess.Popen(play_command)

# 打开摄像头，并设置分辨率和帧率
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
cap.set(cv2.CAP_PROP_FPS, 10)

# 设置保存视频的编解码器和帧率
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 10.0

# 创建 VideoWriter 对象，用于保存视频
out = cv2.VideoWriter('output.avi', fourcc, fps, (640, 480))

# 创建 MediaPipe 的手部检测器
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5, min_tracking_confidence=0.5)


# 定义一个帧计数器和一个标志变量
count = 0
detect_flag = False

# 循环读取摄像头的每一帧
while count < 200:
    ret, frame = cap.read()
    if not ret:
        break

    print('*', end='', flush=True)

    # 将当前帧转换为 RGB 格式，并调整大小
    frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
    frame = cv2.resize(frame, (640, 480))

    imgWidth = frame.shape[1]
    imgHeight = frame.shape[0]

    fingerCount = 0
    # 在当前帧中检测手部，并获取手势识别结果
    results = hands.process(frame)
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # 获取手势识别结果
            handIndex = results.multi_hand_landmarks.index(hand_landmarks)
            handLabel = results.multi_handedness[handIndex].classification[0].label

            # Set variable to keep landmarks positions (x and y)
            handLandmarks = []

            # Fill list with x and y positions of each landmark
            for landmarks in hand_landmarks.landmark:
                handLandmarks.append([landmarks.x, landmarks.y])

            # for j,lm in enumerate(hand_landmarks.landmark):
            #     xPos = int(lm.x * imgWidth)
            #     yPos = int(lm.y * imgHeight)
            #     cv2.putText(frame, str(j), (xPos - 25, yPos + 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
            #     if j == 4: # 大拇指
            #         cv2.circle(frame, (xPos,yPos), 10, (0, 0, 255), cv2.FILLED)

            # 检测每个手指，如果手指竖起来，计数值加一
            # 拇指: TIP x 坐标必须逼IP x 坐标小
            if handLabel == "Left" and handLandmarks[4][0] > handLandmarks[3][0]:
                fingerCount = fingerCount+1
            elif handLabel == "Right" and handLandmarks[4][0] < handLandmarks[3][0]:
                fingerCount = fingerCount+1

            # 其他手指: TIP y 坐标必须 PIP y 坐标小
            # 所有坐标都是以图片左上角为基础
            if handLandmarks[8][1] < handLandmarks[6][1]:       # 食指
                fingerCount = fingerCount + 1
            if handLandmarks[12][1] < handLandmarks[10][1]:     # 中指
                fingerCount = fingerCount + 1
            if handLandmarks[16][1] < handLandmarks[14][1]:     # 无名指
                fingerCount = fingerCount + 1
            if handLandmarks[20][1] < handLandmarks[18][1]:     # 小拇指
                fingerCount = fingerCount + 1
            
            # 在当前帧中标注出关键点和连接线
            mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
            detect_flag = True

            # 在屏幕上显示手势识别结果
            cv2.putText(frame, str(fingerCount), (50, 450), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 10)        
            print(fingerCount, end='', flush=True)
            if fingerCount == current_digit and fingerCount > 0:
                frame_count += 1
            else:
                current_digit = fingerCount
                frame_count = 1

            if frame_count == 3:
                if current_digit != previous_digit and current_digit != 0:
                    switch_task()
                    previous_digit = current_digit
                frame_count = 0
    
    # 将带标注的当前帧转换为 BGR 格式，并写入视频文件
    if detect_flag:
        out.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
    else:
        out.write(frame)

    # 更新帧计数器
    count += 1

    # 将带标注的当前帧显示在屏幕上
    # cv2.imshow('Hand Gesture Recognition', frame)
    # if cv2.waitKey(1) & 0xFF == ord('q'):
    #     break

# 释放摄像头，关闭所有窗口
cap.release()
out.release()
hands.close()
# cv2.destroyAllWindows()