forked from moritzschaefer/kickercam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringbuffer.py
More file actions
37 lines (26 loc) · 957 Bytes
/
ringbuffer.py
File metadata and controls
37 lines (26 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import numpy as np
import cv2
from config import WIDTH, HEIGHT
class Ringbuffer:
def __init__(self, num_frames):
if num_frames > 42 * 10:
raise ValueError('Too many frames for RAM')
self.num_frames = num_frames
self.current = 0
self.data = [None] * self.num_frames
def store_next_frame(self, frame):
'''
:frame: will be written into next frame
'''
self.current += 1
self.current %= self.num_frames
if isinstance(frame, np.ndarray):
self.data[self.current] = cv2.imencode('.jpg', frame)[1]
def __iter__(self):
for frame_i in list(range(self.current, self.num_frames)) + \
list(range(0, self.current)):
try:
yield cv2.imdecode(self.data[frame_i], cv2.IMREAD_COLOR)
except Exception as e:
print('empty buffer. nothing to decode')
return