forked from sahilparekh/GStreamer-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_prg.py
97 lines (73 loc) · 2.35 KB
/
main_prg.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
'''\
This Simple program Demonstrates how to use G-Streamer and capture RTSP Frames in Opencv using Python
- Sahil Parekh
'''
import multiprocessing as mp
import time
import vid_streamv3 as vs
import cv2
import sys
'''
Main class
'''
class mainStreamClass:
def __init__(self):
#Current Cam
self.camProcess = None
self.cam_queue = None
self.stopbit = None
self.camlink = '' #Add your RTSP cam link
self.framerate = 6
def startMain(self):
#set queue size
self.cam_queue = mp.Queue(maxsize=100)
#get all cams
time.sleep(3)
self.stopbit = mp.Event()
self.camProcess = vs.StreamCapture(self.camlink,
self.stopbit,
self.cam_queue,
self.framerate)
self.camProcess.start()
# calculate FPS
lastFTime = time.time()
try:
while True:
if not self.cam_queue.empty():
# print('Got frame')
cmd, val = self.cam_queue.get()
'''
#calculate FPS
diffTime = time.time() - lastFTime`
fps = 1 / diffTime
# print(fps)
'''
lastFTime = time.time()
# if cmd == vs.StreamCommands.RESOLUTION:
# pass #print(val)
if cmd == vs.StreamCommands.FRAME:
if val is not None:
cv2.imshow('Cam: ' + self.camlink, val)
cv2.waitKey(1)
except KeyboardInterrupt:
print('Caught Keyboard interrupt')
except:
e = sys.exc_info()
print('Caught Main Exception')
print(e)
self.stopCamStream()
cv2.destroyAllWindows()
def stopCamStream(self):
print('in stopCamStream')
if self.stopbit is not None:
self.stopbit.set()
while not self.cam_queue.empty():
try:
_ = self.cam_queue.get()
except:
break
self.cam_queue.close()
self.camProcess.join()
if __name__ == "__main__":
mc = mainStreamClass()
mc.startMain()