-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreshest_frame.py
57 lines (45 loc) · 1.52 KB
/
freshest_frame.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import threading
class FreshestFrame(threading.Thread):
running = False
def __init__(self, capture, name='FreshestFrame'):
self.capture = capture
assert self.capture.isOpened()
self.cond = threading.Condition()
self.running = False
self.frame = None
self.latestnum = 0
self.callback = None
super().__init__(name=name)
self.start()
def start(self):
self.running = True
super().start()
def release(self, timeout=None):
print('release')
self.running = False
self.join(timeout=timeout)
self.capture.release()
def run(self):
counter = 0
while self.running:
(rv, img) = self.capture.read()
if not rv:
break
counter += 1
with self.cond:
self.frame = img
self.latestnum = counter
self.cond.notify_all()
if self.callback:
self.callback(img)
def read(self, wait=True, seqnumber=None, timeout=None):
with self.cond:
if wait:
if seqnumber is None:
seqnumber = self.latestnum + 1
if seqnumber < 1:
seqnumber = 1
rv = self.cond.wait_for(lambda: self.latestnum >= seqnumber, timeout=timeout)
if not rv:
return (self.latestnum, self.frame)
return (self.latestnum, self.frame)