I have purchased an IP camera which I am trying to connect to using RTSP. The RTSP connection URL is rtsp://admin:@192.168.0.27/channel=1&stream=0.554
. I am using OpenCV
to open and display the stream:
#include <opencv2/opencv.hpp>
int main() {
cv::VideoCapture cap;
if (!cap.open("rtsp://admin:@192.168.0.27/channel=1&stream=0.554")) {
std::cout << "Unable to open video capture\n";
return -1;
}
while(true) {
cv::Mat frame;
auto ret = cap.grab();
cap >> frame;
if (frame.empty()) {
break; // End of video stream
}
cv::resize(frame, frame, cv::Size(640, 480));
// Do other stuff here with frame
cv::imshow("frame", frame);
if (cv::waitKey(10) == 27) {
break; // stop capturing by pressing ESC
}
}
return 0;
}
When I run the program, it will connect successfully and display a few frames, but then will start to lag badly and usually hang and output some error like this before crashing:
[h264 @ 0x558ae8e601a0] error while decoding MB 93 40, bytestream -11
I am not sure why I am having issues displaying the stream consistently. Additionally, when it is able to display the stream, I find that it is quickly become out of sync (note I am doing some heavy processing on the frame which takes quite a lot of time). As in, it is not displaying the real time frame, but there is a growing lag.
How can I also ensure to use the "latest" frame, and discard all the other ones which may have accumulated in some buffer. Also, any ideas why it is crashing and how I can improve the streaming?
I was able to find this SO post which deals with getting the latest frame using gstreamer. When I modify my video capture string to utilize gstreamer, it works a bit better.
Here is the modified connection string: "rtspsrc location=rtsp://admin:@192.168.0.27/channel=1&stream=0.554 ! decodebin ! videoconvert ! appsink max-buffers=1 drop=true"
I have no experience with gstreamer so I am not sure what it is doing, but it seems to improve things. However, once it a while, it will all go grey and only display the pixels when there is movement, as shown in the following images. With my experince with codecs, I believe the reference frame is missing, but I am not really sure. Any ideas on how to fix this? If I am not using the correct gstreamer parameters, please make a suggestion as to what I should be using for fast streaming (always using the latest frame). As I mentioned, I have minimal experience with gstreamer. Thanks for the help!
This may be due to packet loss of the network transmission. You can give it a try to modify the URL to use the rtspt://
protocol. This will try to establish a TCP transmission which should prevent packet loss on your receiving side.
The best approach is to use threads to read frames continuously and assign them on an attribute of a class. In this way if some thread encounters the packet loss, the other thread buddies compensate for it.
check this out, I hope it helps:
from threading import Thread
import cv2
class RTSPVideoWriterObject(object):
def __init__(self, src=0):
# Create a VideoCapture object
self.capture = cv2.VideoCapture(src)
self.status, self.frame = None, None
# Default resolutions of the frame are obtained (system dependent)
self.frame_width = int(self.capture.get(3))
self.frame_height = int(self.capture.get(4))
# Set up codec and output video settings
self.codec = cv2.VideoWriter_fourcc(*'MJPG')
self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))
# Start the thread to read frames from the video stream
self.thread = Thread(target=self.update, args=())
self.thread.daemon = True
self.thread.start()
def update(self):
# Read the next frame from the stream in a different thread
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def show_frame(self):
# Display frames in main program
# if self.status:
# cv2.imshow('frame', self.frame)
# Press Q on keyboard to stop recording
key = cv2.waitKey(1)
if key == ord('q'):
self.capture.release()
self.output_video.release()
cv2.destroyAllWindows()
exit(1)
def save_frame(self):
# Save obtained frame into video output file
self.output_video.write(self.frame)
if __name__ == '__main__':
rtsp_link = "rtsp://admin:@192.168.0.27/channel=1&stream=0.554"
video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
while True:
try:
video_stream_widget.show_frame()
video_stream_widget.save_frame()
except AttributeError:
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.