-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamer.py
62 lines (48 loc) · 1.36 KB
/
streamer.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
# import libraries
import time
import threading
from vidgear.gears import VideoGear
from vidgear.gears import NetGear
import sys
import cv2
FPS = 25
def streaming_thread(port, video):
stream = VideoGear(source=video).start() # Open any video stream
options = {'request_timeout': 100}
server = NetGear(port=port, **options) # Define netgear server with default settings
frame_count = 1
while True:
try:
frame = stream.read()
# check if frame is None
if frame is None:
# if True break the infinite loop
break
server.send(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
print(f"Sent frame {frame_count} on {port}")
frame_count += 1
time.sleep(1 / FPS)
except:
# break the infinite loop
break
# safely close video stream
stream.stop()
server.close()
def main(argv):
n = int(argv[1])
ports = argv[2:2 + n]
videos = argv[2 + n:]
print(n, ports, videos)
threads = []
for i in range(n):
t = threading.Thread(target=streaming_thread, args=[ports[i], videos[i]])
t.setDaemon(True)
t.start()
threads.append(t)
try:
for t in threads:
t.join()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main(sys.argv)