import cv2
import sys
from PyQt5.QtWidgets import QApplication
from threading import Thread
import atexit
def reader(cap, in_frames):
while True:
ret, frame = cap.read()
if ret:
in_frames.append(frame)
if len(in_frames) > 1:
in_frames.pop(0)
def shower(form, fs):
while len(fs) > 0:
cv2.imshow(form, fs[-1])
cv2.waitKey(1)
# 清理资源
def before_exit():
cap1.release()
cv2.destroyAllWindows()
atexit.register(before_exit)
if __name__ == '__main__':
app = QApplication(sys.argv)
cap1 = cv2.VideoCapture("rtsp://admin:bbing1979@192.168.102.64/h264/ch1/main/av_stream")
cap2 = cv2.VideoCapture("rtsp://admin:bbing1979@192.168.102.64/h264/ch2/main/av_stream")
cap3 = cv2.VideoCapture("rtsp://admin:bbing1979@192.168.102.64/h264/ch3/main/av_stream")
cap4 = cv2.VideoCapture("rtsp://admin:bbing1979@192.168.102.64/h264/ch4/main/av_stream")
frames1 = [0]
frames2 = [0]
frames3 = [0]
frames4 = [0]
th1 = Thread(target=reader, args=(cap1, frames1))
th2 = Thread(target=shower, args=('video1', frames1))
th3 = Thread(target=reader, args=(cap2, frames2))
th4 = Thread(target=shower, args=('video2', frames2))
th5 = Thread(target=reader, args=(cap3, frames3))
th6 = Thread(target=shower, args=('video3', frames3))
th7 = Thread(target=reader, args=(cap4, frames4))
th8 = Thread(target=shower, args=('video4', frames4))
th1.start()
th2.start()
th3.start()
th4.start()
th5.start()
th6.start()
th7.start()
th8.start()
th1.join()
th2.join()
th3.join()
th4.join()
th5.join()
th6.join()
th7.join()
th8.join()
sys.exit(app.exec_())
本地4个画面大约延迟1秒,4个中最快的比硬盘录像机直接输出的hdmi画面慢2秒,总共3秒。CPU占用非常高。