如何使用OpenCV和Python通过检测到的面部和眼睛移动鼠标 [英] How can I move mouse by detected face and Eye using OpenCV and Python

查看:148
本文介绍了如何使用OpenCV和Python通过检测到的面部和眼睛移动鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过绘制周围的眼睛来检测面部和眼睛,并且在Python教程 Python教程& 学习Opencv .现在,我想让鼠标(光标)在脸部移动"和眼睛闭合/睁开"动作时移动. (PYTHON和OPENCV).请帮忙给我更多有关如何使它起作用的想法. 谢谢.

I have managed to detect face and eyes by drawing cycles around them and it works fine with the help of Python tutorials Python tutorial & Learn Opencv. Now I would like to make the mouse (Cursor) moves when Face moves and Eyes close/open to do mouse clicking. (PYTHON & OPENCV). Please help to give me more ideas on how I can make it works. Thanks.

检查以下代码:

import cv2
import sys
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
video_capture = cv2.VideoCapture(0)
while True:
    ret, img = video_capture.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    Face = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in Face:
         cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
         roi_gray = gray[y:y+h, x:x+w]
         roi_color = img[y:y+h, x:x+w]
         eyes = eye_cascade.detectMultiScale(roi_gray)
         for (ex,ey,ew,eh) in eyes:
             cv2.rectangle(roi_color,(ex,ey), (ex+ew,ey+eh), (0,255,0), 2)
   # Display the resulting frame
   cv2.imshow('Face and Eye Detected', img)
   if cv2.waitKey(1) & 0xFF == ord('q'):
        break
#cv2.waitKey(0)
video_capture.release()
cv2.destroyAllWindows()    

推荐答案

如果您在Windows环境中工作,则需要的是python win32api中的SetCursorPos方法.使用:

If you're working in Windows environment, what you're looking for is the SetCursorPos method in the python win32api. Use:

import win32api
import cv2
import sys
...
for (ex,ey,ew,eh) in eyes:
    win32api.SetCursorPos((ex,ey))
    cv2.rectangle(roi_color,(ex,ey), (ex+ew,ey+eh), (0,255,0), 2)

这会将光标设置到矩形的左上角顶点.如果希望将光标移动到矩形的中心,请使用:

This will set the cursor to the top-left vertex of the rectangle. If you wish to move the cursor to the center of the rect, use:

win32api.SetCursorPos((ex+ew/2,ey+eh/2))

这篇关于如何使用OpenCV和Python通过检测到的面部和眼睛移动鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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