파이썬 에어 마우스 스크립트(손 추적 포함)
Wikiprompt, 무료 프롬프트 백과사전에서
파이썬 에어 마우스 스크립트(손 추적 포함) 웹캠을 손 추적을 통해 에어 마우스로 변환하는 Python 스크립트를 생성하기 위한 프롬프트입니다. 커서 제어와 클릭 제스처 기능을 포함합니다.
프롬프트 내용저장
🌐
import cv2
import mediapipe as mp
import pyautogui
import math
# Sensitivity settings - adjust these values
SMOOTHING = 0.5 # Higher = smoother but slower response (0.1 to 1.0)
SCREEN_WIDTH, SCREEN_HEIGHT = pyautogui.size() # Get screen size
CAM_WIDTH, CAM_HEIGHT = 640, 480 # Webcam resolution
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7, min_tracking_confidence=0.5)
mp_draw = mp.solutions.drawing_utils
# Initialize webcam
cap = cv2.VideoCapture(0)
cap.set(3, CAM_WIDTH)
cap.set(4, CAM_HEIGHT)
# Cursor position variables for smoothing
prev_x, prev_y = 0, 0
cursor_x, cursor_y = SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2
# Pinch detection thresholds (adjust if needed)
PINCH_DISTANCE = 0.04 # Normalized distance for pinch detection
# State variables
paused = False
left_clicked = False
right_clicked = False
def get_distance(point1, point2):
"""Calculate Euclidean distance between two points"""
return math.sqrt((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2)
while True:
success, frame = cap.read()
if not success:
break
# Flip frame horizontally for mirror effect
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process hand detection
result = hands.process(rgb_frame)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
# Draw hand landmarks on frame
mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Get key landmarks
index_tip = hand_landmarks.landmark[8] # Index finger tip
thumb_tip = hand_landmarks.landmark[4] # Thumb tip
middle_tip = hand_landmarks.landmark[12] # Middle finger tip
wrist = hand_landmarks.landmark[0] # Wrist
# Check for open palm (all fingers extended)
finger_tips = [8, 12, 16, 20] # Index, middle, ring, pinky tips
extended_count = 0
for tip_id in finger_tips:
tip = hand_landmarks.landmark[tip_id]
pip = hand_landmarks.landmark[tip_id - 2] # PIP joint
if tip.y < pip.y: # Finger is extended
extended_count += 1
# Open palm detection - pause tracking
if extended_count >= 4:
paused = True
cv2.putText(frame, "PAUSED", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
else:
paused = False
if not paused:
# Map index finger position to screen coordinates
# Invert X for natural movement (mirror effect)
target_x = (1 - index_tip.x) * SCREEN_WIDTH
target_y = index_tip.y * SCREEN_HEIGHT
# Apply smoothing to cursor movement
cursor_x = prev_x + (target_x - prev_x) * SMOOTHING
cursor_y = prev_y + (target_y - prev_y) * SMOOTHING
# Move cursor
pyautogui.moveTo(cursor_x, cursor_y)
# Update previous positions
prev_x, prev_y = cursor_x, cursor_y
# Calculate pinch distances
index_thumb_dist = get_distance(index_tip, thumb_tip)
middle_thumb_dist = get_distance(middle_tip, thumb_tip)
# Left click - index finger + thumb pinch
if index_thumb_dist < PINCH_DISTANCE and not left_clicked:
pyautogui.click(button='left')
left_clicked = True
cv2.putText(frame, "LEFT CLICK", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
elif index_thumb_dist >= PINCH_DISTANCE:
left_clicked = False
# Right click - middle finger + thumb pinch
if middle_thumb_dist < PINCH_DISTANCE and not right_clicked:
pyautogui.click(button='right')
right_clicked = True
cv2.putText(frame, "RIGHT CLICK", (10, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
elif middle_thumb_dist >= PINCH_DISTANCE:
right_clicked = False
# Display the frame
cv2.imshow("Air Mouse", frame)
# Exit on 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Cleanup
cap.release()
cv2.destroyAllWindows()
전체 프롬프트를 보려면 로그인하세요
Continue with:
By logging in, you agree to our Terms of Use and Privacy Policy
사용법
이 프롬프트는 coding와 함께 사용하도록 설계되었습니다. 위의 프롬프트 내용을 복사하여 원하는 AI 도구에 붙여넣으세요.
최상의 결과를 얻으려면 자리 표시자(대괄호 또는 대문자로 표시)를 특정 요구 사항으로 사용자 지정할 수 있습니다.
토론
댓글 0개