You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

face.py 9.1 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import numpy
  2. import cv2
  3. import os
  4. import curses
  5. import time
  6. from PCA9685 import PCA9685
  7. from bonjour import bonjour
  8. from missing import missing
  9. from cachecache import cachecache
  10. # Load the cascade
  11. # ========================================================================
  12. # faceCascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')
  13. # faceCascade.load('data/haarcascade_frontalface_default.xml')
  14. cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
  15. #cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_eye_tree_eyeglasses.xml"
  16. faceCascade = cv2.CascadeClassifier(cascPath)
  17. # Read the input image
  18. print(cascPath)
  19. # Start video capture
  20. video_capture = cv2.VideoCapture(0)
  21. # font
  22. font = cv2.FONT_HERSHEY_SIMPLEX
  23. # org
  24. org = (50, 50)
  25. # fontScale
  26. fontScale = 1
  27. # Blue color in BGR
  28. color = (255, 0, 0)
  29. # Line thickness of 2 px
  30. thickness = 2
  31. # ========================================================================
  32. sync_freq = 0
  33. # ========================================================================
  34. # get the curses screen window
  35. # screen = curses.initscr()
  36. # # turn off input echoing
  37. # curses.noecho()
  38. # # respond to keys immediately (don't wait for enter)
  39. # curses.cbreak()
  40. # # map arrow keys to special values
  41. # screen.keypad(True)
  42. #setting start up serrvo positions
  43. # ========================================================================
  44. pwm = PCA9685()
  45. pwm.setPWMFreq(50)
  46. max_PAN = 180
  47. max_TILT = 145
  48. min_PAN = 0
  49. min_TILT = 0
  50. max_rate_TILT = 3
  51. max_rate_PAN = 3
  52. step_PAN = 1
  53. step_TILT = 1
  54. current_PAN = 90
  55. current_TILT = 60
  56. pwm.setRotationAngle(1, current_PAN) #PAN
  57. pwm.setRotationAngle(0, current_TILT) #TILT
  58. # pseudo-PID control
  59. k_PAN = 0.015
  60. k_TILT = -0.015
  61. kd_PAN = 0.095
  62. kd_TILT = -0.095
  63. error_acceptance = 15
  64. # ========================================================================
  65. previous_x = 0
  66. previous_y = 0
  67. previous_h = 0
  68. previous_w = 0
  69. delta_x = 0
  70. delta_y = 0
  71. previous_delta_x = 0
  72. previous_delta_y = 0
  73. delta_x_dot = 0
  74. delta_y_dot = 0
  75. rectangle_found = 0
  76. # make some fun
  77. bonjour = bonjour()
  78. missing = missing()
  79. cachecache = cachecache()
  80. bonjour_ind = 0
  81. missing_ind = 0
  82. cachecache_ind = 0
  83. #
  84. # main loop
  85. # ========================================================================
  86. # https://techvidvan.com/tutorials/face-recognition-project-python-opencv/
  87. try:
  88. while True:
  89. # Try to reduce lagging issues
  90. if sync_freq == 0:
  91. # Capture frame-by-frame
  92. ret, frame = video_capture.read()
  93. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  94. faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2, minNeighbors=4, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
  95. if sync_freq < 0:
  96. sync_freq += 1
  97. # stopping blinking rectangle
  98. # if rectangle_found > 0:
  99. # cv2.rectangle(frame, (previous_x, previous_y), (x+previous_w, y+previous_h), (0, 255, 0), 2)
  100. #
  101. else:
  102. sync_freq = 0
  103. rectangle_found = 0
  104. for (x, y, w, h) in faces:
  105. cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  106. rectangle_found += 1
  107. if rectangle_found == 1:
  108. print(' x y previous ', previous_x, previous_y)
  109. # ========================================================================================
  110. # stay away from me !
  111. # delta_x = previous_x - x
  112. # delta_y = previous_y - y
  113. # get in touch !
  114. delta_x = 300 - x
  115. delta_y = 200 - y
  116. delta_x_dot = delta_x - previous_delta_x
  117. delta_y_dot = delta_y - previous_delta_y
  118. # ========================================================================================
  119. # ignoring small error
  120. if abs(delta_x) < error_acceptance:
  121. delta_x = 0
  122. delta_x_dot = 0
  123. if abs(delta_y) < error_acceptance:
  124. delta_y = 0
  125. delta_y_dot = 0
  126. # ========================================================================================
  127. print(' x y new ', x, y)
  128. previous_x = x
  129. previous_y = y
  130. previous_h = h
  131. previous_w = w
  132. previous_delta_x = delta_x
  133. previous_delta_y = delta_y
  134. cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  135. cv2.putText(frame, str(x) + " " + str(y), (x, y), font, fontScale, (75, 75, 0), thickness, cv2.LINE_AA)
  136. # wait for keypress
  137. # ===========================================================
  138. char = cv2.waitKey(20)
  139. #print('key pressed', char)
  140. if char == ord('q'):
  141. break
  142. if char == ord('p'):
  143. #if p is pressed take a photo!
  144. # camera.capture('image%s.jpg' % pic)
  145. # pic = pic +1
  146. # screen.addstr(0, 0, 'picture taken! ')
  147. cv2.putText(frame, 'another day in paradise !', (50,50) , font, fontScale, color, thickness, cv2.LINE_AA)
  148. elif char == 83:
  149. current_PAN = max(min_PAN, current_PAN - step_PAN)
  150. pwm.setRotationAngle(1, current_PAN) #PAN
  151. #time.sleep(0.001)
  152. cv2.putText(frame, 'right ', (20, 20), font, fontScale, (0, 255, 0), thickness, cv2.LINE_AA)
  153. elif char == 81:
  154. current_PAN = min(max_PAN, current_PAN + step_PAN)
  155. pwm.setRotationAngle(1, current_PAN) #PAN
  156. #time.sleep(0.001)
  157. cv2.putText(frame, 'left ', (20, 20), font, fontScale, (0, 255, 0), thickness, cv2.LINE_AA)
  158. elif char == 82:
  159. current_TILT = max(min_TILT, current_TILT - step_TILT)
  160. pwm.setRotationAngle(0, current_TILT) #TILT
  161. #time.sleep(0.001)
  162. cv2.putText(frame, 'up ', (20, 20), font, fontScale, (0, 255, 0), thickness, cv2.LINE_AA)
  163. elif char == 84:
  164. current_TILT = min(max_TILT, current_TILT + step_TILT)
  165. pwm.setRotationAngle(0, current_TILT) #TILT
  166. #time.sleep(0.001)
  167. cv2.putText(frame, 'down ', (20, 20), font, fontScale, (0, 255, 0), thickness, cv2.LINE_AA)
  168. elif rectangle_found > 0 and (abs(delta_x) < 500 and abs(delta_y) < 500) and char == -1:
  169. # stay away
  170. # k_PAN = -0.01
  171. # k_TILT = +0.01
  172. # get in touch
  173. print('pan tilt -- current ', current_PAN, current_TILT)
  174. # pseu-do PID
  175. delta_TILT = k_TILT * delta_y + kd_TILT * delta_y_dot
  176. # rate-limiter
  177. delta_TILT = min(abs(delta_TILT), max_rate_TILT)*numpy.sign(delta_TILT)
  178. # noise exclude
  179. if abs(delta_TILT) < step_TILT:
  180. delta_TILT = 0
  181. # here we go
  182. current_TILT = current_TILT + delta_TILT
  183. if current_TILT > max_TILT:
  184. current_TILT = max_TILT
  185. if current_TILT < min_TILT:
  186. current_TILT = min_TILT
  187. print('delta tilt ', delta_TILT)
  188. # pseu-do PID
  189. delta_PAN = k_PAN * delta_x + kd_PAN * delta_x_dot
  190. # rate-limiter
  191. delta_PAN = min(abs(delta_PAN), max_rate_PAN)*numpy.sign(delta_PAN)
  192. # noise exclude
  193. if abs(delta_PAN) < step_PAN:
  194. delta_PAN = 0
  195. # here we go
  196. current_PAN = current_PAN + delta_PAN
  197. if current_PAN > max_PAN:
  198. current_PAN = max_PAN
  199. if current_PAN < min_PAN:
  200. current_PAN = min_PAN
  201. print('delta PAN ', delta_PAN)
  202. print('delta_x delta_y ', delta_x, delta_y)
  203. print('pan tilt -- new ', current_PAN, current_TILT)
  204. pwm.setRotationAngle(1, current_PAN)
  205. pwm.setRotationAngle(0, current_TILT)
  206. elif char == -1:
  207. cv2.putText(frame, 'on air !', (20, 20), font, fontScale, (0, 255, 0), thickness, cv2.LINE_AA)
  208. #print('on air !')
  209. # Display the resulting frame
  210. cv2.imshow('face_tracking', frame)
  211. #
  212. finally:
  213. # shut down cleanly
  214. pwm.exit_PCA9685()
  215. video_capture.release()
  216. cv2.destroyAllWindows()

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.