22import  numpy  as  np 
33import  copy 
44import  math 
5+ import  time 
6+ from  picamera  import  PiCamera 
7+ from  picamera .array  import  PiRGBArray 
8+ import  requests 
59#from appscript import app 
610
711# Environment: 
@@ -61,14 +65,25 @@ def calculateFingers(res,drawing): # -> finished bool, cnt: finger count
6165
6266
6367# Camera 
64- camera  =  cv2 .VideoCapture (0 )
65- camera .set (10 ,200 )
68+ # camera = cv2.VideoCapture(0) 
69+ # camera.set(10,200) 
70+ FRAME_THRESHOLD  =  5 
6671cv2 .namedWindow ('trackbar' )
6772cv2 .createTrackbar ('trh1' , 'trackbar' , threshold , 100 , printThreshold )
6873
69- 70- while  camera .isOpened ():
71-  ret , frame  =  camera .read ()
74+ camera  =  PiCamera ()
75+ camera .resolution  =  (640 , 480 )
76+ camera .framerate  =  32 
77+ rawCapture  =  PiRGBArray (camera , size = (640 , 480 ))
78+ time .sleep (0.1 )
79+ 80+ session  =  requests .session ()
81+ fcnt  =  0 
82+ last_cnt  =  - 1 
83+ for  frame  in  camera .capture_continuous (rawCapture , format = 'bgr' , use_video_port = True ):
84+  # ret, frame = camera.read() 
85+  frame  =  frame .array 
86+  rawCapture .truncate (0 )
7287 threshold  =  cv2 .getTrackbarPos ('trh1' , 'trackbar' )
7388 frame  =  cv2 .bilateralFilter (frame , 5 , 50 , 100 ) # smoothing filter 
7489 frame  =  cv2 .flip (frame , 1 ) # flip the frame horizontally 
@@ -111,10 +126,21 @@ def calculateFingers(res,drawing): # -> finished bool, cnt: finger count
111126 cv2 .drawContours (drawing , [hull ], 0 , (0 , 0 , 255 ), 3 )
112127
113128 isFinishCal ,cnt  =  calculateFingers (res ,drawing )
129+  if (isFinishCal  and  cnt  ==  last_cnt ):
130+  fcnt += 1 
131+  if (fcnt >= FRAME_THRESHOLD ):
132+  fcnt = 0 
133+  action_dictionary  =  {1 : 'toggle_play' , 2 : 'stop' , 3 : 'next_track' }
134+  action  =  action_dictionary .get (cnt , 'toggle_play' )
135+  print ("{} emitted" .format (action ))
136+  session .get ('http://0.0.0.0:8080/{}' .format (action ))
137+  else :
138+  fcnt = 0 
114139 if  triggerSwitch  is  True :
115140 if  isFinishCal  is  True  and  cnt  <=  2 :
116141 print  (cnt )
117142 #app('System Events').keystroke(' ') # simulate pressing blank space 
143+  last_cnt  =  cnt 
118144
119145
120146 cv2 .imshow ('output' , drawing )
@@ -123,6 +149,7 @@ def calculateFingers(res,drawing): # -> finished bool, cnt: finger count
123149 k  =  cv2 .waitKey (10 )
124150 if  k  ==  27 : # press ESC to exit 
125151 camera .release ()
152+  camera .close ()
126153 cv2 .destroyAllWindows ()
127154 break 
128155 elif  k  ==  ord ('b' ): # press 'b' to capture the background 
0 commit comments