This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream.py
70 lines (60 loc) · 2.12 KB
/
stream.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
import cv2
import time
import threading
from PyQt5.QtGui import (QImage, QPixmap)
# this solution is inspired from stackoverflow:
# https://reurl.cc/MvrER4
class StoppableThread(threading.Thread):
# inherit from threading.Thread
# Thread class with a stop() method.
# The thread itself has to check regularly for the stopped() condition.
def __init__(self, url, out_label, width, height):
super().__init__()
self._stop_event = threading.Event()
self.url = url
self.out_label = out_label
self.width = width
self.height = height
def stop(self):
# set self._stop_event as True
self._stop_event.set()
def stopped(self):
# if self._stop_event is True, return True
return self._stop_event.is_set()
class StreamVideo(StoppableThread):
def run(self):
capture = cv2.VideoCapture(self.url)
capture.set(cv2.CAP_PROP_BUFFERSIZE, 2)
# FPS = 1/X
# X = desired FPS
FPS = 1 / 60
FPS_MS = int(FPS * 1000)
while(not self.stopped()):
(status, frame) = capture.read()
if(status):
# resize the frame to fit the out.label
frame = cv2.resize(frame, (self.width, self.height))
# convert from RGB to BGR
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# The image is stored using a 24-bit RGB format (8-8-8).
img = QImage(
frame.data,
frame.shape[1],
frame.shape[0],
QImage.Format_RGB888
)
# output image
self.out_label.setPixmap(QPixmap.fromImage(img))
# cv2.imshow("frame", frame) # for debugging
time.sleep(FPS)
cv2.waitKey(FPS_MS)
else:
self.out_label.setText("URL not found")
break
# for debugging
if __name__ == "__main__":
url = "rtmp://202.69.69.180:443/webcast/bshdlive-pc"
testthread = StreamVideo(url)
testthread.start()
time.sleep(10)
testthread.stop()