detecter_manager.py 943 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import queue
  2. import dlib
  3. def singleton(cls):
  4. instances = {}
  5. def _singleton(*args, **kw):
  6. if cls not in instances:
  7. instances[cls] = cls(*args, **kw)
  8. return instances[cls]
  9. return _singleton
  10. @singleton
  11. class Detectors(object):
  12. __queue = queue.Queue(maxsize=4)
  13. def __init__(self):
  14. for i in range(4):
  15. detector = dlib.get_frontal_face_detector()
  16. sp = dlib.shape_predictor("models/shape_predictor_68_face_landmarks.dat")
  17. facerec = dlib.face_recognition_model_v1("models/dlib_face_recognition_resnet_model_v1.dat")
  18. detector = {
  19. 'detector': detector,
  20. 'sp': sp,
  21. 'facerec': facerec
  22. }
  23. self.__queue.put(detector)
  24. def close(self, detector):
  25. if detector is not None:
  26. self.__queue.put(detector)
  27. def open(self):
  28. return self.__queue.get()