cv2。错误:(cv :: imshow中的(-215)size.width> 0 && size.height> 0

时间:2018-08-16 18:32:10

标签: python-3.x multithreading sockets opencv base64

我试图使用python 3中的套接字进行视频流传输。我正在使用imutils.video.FileVideoStream连续读取帧,并使用base64编码通过服务器将图像从客户端发送到另一个客户端。这是我的Send()函数:-

发送

def Send():
fvs = FileVideoStream(0).start()
time.sleep(1.0)
while True:
    frame = fvs.read()

    # cvImage = cv2.cvtColor(np.array(frame), cv2.COLOR_BGR2RGB)
    # pil = Image.fromarray(cvImage)
    # b = io.BytesIO()
    # pil.save(b, 'jpeg')
    # imgBytes = b.getvalue()

    encoded, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)

    length = str(len(jpg_as_text))
    # print("client is sending...")
    client.sendall(length.encode(encoding='utf_8'))
    client.sendall(jpg_as_text)

接收

def Recieve():
print("Client is recieving...")
while True:
    length = int(client.recv(2 * BufferSize).decode("utf-8"))
    print("length:- {}".format(length))
    data = b''
    while True:
        if len(data) < length:
            to_read = length - len(data)
            data += client.recv(BufferSize if to_read > BufferSize else to_read)
        else:
            break

    # pilBytes = io.BytesIO(data)
    # pilBytes.seek(0)
    # pilImage = Image.open(pilBytes)
    # cvImage = cv2.cvtColor(np.array(pilImage), cv2.COLOR_RGB2BGR)
    # cv2.imshow("Video", np.array(pilImage))
    print("data length:- {}".format(len(data)))
    img = base64.b64decode(data)
    npimg = np.fromstring(img, dtype=np.uint8)
    source = cv2.imdecode(npimg, 1)
    cv2.imshow("Stream", source)
    cv2.waitKey(1)

最后这是server.py的主要部分

服务器

def ClientConnection(client):
length = int(client.recv(2 * BufferSize).decode("utf-8"))
SendLength(client, str(length))
while True:
    # print("In client Connections...")
    receivingBuffer = client.recv(BufferSize)
    if not receivingBuffer:
        break
    data = b''
    if len(data) < length:

        to_read = length - len(data)
        data = client.recv(BufferSize if to_read > BufferSize else to_read)
        length -= len(data)
    broadcast(client, data)

请帮我解决问题。预先感谢。

编辑

我在“ Recieve()”中打印了每个变量,即img,npimg和source,所有这些都可以了,除了source变量,后者为None,当我尝试从我的本地主机上的字节,所有变量都将具有相似的尺寸和类型,除了源变量。源变量显示为“无”。请帮帮我。

再次编辑 这是对服务器端进行一些更改后的代码:-

服务器

from socket import socket, AF_INET, SOCK_STREAM   
from threading import Thread

HOST = "192.168.43.215"
PORT = 4000
BufferSize = 4096
addresses = {}

def Connections():
while True:
    client, addr = server.accept()
    print("{} is connected!!".format(addr))
    addresses[client] = addr
    Thread(target=ClientConnection, args=(client, )).start()

def ClientConnection(client):
length = int(client.recv(BufferSize).decode("utf-8"))
SendLength(client, str(length))
data = b''
while length > BufferSize:
    length -= BufferSize
    data += client.recv(BufferSize)
data += client.recv(length)
broadcast (client,data)
print (length)
print("Frame sent ")

def SendLength(clientSocket, length):
for client in addresses:
    if client != clientSocket:
        print("sending length:- {}".format(length))
        client.send(length.encode("utf-8"))

def broadcast(clientSocket, data_to_be_sent):
for client in addresses:
    if client != clientSocket:
        print("Broadcasting...")
        client.sendall(data_to_be_sent)
server = socket(family=AF_INET, type=SOCK_STREAM)
try:
    server.bind((HOST, PORT))
except OSError:
    print("Server Busy")

server.listen(2)
print("Waiting for connection..")
AcceptThread = Thread(target=Connections)
AcceptThread.start()
AcceptThread.join()

客户

import cv2
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
import base64
import numpy as np
import time

def Recieve():
print("Client is recieving...")
while True:
    length = int(client.recv(2 * BufferSize).decode("utf-8"))
    print("length:- {}".format(length))
    data = b''
    while True:
        if len(data) < length:
            to_read = length - len(data)
            data += client.recv(BufferSize if to_read > BufferSize else to_read)
        else:
            break

    print("data length:- {}".format(len(data)))
    img = base64.b64decode(data)
    npimg = np.fromstring(img, dtype=np.uint8)
    source = cv2.imdecode(npimg, 1)
    cv2.imshow("Stream", source)
    cv2.waitKey(1)

def Send():
cap = cv2.VideoCapture(0)
time.sleep(1.0)
while True:
    ret, frame = cap.read()

    jpg_as_text = base64.b64encode(frame)

    length = str(len(jpg_as_text))
    client.sendall(length.encode(encoding='utf_8'))
    client.sendall(jpg_as_text)

最后启动线程:-

RecieveThread = Thread(target=Recieve).start() 
SendThread = Thread(target=Send).start()
print("Waiting for connection..")
AcceptThread = Thread(target=Connections)
AcceptThread.start()
AcceptThread.join()

0 个答案:

没有答案