Hand Track
Hand Track
/usr/bin/env python
# coding: utf-8
# In[37]:
import numpy as np
import cv2
import time
import math
import imutils
from imutils.video import FileVideoStream, VideoStream
import keyboard
from pynput.keyboard import Key, Controller
#keyboard=Controller()
import numpy as np
# In[38]:
size_height = 600
size_width = 600
last = 0 #last extreme of steer
chng = 3 #change in last, to remove small changes fulucations
chngt = 3 #to restart change to 0, if moved towards centre by some value
THRESH_ACTION = 60 # t
if p1[0] < 10: #quits program if taken hand to left top corner
q = True # quit
# In[39]:
color = {"blue":(255,0,0), "red":(0,0,255), "green":(0,255,0), "white":
(255,255,255)}
#keyboard.press(cmd)
return cmd
def keyboard_events_r(rcord,cord_right, cmd2):
try:
x1,x2 = cord_right
#y1,y2 = cord_left
#xl,yl=lcord
xr,yr=rcord
except Exception as e:
print(e)
return
if xr < x1:
cmd2 = "a"
elif(xr > x2):
cmd2 = "d"
#elif(yl<y1):
# cmd = "up"
#elif(yl > y2):
# cmd = "down"
if cmd2:
print("Detected another movement: ", cmd2, "\n")
keyboard.press(cmd2)
#elif cmd=="up":
# keyboard.press(cmd2)
#elif cmd=="down":
# keyboard.press(cmd2)
#keyboard.press(cmd)
return cmd2
def reset_press_flag(lcord,rcord,cord_left,cord_right,cmd,cmd2):
try:
x1,x2 = cord_right
y1,y2 = cord_left
xl,yl=lcord
xr,yr=rcord
#xc, yc = nose_cords
except:
return True,cmd,cmd2
if x1<xr<x2 or y1<yl<y2:
if x1<xr<x2 and (cmd2!=None and cmd2!=""):
keyboard.release(cmd2)
if y1<yl<y2 and (cmd!=None and cmd!=""):
keyboard.release(cmd)
if x1<xr<x2 and y1<yl<y2:
#keyboard.release(cmd)
return True,None,None
elif x1<xr<x2:
return True,cmd,None
elif y1<yl<y2:
return True,None,cmd2
return False,cmd,cmd2
# In[40]:
while True:
frame = get_frame()
curr = (time.time() - t)
if curr > TIMER_SETUP:
break
cv2.putText(frame, str(int(TIMER_SETUP - curr)+1), (225,255),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, COLOR_RED, 4)
cv2.imshow("Setup", frame)
cv2.waitKey(1)
FRAME = frame.copy()
cv2.destroyAllWindows()
frame = FRAME.copy()
cv2.putText(frame, 'Select Right Hand for acceleration', (30,30),
cv2.FONT_HERSHEY_SIMPLEX, 0.75, COLOR_RED, 2)
bboxright = cv2.selectROI(frame, False) # bounding box for right hand
#centleft = get_centroid(bboxleft)
#centright = get_centroid(bboxright)
cv2.destroyAllWindows()
#fvs.release()
# In[41]:
#BBR
'''
cv2.destroyAllWindows()
'''
# In[ ]:
# In[42]:
press_flag = False
cmd = ""
cmd2= ""
while True:
frame = get_frame()
#if curr > TIMER_SETUP or frame is None:
# break
#cv2.putText(frame, str(int(TIMER_SETUP - curr)+1), (225,255),
cv2.FONT_HERSHEY_SIMPLEX, 1.5, COLOR_RED, 4)
if BBL is not None:
# grab the new bounding box coordinates of the object
(success, box) = trackerleft.update(frame)
(success1, box1) = trackerright.update(frame)
# check to see if the tracking was a success
if success:
(x, y, w, h) = [int(v) for v in box]
cv2.rectangle(frame, (x, y), (x + w, y + h),
(0, 255, 0), 2)
(a,b,c,d)= [int(f) for f in box1]
cv2.rectangle(frame, (a, b), (a + c, b + d),
(0, 255, 0), 2)
cv2.imshow("Tracking", frame)
if cv2.waitKey(1)==13:
break
cv2.destroyAllWindows()
fvs.release()
# In[43]:
#fvs.release()
# In[44]:
#BBL