Spaces:
Runtime error
Runtime error
Download dp2/utils/bufferless_video_capture.py from molecularmax/deep_privacy2_face_guard: direct link, hf CLI and curl.
- Browser
- Download file 958 Bytes
-
https://huggingface.co/spaces/molecularmax/deep_privacy2_face_guard/resolve/main/dp2/utils/bufferless_video_capture.py
- Command line
-
hf download hf://spaces/molecularmax/deep_privacy2_face_guard/dp2/utils/bufferless_video_capture.py
-
curl -L -o bufferless_video_capture.py https://huggingface.co/spaces/molecularmax/deep_privacy2_face_guard/resolve/main/dp2/utils/bufferless_video_capture.py
958 Bytes
| import queue | |
| import threading | |
| import cv2 | |
| class BufferlessVideoCapture: | |
| def __init__(self, name, width=None, height=None): | |
| self.cap = cv2.VideoCapture(name) | |
| if width is not None and height is not None: | |
| self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) | |
| self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) | |
| self.q = queue.Queue() | |
| t = threading.Thread(target=self._reader) | |
| t.daemon = True | |
| t.start() | |
| # read frames as soon as they are available, keeping only most recent one | |
| def _reader(self): | |
| while True: | |
| ret, frame = self.cap.read() | |
| if not ret: | |
| break | |
| if not self.q.empty(): | |
| try: | |
| self.q.get_nowait() # discard previous (unprocessed) frame | |
| except queue.Empty: | |
| pass | |
| self.q.put((ret, frame)) | |
| def read(self): | |
| return self.q.get() | |