Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import face_recognition
  5. import time
  6. import math
  7.  
  8. video_capture = cv2.VideoCapture(0)
  9. width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH )
  10. height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT )
  11.  
  12. bgSubThreshold = 50
  13. learningRate = 0
  14. value_1 = 0
  15. counter = 0
  16. found_face = False
  17.  
  18. bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold)
  19.  
  20. def removeBG(frame):
  21.     fgmask = bgModel.apply(frame,learningRate=learningRate)
  22.     # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  23.     # res = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)
  24.  
  25.     kernel = np.ones((3, 3), np.uint8)
  26.    
  27.     fgmask = cv2.erode(fgmask, kernel, iterations=1)
  28.     res = cv2.bitwise_and(frame, frame, mask=fgmask)
  29.     return res
  30.  
  31. def calculateFingers(res,drawing):  # -> finished bool, cnt: finger count
  32.     #  convexity defect
  33.     hull = cv2.convexHull(res, returnPoints=False)
  34.     if len(hull) > 3:
  35.         defects = cv2.convexityDefects(res, hull)
  36.         if type(defects) != type(None):  # avoid crashing.   (BUG not found)
  37.  
  38.             cnt = 0
  39.             avaragePoints = 0
  40.  
  41.             for i in range(defects.shape[0]):  # calculate the angle
  42.                 s, e, f, d = defects[i][0]
  43.                 start = tuple(res[s][0])
  44.                 end = tuple(res[e][0])
  45.                 far = tuple(res[f][0])
  46.                 a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
  47.                 b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
  48.                 c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
  49.                 angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))  # cosine theorem
  50.                 if angle <= math.pi / 2:  # angle less than 90 degree, treat as fingers
  51.                     cnt += 1
  52.  
  53.                     avaragePoints += angle
  54.                     avaragePoints = avaragePoints / cnt
  55.  
  56.                     cv2.circle(drawing, far, 8, [211, 84, 0], -1)
  57.             return True, cnt, far
  58.     return False, 0, 0
  59.  
  60.  
  61.  
  62.  
  63. while True:
  64.     ret, original = video_capture.read()
  65.  
  66.     original = cv2.bilateralFilter(original, 5, 50, 100)  # smoothing filter
  67.     original = cv2.flip(original, 1)  # flip the frame horizontally
  68.  
  69.    
  70.     # Create a big rectangle cus we don't need that
  71.     height, width, channels = original.shape
  72.     upper_left = (int(width / 4), int(height))
  73.     bottom_right = (int(width * 3 / 4), int(0))
  74.     cv2.rectangle(original, upper_left, bottom_right, (255, 0, 255), cv2.FILLED)
  75.  
  76.  
  77.     # third_upper_left = (int(0), int(0))
  78.     # third_bottom_right = (int(width), int(height / 3))
  79.     # cv2.rectangle(original, third_upper_left, third_bottom_right, (0, 255, 255), cv2.FILLED)
  80.  
  81.     img = removeBG(original)
  82.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  83.     blur = cv2.GaussianBlur(gray, (25, 25), 0)
  84.     cv2.imshow('blur', blur)
  85.     ret, thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY)
  86.  
  87.     _,contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  88.     length = len(contours)
  89.     maxArea = -1
  90.     if length > 0:
  91.         for i in range(length):  # find the biggest contour (according to area)
  92.             temp = contours[i]
  93.             area = cv2.contourArea(temp)
  94.             c = max(temp, key = cv2.contourArea)
  95.             x,y,w,h = cv2.boundingRect(c)
  96.             if area > maxArea:
  97.                 maxArea = area
  98.                 ci = i
  99.  
  100.         res = contours[ci]
  101.         hull = cv2.convexHull(res)
  102.         drawing = np.zeros(img.shape, np.uint8)
  103.         cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2)
  104.         cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3)
  105.         isFinishCal,cnt,avaragePoints = calculateFingers(res,drawing)
  106.         if isFinishCal is True:
  107.            
  108.             if cnt >= 3:
  109.                 # print(avaragePoints)
  110.                 # print(width, height)
  111.                 hand_x = 100/width*avaragePoints[0]
  112.                 hand_y = 100/height*avaragePoints[1]
  113.                 print(hand_x, hand_y)
  114.                 if hand_x < 50:
  115.                     position = "left"
  116.                 elif hand_x > 50:
  117.                     position = "right"
  118.                
  119.                 print(position)
  120.            
  121.  
  122.  
  123.     cv2.imshow('output', drawing)
  124.    
  125.     cv2.imshow('original', original)
  126.     # cv2.imshow('frame2', frame)
  127.  
  128.     if cv2.waitKey(1) & 0xFF == ord('q'):
  129.         break
  130.  
  131.  
  132. video_capture.release()
  133. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement