Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created December 4, 2019 06:00
Show Gist options
  • Save anmolj7/0c98a631f5a493f83e91727fad3a1d2c to your computer and use it in GitHub Desktop.
Save anmolj7/0c98a631f5a493f83e91727fad3a1d2c to your computer and use it in GitHub Desktop.
I actually worked as an intern for an IOT company for 2 weeks, and I created a red object tracker there, so, I just modified the code to control the mouse. The directions are a bit confusing, Like, if you move your hand up, it moves the cursor down.
#import requests This is a required module if you want to use your android's webcam.
import cv2
import numpy as np
import pyautogui
import mouse
from math import sqrt
prev_cord = None
sX, sY = pyautogui.size()
sX, sY = sX//2, sY//2
pyautogui.FAILSAFE = False
pyautogui.moveTo(sX, sY)
def calc_dist(x1, y1, x2, y2):
#Using the distance formula for the following.
return round(sqrt(abs(x1-x2)**2 + abs(y2-y1)**2))
def process_frame(frame):
global prev_cord
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#Red color
lower = np.array([0, 48, 80], dtype = "uint8")
upper = np.array([20, 255, 255], dtype = "uint8")
#x, y cordinates.
x, y = frame.shape[:2]
print(x, y)
Center = (x//2, y//2) #Center's cordinates.
red_mask = cv2.inRange(hsv_frame, lower, upper)
red = cv2.bitwise_and(frame, frame, mask=red_mask)
indices = np.where(red_mask != [0])
cordinates = zip(indices[0], indices[1])
red_cnts = cv2.findContours(red_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(red_cnts) > 0:
red_area = max(red_cnts, key=cv2.contourArea)
(xg, yg, wg, hg) = cv2.boundingRect(red_area)
CenterOfObj = [(xg+wg)//2, (yg+hg)//2]
#Making center of frame as 0, 0
CenterOfObj[0] -= Center[0]
CenterOfObj[1] -= Center[1]
print(f'The center of the object is {calc_dist(0, 0, CenterOfObj[0], CenterOfObj[1])} units away from center of the frame.')
if not prev_cord:
prev_cord = CenterOfObj
if prev_cord[0] > CenterOfObj[0]:
print('The object is moving North', end="")
if prev_cord[1] > CenterOfObj[1]:
print('-East.')
elif prev_cord[1] < CenterOfObj[1]:
print('-West.')
else:
print('')
elif prev_cord[0] < CenterOfObj[0]:
print('The object is moving South.', end="")
if prev_cord[1] > CenterOfObj[1]:
print('-East.')
elif prev_cord[1] < CenterOfObj[1]:
print('-West.')
else:
print('')
else:
if prev_cord[1] > CenterOfObj[1]:
print('The object is moving East.')
elif prev_cord[1] < CenterOfObj[1]:
print('The object is moving West.')
else:
print('The object is at Rest.')
dx = CenterOfObj[0] - prev_cord[0]
dy = CenterOfObj[1] - prev_cord[1]
mouse.move(dx*10, -dy*10)
print(f'The total displacement in x axis: {dx} units')
print(f'The total displacement in y axis: {dy} units')
cv2.rectangle(frame, (xg, yg), (xg+wg, yg+hg), (0, 255, 0), 2)
cv2.namedWindow('Frame', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Frame', 800, 1000)
cv2.imshow("Frame", frame)
cv2.namedWindow('Skin Mask', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Skin Mask', 600, 600)
cv2.imshow("Skin Mask", red)
#This is the code for the default webcam.
cap = cv2.VideoCapture(0)
prev_cord = None
while True:
_, frame = cap.read()
process_frame(frame)
key = cv2.waitKey(1)
if key == 27:
break
#This is the code if you want to use your android phone's camera as the source.
#You need to install the app IpWebCam from the playstore, start the server, and enter the url in the 'url' variable.
# url = "http://192.168.43.252:8080/shot.jpg"
# while True:
# img_resp = requests.get(url)
# img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
# img = cv2.imdecode(img_arr, -1)
# process_frame(img)
# # cv2.imshow("AndroCam", img)
# # cv2.namedWindow('AndroCam', cv2.WINDOW_NORMAL)
# # cv2.resizeWindow('AndroCam', 600, 600)
# key = cv2.waitKey(1)
# if key == 27:
# break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment