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()
© 版权声明
- 本站永久网址:https://blog.ksmlc.cn/
- 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责
- 本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新
- 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长QQ:2760460838进行删除处理
THE END
暂无评论内容