使用opencv读取vlc生成的rtsp视频流

使用opencv读取vlc生成的rtsp视频流

视频读取封装为一个方法,在主函数中创建线程并调用。

demo.py

import cv2
from lib import start
import threading

if __name__ == '__main__':
    image_list = []
    t = threading.Thread(target=start, args=(image_list, ))
    t.start()
    while True:
        if len(image_list) < 11:
            if len(image_list) > 0:
                print(len(image_list))
            continue

        image = image_list[10]
        cv2.imshow('image', image)
        cv2.waitKey(1)

lib.py

import vlc
import ctypes
import cv2
import time
import numpy as np
from PIL import Image


def start(image_list):
    vlcInstance = vlc.Instance()

    m = vlcInstance.media_new("rtsp://172.18.197.185:8554/vlc")

    mp = vlc.libvlc_media_player_new_from_media(m)

    VIDEOWIDTH = 1920
    VIDEOHEIGHT = 1080

    size = VIDEOWIDTH * VIDEOHEIGHT * 4
    buf = (ctypes.c_ubyte * size)()
    buf_p = ctypes.cast(buf, ctypes.c_void_p)

    VideoLockCb = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p))

    @VideoLockCb
    def _lockcb(opaque, planes):
        # print("lock", file=sys.stderr)
        planes[0] = buf_p

    @vlc.CallbackDecorators.VideoDisplayCb
    def _display(opaque, picture):
        img = Image.frombuffer("RGBA", (VIDEOWIDTH, VIDEOHEIGHT), buf, "raw", "BGRA", 0, 1)
        opencvImage = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
        if len(image_list) >= 20:
            del image_list[0]
        image_list.append(opencvImage)

    vlc.libvlc_video_set_callbacks(mp, _lockcb, None, _display, None)
    mp.video_set_format("BGRA", VIDEOWIDTH, VIDEOHEIGHT, VIDEOWIDTH * 4)
    while True:
        mp.play()
        time.sleep(1)

你可能感兴趣的:(科研,python,opencv,vlc,rtsp)