由于Python算法对画面进行处理时会耗时,导致帧堵塞后持续掉帧,直至程序挂断。在此方案可开一个线程、一个全局变量时刻获取最新帧,算法处理最新帧即可。
import threading
def get_latest_frame():
global latest_frame
while (cap.isOpened()):
ret, frame = cap.read()
if ret == False:
is_frame = False
continue
latest_frame = frame
cap.release()
# 配置摄像头及全局变量
rtsp_url = r'rtsp://admin:DSMY666666@192.168.3.42:5545/Streaming/Channels/102'
latest_frame = None
cap = cv2.VideoCapture(rtsp_url)
threading.Thread(target=get_latest_frame).start()