Python Opencv和套接字-h264编码的流式视频
因此,我试图制作一个流光器,将视频从一台计算机传输到我的局域网上的另一台计算机(或者现在是同一台计算机)。我需要它使用尽可能少的带宽,所以我试图在h264编码。我很难做到这一点,我真的不知道从哪里开始。现在它是用jpg编码的,并且一帧一帧地发送。然而,我知道这是非常低效的,并且会消耗大量带宽。这是我当前的接收代码Python Opencv和套接字-h264编码的流式视频,python,sockets,opencv,encode,h.264,Python,Sockets,Opencv,Encode,H.264,因此,我试图制作一个流光器,将视频从一台计算机传输到我的局域网上的另一台计算机(或者现在是同一台计算机)。我需要它使用尽可能少的带宽,所以我试图在h264编码。我很难做到这一点,我真的不知道从哪里开始。现在它是用jpg编码的,并且一帧一帧地发送。然而,我知道这是非常低效的,并且会消耗大量带宽。这是我当前的接收代码 import cv2 import socket import _pickle import time host = "192.168.1.196" port = 25565 boo
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
这是我当前的客户代码(发件人):
我只是需要一些关于如何在h264中编码和解码视频的帮助 您可以使用和base64字符串编码/解码模式来完成此操作。在服务器端,想法是:
- 从摄影机流获取帧
- 使用
cv2.imencode从内存缓冲区读取图像
- 使用base64将
转换为ndarray
,并通过套接字发送str
- 从套接字读取图像字符串
- 使用base64将
转换为str
字节
- 使用
+np.frombuffer
cv2.imdecode
服务器
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
谢谢我明天会试试这个,我会告诉你进展如何。我有一个最大上限为4Mbit/s的流编码器(出于某些原因…规则…),希望它能很好地工作。谢谢,我继续使用套接字模块而不是ZeroMQ,并用base64编码,但我仍在使用9mbps。我需要它小于4Mbps。知道如何在h.264中对视频进行编码吗?在发送之前是否会缩小帧的大小以减少带宽?我不知道如何用h.264编码视频很巧合,我就是这么做的,并将其灰度化,现在我非常感谢你的帮助,伙计。
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