mityuxuan
小白行天涯
2021-01-26 22:18

type 'numpy.ndarray' to 'str' for 'ext'

  • python
import socket
import struct
import numpy as np
import cv2

bufSize = 1024

client = None

HOST = "127.0.0.1"

PORT = 9999

img = None

cv2.namedWindow("Main")


def run():
    global client, img
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((HOST, PORT))
    length = client.recv(4)
    le = struct.unpack(">I", length)[0]
    imb = b''
    while le > bufSize:
        t = client.recv(bufSize)
        imb += t
        le -= len(t)
    while le > 0:
        t = client.recv(le)
        imb += t
        le -= len(t)
    data = np.frombuffer(imb, dtype=np.uint8)
    img = cv2.imencode(data, cv2.IMREAD_COLOR)
    while True:
        cv2.imshow("Main", cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        length = client.recv(4)
        le = struct.unpack(">I", length)[0]
        imb = b''
        while le > bufSize:
            t = client.recv(bufSize)
            imb += t
            le -= len(t)
        while le > 0:
            t = client.recv(le)
            imb += t
            le -= len(t)
        data = np.frombuffer(imb, dtype=np.uint8)
        ims = cv2.imencode(data, cv2.IMREAD_COLOR)
        img = img + ims
        cv2.waitKey(90)


run()
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答