ZPPR/main.py

67 lines
1.5 KiB
Python

import cv2
import numpy
import pynput.mouse
def openCamera(input):
cv2.imshow("frame", input)
key = cv2.waitKey(1)
return key
def drawBox(finder, frame, b, g, r):
for x, y, w, h in finder:
cv2.rectangle(frame, (x, y), (x + w, y + h) ,(b, g, r), 2)
def exit(video):
video.release()
cv2.destroyAllWindows()
mouse = pynput.mouse.Controller()
video = cv2.VideoCapture(0)
frame = video.read()
findFistInit = None
centerX = 0
centerY = 0
while True:
cascadeHand = cv2.CascadeClassifier("hand.xml")
cascadeFist = cv2.CascadeClassifier("fist.xml")
frame = video.read()[1]
fmask = cv2.GaussianBlur(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), (21,21), 0)
cv2.imshow("fmask", fmask)
findFist = cascadeFist.detectMultiScale(fmask, scaleFactor = 1.1, minNeighbors = 75)
if findFistInit is None:
findFistInit = findFist
if findFist == ():
findHand = cascadeHand.detectMultiScale(fmask, scaleFactor = 1.1, minNeighbors = 10)
if findHand != ():
mouse.click(pynput.mouse.Button.left, 1)
if findFist != () and findFistInit != ():
dx, dy, dw, dh = findFist[0][0] - findFistInit[0][0], findFist[0][1] - findFistInit[0][1], findFist[0][2] - findFistInit[0][2], findFist[0][3] - findFistInit[0][3]
centerX = (dx + dx + dw) / 2
centerY = (dy + dy + dh) / 2
mouse.move( -1 * 7 * dx, 7 * dy)
findFistInit = findFist
drawBox(findFist, frame, 0, 0, 255)
drawBox(findHand, frame, 0, 255, 0)
frame = cv2.flip(frame, 1)
key = openCamera(frame)
if(key == ord('q')):
break
exit(video)