【问题标题】:How to solve this issue with UnicodeDecodeError?如何用 UnicodeDecodeError 解决这个问题?
【发布时间】:2020-11-10 16:38:17
【问题描述】:

我有以下代码尝试使用 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

非常感谢您的帮助。

【问题讨论】:

    标签: python python-3.x sockets keras pyzmq


    【解决方案1】:

    您正在发送二进制数据,但随后尝试将其读取为带有 recv_string() 的 Unicode 字符串。

    您需要使用footage_socket.recv()

    【讨论】:

    • 嗨,@rveerd,我真的很感谢你的大力帮助,我花了很多时间才弄清楚这一点,但你真的节省了我的时间。
    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 2020-04-27
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多