Python捕捉手指操作轨迹

Python捕捉手指操作轨迹

import cv2
import mediapipe as mp
import time
import numpy as np
import tkinter as tk
from tkinter import ttk
import threading
import webbrowser

class FingerTrackingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("手指追踪应用-KS-MLC_GUI")
        # self.root.iconbitmap("ksmlc.ico")

        self.label = tk.Label(root, text="选择功能:")
        self.label.pack(pady=10)

        self.functionality_var = tk.StringVar()
        self.functionality_var.set("手捏带痕迹")

        self.functionality_combobox = ttk.Combobox(root, textvariable=self.functionality_var, values=["手捏带痕迹", "手捏不带痕迹", "只显示手指框架"])
        self.functionality_combobox.pack(pady=10)

        self.start_button = tk.Button(root, text="开始", command=self.start_tracking)
        self.start_button.pack(pady=10)

        self.open_browser_button = tk.Button(root, text="打开开发者网站", command=self.open_browser)
        self.open_browser_button.pack(pady=10)

        self.cap = cv2.VideoCapture(0)
        self.my_hands = mp.solutions.hands
        self.hands = self.my_hands.Hands()
        self.mp_draw = mp.solutions.drawing_utils
        self.p_time = 0
        self.c_time = 0
        self.pinch_trace = []
        self.is_fullscreen = False

    def start_tracking(self):
        functionality = self.functionality_var.get()

        if functionality == "手捏带痕迹":
            threading.Thread(target=self.track_hand_pinch_with_trace).start()
        elif functionality == "手捏不带痕迹":
            threading.Thread(target=self.track_hand_pinch_without_trace).start()
        elif functionality == "只显示手指框架":
            threading.Thread(target=self.show_hand_framework).start()

        # 切换到全屏
        self.root.attributes('-fullscreen', self.is_fullscreen)
        self.is_fullscreen = not self.is_fullscreen

    def track_hand_pinch_with_trace(self):
        finger_count = 0
        while True:
            success, img = self.cap.read()
            img = cv2.flip(img, 1)

            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            result = self.hands.process(img_rgb)

            if result.multi_hand_landmarks:
                for hand_lms in result.multi_hand_landmarks:
                    for idx, lm in enumerate(hand_lms.landmark):
                        h, w, c = img.shape
                        cx, cy = int(lm.x * w), int(lm.y * h)

                        if idx == 8:
                            thumb_tip_x, thumb_tip_y = int(hand_lms.landmark[4].x * w), int(hand_lms.landmark[4].y * h)
                            index_tip_x, index_tip_y = cx, cy

                            distance = ((thumb_tip_x - index_tip_x) <span style="font-weight: bold;" data-type="strong"> 2 + (thumb_tip_y - index_tip_y) </span> 2) ** 0.5

                            if distance < 30:
                                self.pinch_trace.append((cx, cy))
                                finger_count += 1

                    self.mp_draw.draw_landmarks(img, hand_lms, self.my_hands.HAND_CONNECTIONS)

                if self.pinch_trace:
                    pts = np.array(self.pinch_trace, np.int32)
                    pts = pts.reshape((-1, 1, 2))
                    cv2.polylines(img, [pts], isClosed=False, color=(255, 0, 0), thickness=2)

            self.show_fps(img, finger_count)
            cv2.imshow("Image", img)

            if cv2.waitKey(1) == ord('q'):
                break

    def track_hand_pinch_without_trace(self):
        finger_count = 0
        while True:
            success, img = self.cap.read()
            img = cv2.flip(img, 1)

            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            result = self.hands.process(img_rgb)

            if result.multi_hand_landmarks:
                for hand_lms in result.multi_hand_landmarks:
                    for idx, lm in enumerate(hand_lms.landmark):
                        h, w, c = img.shape
                        cx, cy = int(lm.x * w), int(lm.y * h)

                        if idx == 8:
                            thumb_tip_x, thumb_tip_y = int(hand_lms.landmark[4].x * w), int(hand_lms.landmark[4].y * h)
                            index_tip_x, index_tip_y = cx, cy

                            distance = ((thumb_tip_x - index_tip_x) <span style="font-weight: bold;" data-type="strong"> 2 + (thumb_tip_y - index_tip_y) </span> 2) ** 0.5

                            if distance < 30:
                                cv2.circle(img, (cx, cy), 10, (255, 0, 0), -1)
                                finger_count += 1

                    self.mp_draw.draw_landmarks(img, hand_lms, self.my_hands.HAND_CONNECTIONS)

            self.show_fps(img, finger_count)
            cv2.imshow("Image", img)

            if cv2.waitKey(1) == ord('q'):
                break

    def show_hand_framework(self):
        finger_count = 0
        while True:
            success, img = self.cap.read()
            img = cv2.flip(img, 1)

            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            result = self.hands.process(img_rgb)

            if result.multi_hand_landmarks:
                for hand_lms in result.multi_hand_landmarks:
                    for idx, lm in enumerate(hand_lms.landmark):
                        h, w, c = img.shape
                        cx, cy = int(lm.x * w), int(lm.y * h)

                        if idx == 8:
                            finger_count += 1

                    self.mp_draw.draw_landmarks(img, hand_lms, self.my_hands.HAND_CONNECTIONS)

            self.show_fps(img, finger_count)
            cv2.imshow("Image", img)

            if cv2.waitKey(1) == ord('q'):
                break

    def open_browser(self):
        webbrowser.open("https://ksmlc.cn")

    def show_fps(self, img, finger_count):
        self.c_time = time.time()
        fps = 1 / (self.c_time - self.p_time)
        self.p_time = self.c_time

        cv2.putText(img, f"FPS: {int(fps)} | Finger Count: {finger_count}", (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

if __name__ == "__main__":
    root = tk.Tk()
    app = FingerTrackingApp(root)
    root.mainloop()

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容