-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gesture Control Video Player using OepnCV.py
165 lines (132 loc) · 5.7 KB
/
Gesture Control Video Player using OepnCV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
@author: NISHANT
"""
#-----------------------------------------------------------------------------
#Step - 1 -Import Libraries and capture camera
#Step - 2 -Convert frames Into hsv
#Step - 3 -Track hand on color basis
#Step - 4 -Create mask on the basis of color and filter actual color
#Step - 5 -Invert pixel value and then enchance the result for better output
#Step - 6 -Find Contours for specific colored object
#Step - 7 -Find Max area contour and draw it on live feed
#Step - 8 -Find Convexity detect for counting Values and Apply Cosin method
#Step - 9 -Bind hand gestures with keyboard keys.
#Step -10 -Enjoy your output
#-----------------Gesture Control Media Player---------------------------
#Step -1
import cv2
import numpy as np
import math
import pyautogui as p
import time as t
#Read Camera
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
def nothing(x):
pass
#window name
cv2.namedWindow("Color Adjustments",cv2.WINDOW_NORMAL)
cv2.resizeWindow("Color Adjustments", (300, 300))
cv2.createTrackbar("Thresh", "Color Adjustments", 0, 255, nothing)
#COlor Detection Track
cv2.createTrackbar("Lower_H", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_S", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Lower_V", "Color Adjustments", 0, 255, nothing)
cv2.createTrackbar("Upper_H", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_S", "Color Adjustments", 255, 255, nothing)
cv2.createTrackbar("Upper_V", "Color Adjustments", 255, 255, nothing)
while True:
_,frame = cap.read()
frame = cv2.flip(frame,2)
frame = cv2.resize(frame,(600,500))
# Get hand data from the rectangle sub window
cv2.rectangle(frame, (0,1), (300,500), (255, 0, 0), 0)
crop_image = frame[1:500, 0:300]
#Step -2
hsv = cv2.cvtColor(crop_image, cv2.COLOR_BGR2HSV)
#detecting hand
l_h = cv2.getTrackbarPos("Lower_H", "Color Adjustments")
l_s = cv2.getTrackbarPos("Lower_S", "Color Adjustments")
l_v = cv2.getTrackbarPos("Lower_V", "Color Adjustments")
u_h = cv2.getTrackbarPos("Upper_H", "Color Adjustments")
u_s = cv2.getTrackbarPos("Upper_S", "Color Adjustments")
u_v = cv2.getTrackbarPos("Upper_V", "Color Adjustments")
#Step -3
lower_bound = np.array([l_h, l_s, l_v])
upper_bound = np.array([u_h, u_s, u_v])
#Step - 4
#Creating Mask
mask = cv2.inRange(hsv, lower_bound, upper_bound)
#filter mask with image
filtr = cv2.bitwise_and(crop_image, crop_image, mask=mask)
#Step - 5
mask1 = cv2.bitwise_not(mask)
m_g = cv2.getTrackbarPos("Thresh", "Color Adjustments") #getting track bar value
ret,thresh = cv2.threshold(mask1,m_g,255,cv2.THRESH_BINARY)
dilata = cv2.dilate(thresh,(3,3),iterations = 6)
#Step -6
#findcontour(img,contour_retrival_mode,method)
cnts,hier = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
try:
#print("try")
#Step -7
# Find contour with maximum area
cm = max(cnts, key=lambda x: cv2.contourArea(x))
#print("C==",cnts)
epsilon = 0.0005*cv2.arcLength(cm,True)
data= cv2.approxPolyDP(cm,epsilon,True)
hull = cv2.convexHull(cm)
cv2.drawContours(crop_image, [cm], -1, (50, 50, 150), 2)
cv2.drawContours(crop_image, [hull], -1, (0, 255, 0), 2)
#Step - 8
# Find convexity defects
hull = cv2.convexHull(cm, returnPoints=False)
defects = cv2.convexityDefects(cm, hull)
count_defects = 0
#print("Area==",cv2.contourArea(hull) - cv2.contourArea(cm))
for i in range(defects.shape[0]):
s,e,f,d = defects[i,0]
start = tuple(cm[s][0])
end = tuple(cm[e][0])
far = tuple(cm[f][0])
#Cosin Rule
a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = (math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) * 180) / 3.14
#print(angle)
# if angle <= 50 draw a circle at the far point
if angle <= 50:
count_defects += 1
cv2.circle(crop_image,far,5,[255,255,255],-1)
print("count==",count_defects)
#Step - 9
# Print number of fingers
if count_defects == 0:
cv2.putText(frame, " ", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255),2)
elif count_defects == 1:
p.press("space")
cv2.putText(frame, "Play/Pause", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
elif count_defects == 2:
p.press("up")
cv2.putText(frame, "Volume UP", (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
elif count_defects == 3:
p.press("down")
cv2.putText(frame, "Volume Down", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
elif count_defects == 4:
p.press("right")
cv2.putText(frame, "Forward", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,0,255), 2)
else:
pass
except:
pass
#step -10
cv2.imshow("Thresh", thresh)
#cv2.imshow("mask==",mask)
cv2.imshow("filter==",filtr)
cv2.imshow("Result", frame)
key = cv2.waitKey(25) &0xFF
if key == 27:
break
cap.release()
cv2.destroyAllWindows()