如何在python中反转光标移动? [英] How can I invert the cursor movement in python?

查看:180
本文介绍了如何在python中反转光标移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

如果您知道屏幕尺寸,只需从屏幕尺寸中减去现在的尺寸即可将光标移到另一侧. 例如:

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

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

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)

这篇关于如何在python中反转光标移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆