如何在python中反转光标移动?

Tig*_*erC 13 python windows opencv pyautogui

在这段代码中,我使用的是Python 2.7.13,OpenCV 2.4.13和PyAutoGUI 0.9.36.目标是根据面部运动移动光标,但光标移动被反转.例如,如果我的脸向右移动,则光标向左移动,如果我的脸向左移动,则光标向右移动.此外,我希望光标在我的PC的整个屏幕中向右,向左,向上和向下移动,其大小为x = 1920,y = 1080.

该计划的目的是表明有可能获得一种获得更多独立性和获取途径的新方法,以便患有四肢瘫痪的人能够进行简单的活动,这些活动是数百万人的常规活动的一部分,例如转动打开和关闭灯,打开和关闭电视.

import cv2
import pyautogui

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=5,
        minSize=(80, 80),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    #print 'faces: ', faces

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 3)

    #width, height = pyautogui.size()
    #cursorx, cursory = pyautogui.position()
    #posx = width - cursorx
    #posy = cursory
    pyautogui.moveTo(x+w, y+h)

    # Display the resulting frame
    #cv2.imshow('Video', frame)
    rimg = cv2.flip(frame,1) #invert the object frame
    cv2.imshow("vertical flip", rimg) 

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

小智 2

如果您知道屏幕尺寸,只需从屏幕尺寸中减去现在的尺寸即可使光标位于另一侧。例如:

pyautogui.moveTo(1920 - (x+w), 1080 - (y+h))
Run Code Online (Sandbox Code Playgroud)

如果 x+w 为您提供的屏幕位置为 2(屏幕左侧),那么现在它将为您提供的屏幕位置为 1918(屏幕右侧)