Min Ma
Created December 1, 2023

face location as virtual input

Using face movement as virtual input like joystick in game.

7
face location as virtual input

Things used in this project

Hardware components

Intel RealSense Camera
Intel RealSense Camera
×1

Software apps and online services

OpenCV
OpenCV

Story

Read more

Code

face location

Python
recongnize face and get position.
# coding: utf-8
import cv2
capture = cv2.VideoCapture(0)
facecascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyecascade = cv2.CascadeClassifier("haarcascade_eye.xml")
oldX=0
oldY=0
while(capture.isOpened()):
    retval,image = capture.read()
    #eyes = eyecascade.detectMultiScale(image,1.15)
    faces = facecascade.detectMultiScale(image,1.15)
    
    #for (x,y,w,h) in eyes:
    #    cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),5)
    for (x,y,w,h) in faces:
        cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),5)
        nowX = x+w/2
        nowY = y+h/2
        range = (oldX-nowX)*(oldX-nowX)+(oldY-nowY)*(oldY-nowY)
        if range >20:
            h = nowX-oldX
            v = nowY-oldY
            print(oldX,oldY,h,v,range)
        else:
            h = 0
            v = 0
        oldX = nowX
        oldY = nowY
    
    cv2.imshow("video",image)
    key = cv2.waitKey(1)
    if key == 32:
        break
    if key == 97:
        cv2.imwrite("face.jpg",image)
capture.release()
cv2.destroyAllWindows()

Credits

Min Ma

Min Ma

8 projects • 1 follower
Senior Software Engineer

Comments