python移动光标位置,如何反转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()

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值