Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import copy
  4. import os
  5. import math
  6. #from appscript import app
  7.  
  8. # Environment:
  9. # OS : Mac OS EL Capitan
  10. # python: 3.5
  11. # opencv: 2.4.13
  12.  
  13. # parameters
  14. cap_region_x_begin=0.5 # start point/total width
  15. cap_region_y_end=0.8 # start point/total width
  16. threshold = 60 # BINARY threshold
  17. blurValue = 41 # GaussianBlur parameter
  18. bgSubThreshold = 50
  19. learningRate = 0
  20.  
  21. # variables
  22. delay = 32
  23. delay1 = 32
  24. oncmd = './on.sh'
  25. offcmd = './off.sh'
  26. cmd = './meshtest'
  27. isBgCaptured = 0 # bool, whether the background captured
  28. on = delay
  29. off = delay
  30. triggerSwitch = False # if true, keyborad simulator works
  31.  
  32. def printThreshold(thr):
  33. print("! Changed threshold to "+str(thr))
  34.  
  35.  
  36. def removeBG(frame):
  37. fgmask = bgModel.apply(frame,learningRate=learningRate)
  38. # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  39. # res = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
  40.  
  41. kernel = np.ones((3, 3), np.uint8)
  42. fgmask = cv2.erode(fgmask, kernel, iterations=1)
  43. res = cv2.bitwise_and(frame, frame, mask=fgmask)
  44. return res
  45.  
  46.  
  47. def calculateFingers(res,drawing): # -> finished bool, cnt: finger count
  48. # convexity defect
  49. hull = cv2.convexHull(res, returnPoints=False)
  50. if len(hull) > 3:
  51. defects = cv2.convexityDefects(res, hull)
  52. if type(defects) != type(None): # avoid crashing. (BUG not found)
  53.  
  54. cnt = 0
  55. for i in range(defects.shape[0]): # calculate the angle
  56. s, e, f, d = defects[i][0]
  57. start = tuple(res[s][0])
  58. end = tuple(res[e][0])
  59. far = tuple(res[f][0])
  60. a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
  61. b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
  62. c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
  63. angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
  64. if angle <= math.pi / 2: # angle less than 90 degree, treat as fingers
  65. cnt += 1
  66. cv2.circle(drawing, far, 8, [211, 84, 0], -1)
  67. return True, cnt
  68. return False, 0
  69.  
  70.  
  71. # Camera
  72. camera = cv2.VideoCapture(0)
  73. camera.set(10,200)
  74. cv2.namedWindow('trackbar')
  75. cv2.createTrackbar('trh1', 'trackbar', threshold, 100, printThreshold)
  76.  
  77.  
  78. while camera.isOpened():
  79. ret, frame = camera.read()
  80. threshold = cv2.getTrackbarPos('trh1', 'trackbar')
  81. frame = cv2.bilateralFilter(frame, 5, 50, 100) # smoothing filter
  82. frame = cv2.flip(frame, 1) # flip the frame horizontally
  83. cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0),
  84. (frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2)
  85. cv2.imshow('original', frame)
  86.  
  87. # Main operation
  88. if isBgCaptured == 1: # this part wont run until background captured
  89. img = removeBG(frame)
  90. img = img[0:int(cap_region_y_end * frame.shape[0]),
  91. int(cap_region_x_begin * frame.shape[1]):frame.shape[1]] # clip the ROI
  92. cv2.imshow('mask', img)
  93.  
  94. # convert the image into binary image
  95. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  96. blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0)
  97. cv2.imshow('blur', blur)
  98. ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY)
  99. cv2.imshow('ori', thresh)
  100.  
  101.  
  102. # get the coutours
  103. thresh1 = copy.deepcopy(thresh)
  104. _,contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  105. length = len(contours)
  106. maxArea = -1
  107. if length > 0:
  108. for i in range(length): # find the biggest contour (according to area)
  109. temp = contours[i]
  110. area = cv2.contourArea(temp)
  111. if area > maxArea:
  112. maxArea = area
  113. ci = i
  114.  
  115. res = contours[ci]
  116. hull = cv2.convexHull(res)
  117. drawing = np.zeros(img.shape, np.uint8)
  118. cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2)
  119. cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3)
  120.  
  121. isFinishCal,cnt = calculateFingers(res,drawing)
  122. if triggerSwitch is True:
  123. if isFinishCal is True:
  124. print (cnt)
  125. if cnt == 4:#on
  126. if on < delay1:
  127. on = on + 1
  128. if on >= delay1:
  129. os.system(oncmd)
  130. print('on received')
  131. #os.system(cmd)
  132. on = 0
  133. if cnt == 2:#off
  134. if off < delay:
  135. off = off + 1
  136. if off >= delay:
  137. os.system(offcmd)
  138. print('off received')
  139. #os.system(cmd)
  140. off = 0
  141.  
  142. #app('System Events').keystroke(' ') # simulate pressing blank space
  143.  
  144.  
  145. cv2.imshow('output', drawing)
  146.  
  147. # Keyboard OP
  148. k = cv2.waitKey(10)
  149. if k == 27: # press ESC to exit
  150. break
  151. elif k == ord('b'): # press 'b' to capture the background
  152. bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold)
  153. isBgCaptured = 1
  154. print( 'Background captured!')
  155. elif k == ord('r'): # press 'r' to reset the background
  156. bgModel = None
  157. triggerSwitch = False
  158. isBgCaptured = 0
  159. print ('Background reset!')
  160. elif k == ord('n'):
  161. triggerSwitch = True
  162. print ('Command mode on!')
  163. elif k == ord('m'):
  164. triggerSwitch = False
  165. print ('Command mode off!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement