如何使用硬件解码器解码 python 中的 RTSP 流? (英伟达 JetSon Nano)

How to use hardware decoder for decode RTSP stream in python? (NVidia JetSon Nano)

我有 NVIDIA Jetson Nano 和全高清 IP 摄像头。 相机流 RTSP/h264.

我想解码来自此相机的 python 脚本中的帧以进行分析。

所以,我尝试使用类似的东西:

# import the necessary packages
from imutils.video import VideoStream
import imutils
import time
import cv2
# grab a reference to the webcam
print("[INFO] starting video stream...")
#vs = VideoStream(src=0).start()
vs = VideoStream(src="rtsp://login:password@192.168.1.180").start()
time.sleep(2.0)

# loop over frames
while True:
    # grab the next frame
    frame = vs.read()
    # resize the frame to have a maximum width of 500 pixels
    frame = imutils.resize(frame, width=500)
    # show the output frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
# release the video stream and close open windows
vs.stop()
cv2.destroyAllWindows()

行得通,但在 CPU 上以这种方式解码了帧。如何使用GPU解码器?

解决方法:

cv2.VideoCapture 与 GStreamer 后端一起使用:

import cv2
pipeline = "rtspsrc location=\"rtsp://login:password@host:port/\" ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, format=(string)BGRx! videoconvert ! appsink"
capture = cv2.VideoCaputure(pipeline, cv2.CAP_GSTREAMER)

while capture.isOpened():
    res, frame = capture.read()
    cv2.imshow("Video", frame)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break
capture.release()
cv2.destroyAllWindows()