套接字流 - Python(剪切部分消息)

时间:2016-11-12 15:34:29

标签: python sockets opencv numpy

  • 我尝试做什么:录制视频,然后使用Socket Stream将视频从一台PC发送到另一台PC。为此,我使用OpenCV 库CV2,NumPy和Socket。

  • 问题:当我发送帧时,它只发送整个部分(921600中的43776),之后发生错误,当我发送时 尝试在另一台电脑上显示框架。

服务器:

    #!/usr/bin/python

import socket
import cv2
import numpy as np

ip = "XXX.XXX.X.XXX"

def Test(Given_List):
    y = 0
    temp = []
    Frame_List = []

    for kappa in range(480):
        Frame_List.append([])

    for each in Given_List:

        if len(temp) < 3:
            temp.append(each)

        if len(temp) >= 3:
            Frame_List[y].append(temp)
            temp = []

        if len(Frame_List[y]) >= 640:
            y += 1
    return Frame_List


while True:
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((ip,50000))
    data = client_socket.recv(10240000)
    s = np.fromstring(data, dtype=np.uint8)
    nice = np.asarray(Test(s))
    cv2.imshow('frame', nice)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

客户:

#!/usr/bin/python

import socket
import cv2

#Receiver ip
ip = "XXX.XXX.X.XXX"
port = 50000

#Set up socket and stuff 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind(("XXX.XXX.X.XXX", port))
server.listen(1)

#Define the cam stuff
cap = cv2.VideoCapture(0)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)

while(cap.isOpened()):
    s, add = server.accept()
    ret, frame = cap.read()
    n = frame.tobytes()

    if ret:
        s.sendall(n)
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
s.close()

1 个答案:

答案 0 :(得分:0)

似乎数据量超过套接字的最大数量,因此我将视频分辨率更改为320x240,现在它可以正常工作。