Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import math
  4. #capture webcam video
  5. cap = cv2.VideoCapture(0)
  6. width, height = 1920,1080
  7. cap.set(3,width)
  8. cap.set(4,height)
  9.  
  10. #create a method that can seperate the foreground from the background
  11. fgbg = cv2.createBackgroundSubtractorMOG2()
  12.  
  13. #setup outfile for writing video
  14. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  15. out = cv2.VideoWriter('/home/sm/Desktop/keyboard.avi',fourcc, 20.0, (1280,960))
  16.  
  17. #define alphabet position
  18. position = 0
  19. #define hand position
  20. hand_position = 0,0
  21. hand_on_keyboard = False
  22. letter_selected = False
  23. #define font and text color
  24. font = cv2.FONT_HERSHEY_SIMPLEX
  25. color = (13,32,210)
  26.  
  27. #create a list for the word
  28. word = ''
  29.  
  30. #create a letter buffer
  31. letter_buffer = []
  32.  
  33. #define frame_num
  34. frame_num = 0
  35.  
  36. #function that determines distance between two points
  37. def distance(p0,p1):
  38. return math.sqrt((p1[1]-p0[1])**2+(p1[0]-p0[0])**2)
  39.  
  40. #main loop
  41. while True:
  42.  
  43. #read a frame from the webcam
  44. _, frame = cap.read()
  45. frame = cv2.flip(frame, 1)
  46. frame_num += 1
  47.  
  48. #create a composite image that includes the webcam
  49. composite = frame.copy()
  50.  
  51. #add the letters
  52. #make a list of letter positions
  53. letter_positions = []
  54. for letter in range(150):
  55. x_position = position + letter*120
  56. y_position = 150
  57. xy = x_position, y_position
  58. cv2.putText(composite,chr(40+letter),xy, font, 2,color,3)
  59. letter_positions.append((chr(40+letter),xy))
  60. #if there is a letter selected, make that letter green
  61. if letter_selected:
  62. cv2.putText(composite, closest_letter, close_letter_position, font, 2, (255,0,0), 3)
  63. #add a line to show where the keyboard starts
  64. cv2.line(composite, (composite.shape[1],200), (0,200), color, 2)
  65.  
  66. #find the background
  67. #look only at the keyboard part of the frame
  68. look = frame[50:200, 0:frame.shape[1]]
  69. fgmask = fgbg.apply(look)
  70.  
  71.  
  72. #define apparent_motion as false
  73. apparent_motion = False
  74. letter_selected = False
  75. previous_position = hand_position
  76. #find the largest contour in the fgmask image
  77. b,a,c = cv2.findContours(fgmask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  78. #draw a circle aroudn the contour
  79. for cnt in a:
  80. (x,y),radius = cv2.minEnclosingCircle(cnt)
  81. center = (int(x),int(y))
  82. area = cv2.contourArea(cnt)
  83. if area > 1000 and area < 2000:
  84. radius = int(radius)
  85. cv2.circle(composite,center,radius,(0,120,255), thickness=5)
  86. cv2.line(composite, (center[0]-50,center[1]), (center[0]+50,center[1]), (0,0,0),3)
  87. cv2.line(composite, (center[0],center[1]-50), (center[0],center[1]+50), (0,0,0),3)
  88. letter_selected = True
  89. #find the closest letter
  90. closest_letter = 'a'
  91. min_distance = 100000
  92. for letter_tuple in letter_positions:
  93. letter_position = letter_tuple[1]
  94. if distance(letter_position, center) < min_distance:
  95. closest_letter = letter_tuple[0]
  96. min_distance = distance(letter_position, center)
  97. close_letter_position = letter_position
  98.  
  99. letter_buffer.append(closest_letter)
  100.  
  101. #determine if there is apparent motion (swiping)
  102. if area > 2000:
  103. hand_position = center
  104. if previous_position != (0,0):
  105. apparent_motion = True
  106. if previous_position[0] > hand_position[0]:
  107. dirrection = 'Left'
  108. position -=100
  109. if previous_position[0] < hand_position[0]:
  110. dirrection = 'Right'
  111. position += 100
  112.  
  113. previous_position = hand_position
  114. #if a letter has been selected for 10 frames, write the letter and clear the buffer
  115. for letter in letter_buffer[20:]:
  116. if letter == closest_letter:
  117. word += letter
  118. letter_buffer = []
  119. #write the word
  120. cv2.putText(composite, word, (50,50), font, 2, (0,0,0), 4)
  121. #show the various images and wait
  122. #cv2.imshow('fgmask',fgmask)
  123. #cv2.imshow('frame', frame)
  124. cv2.imshow('composite', composite)
  125. out.write(composite)
  126. k = cv2.waitKey(30) & 0xff
  127. if k == 27:
  128. break
  129. cap.release()
  130. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement