Search code examples
pythonwindowsopencvpyautogui

How can I invert the cursor movement in python?


In this code, I'm using Python 2.7.13, OpenCV 2.4.13 and PyAutoGUI 0.9.36. The objective is to move the cursor according to the facial movement, but the cursor movement is inverted. For example, if my face goes to right, the cursor moves to left and if my face goes to left, the cursor goes to right. Also, I want the cursor to move right, left, up and down in the whole screen of my PC, whose size is x=1920, y=1080.

The purpose of this program is to show that it is possible to get a new way to acquire more independence and access so that people with tetraplegia are capable of doing the simple activities, which are part of the routine of millions of individuals, such as turning the light on and off and turning TV on and off.

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()

Solution

  • If you know the screen size, just subtract what you have now from the screen size to get the cursor on the opposite side. For example:

    pyautogui.moveTo(1920 - (x+w), 1080 - (y+h))
    

    If x+w was getting you a screen position of 2 (left of screen), it would now get you a screen position of 1918 (right of screen)