视频流推送

降低延迟、内存溢出。

import cv2
import queue
import time
import threading

q = queue.Queue()


def img_resize(image):
    height, width = image.shape[0], image.shape[1]
    # 设置新的图片分辨率框架 640x369 1280×720 1920×1080
    width_new = 1920
    height_new = 1080
    # 判断图片的长宽比率
    if width / height >= width_new / height_new:
        img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
    else:
        img_new = cv2.resize(image, (int(width * height_new / height), height_new))
    return img_new


def receive():
    print("start Reveive")
    url = 'rtsp://admin:Bestpower8866@192.168.4.64/Streaming/Channels/1'
    cap = cv2.VideoCapture(url)
    while cap.isOpened():
        q.put(cap.read())
        q.get() if q.qsize() > 1 else time.sleep(0.01)


def display():
    print("Start Displaying")
    while True:

        ret, frame = q.get()
        # 对获取的视频帧分辨率重处理
        img_new = img_resize(frame)
        cv2.imshow("window_name", img_new)
        del ret
        del frame
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()


if __name__ == '__main__':
    p1 = threading.Thread(target=receive)
    p2 = threading.Thread(target=display)
    p1.start()
    p2.start()

参考:
https://lukeyalvin.blog.csdn.net/article/details/116052377

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值