forked from cyberneuron/RT-CNN-Vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamReader.py
75 lines (68 loc) · 2.18 KB
/
streamReader.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
import threading
import cv2
import itertools
import argparse
import time
class StreamReader:
def __init__(self, src=0,framesleep=None):
self.framesleep = framesleep
self.src = src
self.cap = cv2.VideoCapture(self.src)
self.grabbed, self.frame = self.cap.read()
self.started = False
self.read_lock = threading.Lock()
def __enter__(self):
self.start()
# print(f"FPS is: {self.cap.get(cv2.CAP_PROP_FPS)}")
# self.cap.set(cv2.CAP_PROP_FPS, 2)
return self
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.daemon = True
self.thread.start()
return self
def update(self):
while self.started:
if self.framesleep:
time.sleep(self.framesleep)
grabbed, frame = self.cap.read()
with self.read_lock:
self.grabbed = grabbed
self.frame = frame
def read(self):
while True:
grabbed = self.grabbed
frame = None
if grabbed:
frame = self.frame.copy()
if grabbed:
yield frame
else:
break
# return grabbed, frame
def stop(self):
self.started = False
self.thread.join()
def __exit__(self, exec_type, exc_value, traceback):
self.stop()
self.cap.release()
print("Exiting capture")
print(exec_type, exc_value, traceback)
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('stream', metavar="image", default='http://192.168.16.101:8080/video',
help="Image")
args = parser.parse_args()
with StreamReader(args.stream) as wc:
# for i in itertools.count():
for frame in wc.read():
if cv2.waitKey(1) ==27:
exit(0)
cv2.imshow("frame",frame)