【问题标题】:How to limit the keys pressed using pynput如何限制使用pynput按下的键
【发布时间】:2021-01-08 03:40:19
【问题描述】:

我已经编写了一个代码来使用面部检测来刺激游戏。我找到了脸部的中心,在它移动时,我按下了带有 pynput 库的键。该代码运行良好,但只要检测到多次按下键盘键的点的移动,这只是一个小问题。我想将按下的键限制为 1。

'''

import cv2
import numpy as np
from pynput.keyboard import Key, Controller
import time

keyboard = Controller()

wc = cv2.VideoCapture(0)
time.sleep(2)

for i in range(40):
    ret, img = wc.read()

img = cv2.flip(img,1)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
   cv2.rectangle(img, (x,y), (x + w, y + h), (255, 0, 0), 2)
centre = [int((x + w + x)/2), int((y + h + y)/2)]
    


faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
wc = cv2.VideoCapture(0)
    
# Read until video is completed
while(wc.isOpened()):
  # Capture frame-by-frame
  ret, img = wc.read()
  img = cv2.flip(img,1)
  if ret == True:
    
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x,y), (x + w, y + h), (255, 0, 0), 2)
    centre_new = [int((x + w + x)/2), int((y + h + y)/2)]
    cv2.circle(img, (centre_new[0], centre_new[1]), 0, (0,0,255), 5)
    
    if  centre_new[0] - centre[0] > 100 : 
        keyboard.press(Key.right)
        keyboard.release(Key.right)
        print('right')

    if  centre_new[0] - centre[0] < -100 : 
        keyboard.press(Key.left)
        keyboard.release(Key.left)
        print('left')

    if  centre_new[1] - centre[1] < -100 : 
        keyboard.press(Key.up)
        keyboard.release(Key.up)
        print('up') 
    
    if  centre_new[1] - centre[1] > 100 : 
        keyboard = Controller()
        keyboard.press(Key.down)
        keyboard.release(Key.down)
        print('down')

    # Display the resulting frame
    cv2.imshow('Face',img)

    # Press Q on keyboard to  exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  
  # Break the loop
  else: 
    break

# When everything done, release the video capture object
wc.release()
# Closes all the frames
cv2.destroyAllWindows()

'''

我得到这样的输出:

''' 向上 向上 向上 向上 向上 向上 向上 正确的 正确的 正确的 正确的 正确的 正确的 正确的 正确的 正确的 向上 向上 向上 向上 向上

'''

【问题讨论】:

  • 另请注意,将img = cv2.flip(img, 1) 放在if ret: 之后是有意义的

标签: python opencv computer-vision face-detection pynput


【解决方案1】:

定义一些主区域,面部应该返回以区分一个击键和另一个击键,并使用标志来观察它。这是你要找的吗?

keystroke_zone = 100
home_zone = keystroke_zone - 10  # or whatever smaller than 
is_home = True

while(wc.isOpened()):

    ...

    if is_home:
        if centre_new[0] - centre[0] > keystroke_zone : 
            keyboard.press(Key.right)
            keyboard.release(Key.right)
            print('right')
            is_home = False

        if centre_new[0] - centre[0] < -keystroke_zone : 
            keyboard.press(Key.left)
            keyboard.release(Key.left)
            print('left')
            is_home = False

        if centre_new[1] - centre[1] < -keystroke_zone : 
            keyboard.press(Key.up)
            keyboard.release(Key.up)
            print('up')
            is_home = False

        if centre_new[1] - centre[1] > keystroke_zone : 
            keyboard = Controller()
            keyboard.press(Key.down)
            keyboard.release(Key.down)
            print('down')
            is_home = False
    else:
        if abs(centre_new[0] - centre[0]) < home_zone or\
                abs(centre_new[1] - centre[1]) < home_zone:
            is_home = True

【讨论】:

  • 明白。谢谢。
猜你喜欢
  • 2021-04-19
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2021-04-29
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多