OpenCV打开不存在的RTSP视频流超时的解决方案

本文最后更新于:2 个月前

在我们使用OpenCV计算机开源视觉库去处理视屏流(rtsp, rtmp…)时,我们发现如果去实例化一个不存在的RTSP视屏流地址,会发生超时的问题,而且这个实例化线程是原子阻塞的,无法强制退出,这在某些场景下是不能被接受的,所以我们必须采取一定的方案去限定cv2.VideoCapture(rtsp)实例化的时间,经过多番探索,我发现用自定义多线程和装饰器可以解决这个问题,代码如下:

When we use the OpenCV computer open source vision library to process video streams (rtsp, rtmp…), we found that if we instantiate a non-existent RTSP video stream address, a timeout problem will occur, and This instantiation thread is atomically blocked and cannot be forced to exit. This is unacceptable in some scenarios, so we must adopt a certain plan to limit the instantiation time of cv2.VideoCapture(rtsp) After many explorations, I found that using custom multi-threading and decorators can solve this problem, the code is as follows:

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
# opencv_demo.py
# OpenCV-Python timeout for opening a non-existent RTSP video stream
import cv2
import base64
import time
import threading
TIME_LIMITED: int = 1


class MyThread(threading.Thread):
def __init__(self, target, args=()):
super(MyThread, self).__init__()
self.func = target
self.args = args

def run(self):
self.result = self.func(*self.args)

def get_result(self):
try:
return self.result
except Exception:
return None


# Decorator to limit the actual request time or function execution time
def limit_decor(limit_time):
"""
:param limit_time: Set the maximum allowable execution time, unit: second
:return: Untimed returns the value of the decorated function; timed out returns None
"""
def functions(func):
def run(*params):
thre_func = MyThread(target=func, args=params)
# The thread method terminates when the main thread terminates (exceeds its length)
thre_func.setDaemon(True)
thre_func.start()
# Count the number of segmental slumbers
sleep_num = int(limit_time // 1)
sleep_nums = round(limit_time % 1, 1)
# Sleep briefly several times and try to get the return value
for i in range(sleep_num):
time.sleep(1)
infor = thre_func.get_result()
if infor:
return infor
time.sleep(sleep_nums)
# Final return value (whether or not the thread has terminated)
if thre_func.get_result():
return thre_func.get_result()
else:
return (False, None) # Timeout returns can be customized

return run

return functions


@limit_decor(TIME_LIMITED)
def video_capture_open(rtsp):
capture = cv2.VideoCapture(rtsp)
return (True, capture)

def frame_get(rtsp):
try:
cap_status, cap = video_capture_open(rtsp)
if not cap_status:
print(cap_status, cap)
return cap
while True:
ret, image_frame = cap.read()
cv2.imshow("res", image_frame)
cv2.waitKey(3)
if not ret:
continue
image = cv2.imencode('.png', image_frame)[1]
image_base64_data = str(base64.b64encode(image))[2:-1]
return image_base64_data
except Exception as err:
print(err)
pass


def main() -> None:

RTSP = 'rtsp://192.168.3.88/av0_0' # No existing RTSP video stream address
result = frame_get(RTSP)
if not result:
print("Failed to open RTSP stream")
else:
print(result[:10])

return None


if __name__ == "__main__":
main()

测试结果

1
2
3
python opencv_demo.py
False None
Failed to open RTSP stream

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!