如何用 UnicodeDecodeError 解决这个问题?

How to solve this issue with UnicodeDecodeError?

我有以下代码尝试使用 PyZMQ 将预测结果从一台机器发送到另一台机器。


观看者:

context = zmq.Context()
footage_socket = context.socket(zmq.SUB)
footage_socket.bind('tcp://*:5555')
footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))

while True:
    frame = footage_socket.recv_string()
    img = base64.b64decode( frame )
    predictions = m1.predict(img)
    print(predictions)

footage_socket.close()

主播:

context = zmq.Context()
footage_socket = context.socket(zmq.PUB)
footage_socket.connect('tcp://localhost:5555')
videoFile = "D:/testing.mp4"
camera = cv2.VideoCapture(videoFile)
count = 0

while True:
    grabbed, frame = camera.read()
    count += 1
    print (count)

    try:
        frame = cv2.resize( frame, (224, 224) )

    except cv2.error:
        break

    image= img_to_array(frame)
    image=image.reshape( ( 1,
                           image.shape[0],
                           image.shape[1],
                           image.shape[2]
                           )
                         )
    image=preprocess_input(image)
    preds=model.predict(image)
    footage_socket.send(preds)
footage_socket.close()

我收到以下错误:

frame = footage_socket.recv_string()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc6 in position 4:
invalid continuation byte

非常感谢帮助。

您正在发送二进制数据,但随后尝试使用 recv_string().

将其作为 Unicode 字符串读取

您需要使用footage_socket.recv()