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.

base_camera.py 3.7 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Author : linjie
  4. import time
  5. import threading
  6. try:
  7. from greenlet import getcurrent as get_ident
  8. except ImportError:
  9. try:
  10. from thread import get_ident
  11. except ImportError:
  12. from _thread import get_ident
  13. class CameraEvent(object):
  14. """An Event-like class that signals all active clients when a new frame is
  15. available.
  16. """
  17. def __init__(self):
  18. self.events = {}
  19. def wait(self):
  20. """Invoked from each client's thread to wait for the next frame."""
  21. ident = get_ident()
  22. if ident not in self.events:
  23. # this is a new client
  24. # add an entry for it in the self.events dict
  25. # each entry has two elements, a threading.Event() and a timestamp
  26. self.events[ident] = [threading.Event(), time.time()]
  27. return self.events[ident][0].wait()
  28. def set(self):
  29. """Invoked by the camera thread when a new frame is available."""
  30. now = time.time()
  31. remove = None
  32. for ident, event in self.events.items():
  33. if not event[0].isSet():
  34. # if this client's event is not set, then set it
  35. # also update the last set timestamp to now
  36. event[0].set()
  37. event[1] = now
  38. else:
  39. # if the client's event is already set, it means the client
  40. # did not process a previous frame
  41. # if the event stays set for more than 5 seconds, then assume
  42. # the client is gone and remove it
  43. if now - event[1] > 5:
  44. remove = ident
  45. if remove:
  46. del self.events[remove]
  47. def clear(self):
  48. """Invoked from each client's thread after a frame was processed."""
  49. self.events[get_ident()][0].clear()
  50. class BaseCamera(object):
  51. thread = None # background thread that reads frames from camera
  52. frame = None # current frame is stored here by background thread
  53. last_access = 0 # time of last client access to the camera
  54. event = CameraEvent()
  55. def __init__(self):
  56. """Start the background camera thread if it isn't running yet."""
  57. if BaseCamera.thread is None:
  58. BaseCamera.last_access = time.time()
  59. # start background frame thread
  60. BaseCamera.thread = threading.Thread(target=self._thread)
  61. BaseCamera.thread.start()
  62. # wait until frames are available
  63. while self.get_frame() is None:
  64. time.sleep(0)
  65. def get_frame(self):
  66. """Return the current camera frame."""
  67. BaseCamera.last_access = time.time()
  68. # wait for a signal from the camera thread
  69. BaseCamera.event.wait()
  70. BaseCamera.event.clear()
  71. return BaseCamera.frame
  72. @staticmethod
  73. def frames():
  74. """"Generator that returns frames from the camera."""
  75. raise RuntimeError('Must be implemented by subclasses.')
  76. @classmethod
  77. def _thread(cls):
  78. """Camera background thread."""
  79. print('Starting camera thread.')
  80. frames_iterator = cls.frames()
  81. for frame in frames_iterator:
  82. BaseCamera.frame = frame
  83. BaseCamera.event.set() # send signal to clients
  84. time.sleep(0)
  85. # if there hasn't been any clients asking for frames in
  86. # the last 10 seconds then stop the thread
  87. if time.time() - BaseCamera.last_access > 10:
  88. frames_iterator.close()
  89. print('Stopping camera thread due to inactivity.')
  90. break
  91. BaseCamera.thread = None

随着人工智能和大数据的发展,任一方面对自动化工具有着一定的需求,在当下疫情防控期间,使用mindspore来实现yolo模型来进行目标检测及语义分割,对视频或图片都可以进行口罩佩戴检测和行人社交距离检测,来对公共场所的疫情防控来实行自动化管理。