【问题标题】:Python Opencv and Sockets - Streaming video encoded in h264Python Opencv 和 Sockets - 以 h264 编码的流式视频
【发布时间】:2019-12-03 23:51:05
【问题描述】:

所以我正在尝试制作一个流媒体,将视频从一台计算机流式传输到我的 LAN 上的另一台计算机(或同一台计算机,目前)。我需要它使用尽可能少的带宽,所以我试图在 h264 中编码。我在做这件事时遇到了麻烦,我真的不知道从哪里开始。现在它是用jpg编码的,它是逐帧发送的。但是,我知道这是非常低效的,并且会消耗大量带宽。这是我当前的接收器代码。

import cv2
import socket
import _pickle
import time

host = "192.168.1.196"
port = 25565
boo = True

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # declares s object with two parameters
s.bind((host, port)) # tells my socket object to connect to this host & port "binds it to it"
s.listen(10) # tells the socket how much data it will be receiving.

conn, addr = s.accept()
buf = ''
while boo:
        pictures = conn.recv(128000) # creates a pictures variable that receives the pictures with a max amount of 128000 data it can receive
        decoded = _pickle.loads(pictures) # decodes the pictures
        frame = cv2.imdecode(decoded, cv2.IMREAD_COLOR) # translates decoded into frames that we can see!
        cv2.imshow("recv", frame)
        if cv2.waitKey(1) & 0xFF == ord("q"):  # wait until q key was pressed once and
            break

这是我当前的客户端代码(发件人):

import cv2
import numpy as np
import socket
import _pickle
from cv2 import *

host = "192.168.1.196"
port = 25565

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # declares s object with two parameters
s.connect((host, port))  # connects to the host & port
cap = cv2.VideoCapture(1)
cv2.cv.CV_FOURCC('H','2','6','4')
while cap.isOpened(): # while camera is being used
    ret, frame = cap.read()  # reads each frame from webcam
    cv2.imshow("client", frame)
    if ret:
        encoded = _pickle.dumps(cv2.imencode("fourcc", frame)[1]) # encoding each frame, instead of sending live video it is sending pictures one by one
        s.sendall(encoded)
    if cv2.waitKey(1) & 0xFF == ord("q"): # wait until key was pressed once and
        break
cap.release()
cv2.destroyAllWindows()

我只需要一些关于如何对视频进行编码和在 h264 中解码的帮助。

【问题讨论】:

    标签: python sockets opencv encode h.264


    【解决方案1】:

    您可以使用 pyzmq 和带有 base64 字符串编码/解码的 publish/subscribe 模式来完成此操作。在服务器端,想法是:

    • 从相机流中获取帧
    • 使用cv2.imencode从内存缓冲区读取图像
    • 使用 base64 将 ndarray 转换为 str 并通过套接字发送

    在客户端,我们只是简单地反转这个过程:

    • 从套接字读取图像字符串
    • 使用 base64 将 str 转换为 bytes
    • 使用np.frombuffer + cv2.imdecodebytes 转换为ndarray

    此方法不应使用太多带宽,因为它仅通过套接字发送字符串。


    服务器

    import base64
    import cv2
    import zmq
    
    context = zmq.Context()
    socket = context.socket(zmq.PUB)
    socket.connect('tcp://localhost:7777')
    
    camera = cv2.VideoCapture(0)
    
    while True:
        try:
            ret, frame = camera.read()
            frame = cv2.resize(frame, (640, 480))
            encoded, buf = cv2.imencode('.jpg', frame)
            image = base64.b64encode(buf)
            socket.send(image)
        except KeyboardInterrupt:
            camera.release()
            cv2.destroyAllWindows()
            break
    

    客户

    import cv2
    import zmq
    import base64
    import numpy as np
    
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.bind('tcp://*:7777')
    socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
    
    while True:
        try:
            image_string = socket.recv_string()
            raw_image = base64.b64decode(image_string)
            image = np.frombuffer(raw_image, dtype=np.uint8)
            frame = cv2.imdecode(image, 1)
            cv2.imshow("frame", frame)
            cv2.waitKey(1)
        except KeyboardInterrupt:
            cv2.destroyAllWindows()
            break
    

    【讨论】:

    • 谢谢。明天我会试试这个,我会让你知道它是怎么回事。我有这个流编码器的最大上限为 4 mbit/s(出于某种原因......规则......),希望它运作良好。
    • 谢谢,我继续使用 socket 模块而不是 ZeroMQ,并用 base64 编码,但我仍然在做 9mbps。我需要它小于 4 mbps。关于如何在 h.264 中编码视频的任何想法?
    • 在发送帧之前是否将帧调整得更小以减少带宽?我不确定如何在 h.264 中编码视频
    • 很巧合,我就是这样做的,并对其进行了灰度化,现在我得到了
    • 非常感谢您的帮助。
    猜你喜欢
    • 2015-02-23
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多