Skip to content

Instantly share code, notes, and snippets.

@rebane2001
Created September 27, 2018 19:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebane2001/9ffa846a468583463064ad1a8f2d86d8 to your computer and use it in GitHub Desktop.
Save rebane2001/9ffa846a468583463064ad1a8f2d86d8 to your computer and use it in GitHub Desktop.
Simple script to control your mouse pointer with a laser and a webcam (only works on Windows)
import cv2
import numpy as np
import win32api, win32con
import math
webcam_x = 640 #Set webcam resolution
webcam_y = 640
screen_x = 1280 #Set screen resolution
screen_y = 1024
cam = cv2.VideoCapture(0) #Pick a webcam (change 0 to something else if this doesn't work)
cam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0) #Turn off auto-exposure
cam.set(cv2.CAP_PROP_EXPOSURE, -5.0) #Set exposure extremely low
while True:
ret_val, img = cam.read()
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
laser_pos = cv2.minMaxLoc(grey)
x = math.floor(laser_pos[3][0]/webcam_x*screen_x)
y = math.floor(laser_pos[3][1]/webcam_y*screen_y)
win32api.SetCursorPos((x,y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment