dusty-nv / jetson-inference

Hello AI World guide to deploying deep-learning inference networks and deep vision primitives with TensorRT and NVIDIA Jetson.
https://developer.nvidia.com/embedded/twodaystoademo
MIT License
7.87k stars 2.98k forks source link

How to get the updated frame from rtsp #792

Closed john4d20 closed 4 years ago

john4d20 commented 4 years ago

When i am using input.Capture () from RTSP, i discover it will process all incoming rtsp frames, however, the computing power is not enough , therefore it will become slower and slower. I hope I can process the current frame only rather than process the past frame. SO how can I get the current frame from rtsp?

japrogramer commented 4 years ago

https://developer.nvidia.com/deepstream-sdk

Or

import cv2
import time
import threading

global video_frame
video_frame = None

global thread_lock 
thread_lock = threading.Lock()

GSTREAMER_PIPELINE = (
                'rtspsrc location=rtsp latency=300 !'
                'rtph264depay ! h264parse !'
                'queue max-size-buffers=10 leaky=2 !'
                'omxh264dec enable-max-performance=1 enable-low-outbuffer=1 !'
                'video/x-raw(memory:NVMM) , format=(string)NV12 !'
                'nvvidconv ! video/x-raw , width=450 , height=450 , format=(string)BGRx !'
                'videorate ! video/x-raw , framerate=(fraction)10/1 !'
                'videoconvert ! '
                'appsink drop=1 sync=0 ')

video_stream = cv2.VideoCapture(gstream_elemets, cv2.CAP_GSTREAMER)

def captureFrames():
    global video_frame, thread_lock

    # Video capturing from OpenCV
    video_capture = cv2.VideoCapture(GSTREAMER_PIPELINE, cv2.CAP_GSTREAMER)

    while True and video_capture.isOpened():
        return_key, frame = video_capture.read()
        if not return_key:
            break

        # Create a copy of the frame and store it in the global variable,
        # with thread safe access
        with thread_lock:
            video_frame = frame.copy()

        key = cv2.waitKey(30) & 0xff
        if key == 27:
            break

    video_capture.release()

def encodeFrame():
    global thread_lock
    while True:
        # Acquire thread_lock to access the global video_frame object
        with thread_lock:
            global video_frame
            if video_frame is None:
                continue
            return_key, encoded_image = cv2.imencode(".jpg", video_frame)
            if not return_key:
                continue

        # Output image as a byte array
        yield(bytearray(encoded_image))

if __name__ == '__main__':

    # Create a thread and attach the method that captures the image frames, to it
    process_thread = threading.Thread(target=captureFrames)
    process_thread.daemon = True

    # Start the thread
    process_thread.start()

    # start ai that polls encodeFrame for frames here.