opencv rtsp 延迟高
时间: 2023-12-09 07:02:23 浏览: 176
Opencv读取rtsp流后进行图像处理会有非常大的延时,解决方法是创建一个线程专门用于抓取帧。下面是一个示例代码,可以帮助你更好地理解如何使用线程来解决Opencv读取rtsp流的延迟问题。
```python
import cv2
import threading
class VideoCaptureThreading:
def __init__(self, src=0):
self.capture = cv2.VideoCapture(src)
self.frame = None
self.stopped = False
def start(self):
threading.Thread(target=self.update, args=()).start()
return self
def update(self):
while not self.stopped:
if not self.capture.isOpened():
print('Unable to load camera.')
break
ret, frame = self.capture.read()
if ret:
self.frame = frame
self.capture.release()
def read(self):
return self.frame
def stop(self):
self.stopped = True
if __name__ == '__main__':
video_capture = VideoCaptureThreading('rtsp://admin:abc123456@192.168.1.64/h264/ch1/main/av_stream').start()
while True:
frame = video_capture.read()
if frame is not None:
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.stop()
cv2.destroyAllWindows()
```
阅读全文