-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_capture.py
118 lines (98 loc) · 3.38 KB
/
video_capture.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Written by Luis Mesas
import threading
import time
import cv2
from datetime import datetime
# Define video capture class
class VideoCaptureAsync:
def __init__(self, src=0, width=640, height=480, driver=None):
self.src = src
if driver is None:
self.cap = cv2.VideoCapture(self.src)
else:
self.cap = cv2.VideoCapture(self.src, driver)
# set buffer size to 1
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# time.sleep(2)
self.grabbed, self.frame = self.cap.read()
self.started = False
self.read_lock = threading.Lock()
self.thread = None
def get(self, var1):
return self.cap.get(var1)
def set(self, var1, var2):
self.cap.set(var1, var2)
def start(self):
if self.started:
print('[!] Asynchroneous video capturing has already been started.')
return None
self.started = True
self.thread = threading.Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
while self.started:
grabbed, frame = self.cap.read()
with self.read_lock:
self.grabbed = grabbed
self.frame = frame
def read(self):
# for _ in range(5):
# self.cap.grab()
with self.read_lock:
frame = self.frame.copy()
grabbed = self.grabbed
return grabbed, frame
def stop(self):
self.started = False
self.thread.join()
def __exit__(self, exec_type, exc_value, traceback):
self.cap.release()
class VideoCaptureAsyncWithTimestamp:
def __init__(self, src=0, width=640, height=480, driver=None):
self.src = src
if driver is None:
self.cap = cv2.VideoCapture(self.src)
else:
self.cap = cv2.VideoCapture(self.src, driver)
# set buffer size to 1
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.timestamp = datetime.now()
self.grabbed, self.frame = self.cap.read()
self.started = False
self.read_lock = threading.Lock()
self.thread = None
def get(self, var1):
return self.cap.get(var1)
def set(self, var1, var2):
self.cap.set(var1, var2)
def start(self):
if self.started:
print('[!] Asynchroneous video capturing has already been started.')
return None
self.started = True
self.thread = threading.Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
while self.started:
grabbed, frame = self.cap.read()
with self.read_lock:
self.grabbed = grabbed
self.frame = frame
def read(self):
with self.read_lock:
self.timestamp = datetime.now()
#_, self.frame = self.cap.retrieve()
frame = self.frame.copy()
grabbed = self.grabbed
return grabbed, frame, self.timestamp
def stop(self):
self.started = False
self.thread.join()
def __exit__(self, exec_type, exc_value, traceback):
self.cap.release()