加速节目视频流

时间:2016-11-16 22:34:54

标签: python performance sockets opencv video

我正在编写服务器 - 客户端视频流,服务器获取客户端摄像头捕获的视频。

我的程序正在运行,但我的问题是视频非常慢,有9 +秒的延迟,我想知道如何加快速度以获得更好的性能?

这是我的服务器:

import socket
import cv2
import pickle
import struct

HOST = '0.0.0.0'
PORT = 8765

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

s.bind((HOST, PORT))
print 'Socket bind complete'
s.listen(1)
print 'Socket now listening'

conn, addr = s.accept()
data = ""
payload_size = struct.calcsize("L")
while True:
    while len(data) < payload_size:
        data += conn.recv(131072)
    packed_msg_size = data[:payload_size]

    data = data[payload_size:]
    msg_size = struct.unpack("L", packed_msg_size)[0]

    while len(data) < msg_size:
        data += conn.recv(131072)
    frame_data = data[:msg_size]
    data = data[msg_size:]

    frame=pickle.loads(frame_data)
    #print frame.size
    cv2.imshow('frame', frame)
    cv2.waitKey(1)

这是我的客户:

import cv2
import socket
import pickle
import struct

cap = cv2.VideoCapture(0)
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8765))

while True:
    ret,frame = cap.read()
    data = pickle.dumps(frame)
    clientsocket.sendall(struct.pack("L", len(data)) + data)

感谢您的帮助!

0 个答案:

没有答案