real_time_face_recognition.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # coding=utf-8
  2. """Performs face detection in realtime.
  3. Based on code from https://github.com/shanren7/real_time_face_recognition
  4. """
  5. # MIT License
  6. #
  7. # Copyright (c) 2017 François Gervais
  8. #
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy
  10. # of this software and associated documentation files (the "Software"), to deal
  11. # in the Software without restriction, including without limitation the rights
  12. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. # copies of the Software, and to permit persons to whom the Software is
  14. # furnished to do so, subject to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in all
  17. # copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. # SOFTWARE.
  26. import argparse
  27. import sys
  28. import time
  29. import cv2
  30. import face
  31. def add_overlays(frame, faces, frame_rate):
  32. if faces is not None:
  33. for face in faces:
  34. face_bb = face.bounding_box.astype(int)
  35. cv2.rectangle(frame,
  36. (face_bb[0], face_bb[1]), (face_bb[2], face_bb[3]),
  37. (0, 255, 0), 2)
  38. if face.name is not None:
  39. cv2.putText(frame, face.name, (face_bb[0], face_bb[3]),
  40. cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
  41. thickness=2, lineType=2)
  42. cv2.putText(frame, str(frame_rate) + " fps", (10, 30),
  43. cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0),
  44. thickness=2, lineType=2)
  45. def main(args):
  46. frame_interval = 3 # Number of frames after which to run face detection
  47. fps_display_interval = 5 # seconds
  48. frame_rate = 0
  49. frame_count = 0
  50. video_capture = cv2.VideoCapture(0)
  51. face_recognition = face.Recognition()
  52. start_time = time.time()
  53. if args.debug:
  54. print("Debug enabled")
  55. face.debug = True
  56. while True:
  57. # Capture frame-by-frame
  58. ret, frame = video_capture.read()
  59. if (frame_count % frame_interval) == 0:
  60. faces = face_recognition.identify(frame)
  61. # Check our current fps
  62. end_time = time.time()
  63. if (end_time - start_time) > fps_display_interval:
  64. frame_rate = int(frame_count / (end_time - start_time))
  65. start_time = time.time()
  66. frame_count = 0
  67. add_overlays(frame, faces, frame_rate)
  68. frame_count += 1
  69. cv2.imshow('Video', frame)
  70. if cv2.waitKey(1) & 0xFF == ord('q'):
  71. break
  72. # When everything is done, release the capture
  73. video_capture.release()
  74. cv2.destroyAllWindows()
  75. def parse_arguments(argv):
  76. parser = argparse.ArgumentParser()
  77. parser.add_argument('--debug', action='store_true',
  78. help='Enable some debug outputs.')
  79. return parser.parse_args(argv)
  80. if __name__ == '__main__':
  81. main(parse_arguments(sys.argv[1:]))