通过UDP发送腌制数据,UnicodeDecodeError失败了:' ascii'编解码器可以在位置0:序列不在范围内(128)中解释字节0xff
这是我的代码片段,它将通过以太网从一台PC发送到另一台PC的数据效果很好。
import cv2, socket, pickle
import numpy as np
while True:
ret, photo = cap.read()
ret, buffer = cv2.imencode(".jpg",photo, [int(cv2.IMWRITE_JPEG_QUALITY),30])
x_as_bytes = pickle.dumps(buffer)
socket.sendto((x_as_bytes),(server_ip,server_port))
在接收器端,我无法解码它。它说:
Traceback (most recent call last):
File "receive.py", line 12, in <module>
data=pickle.loads(data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
这是接收器端的片段
while True:
x=s.recvfrom(1000000)
clientip = x[1][0]
data=x[0]
print(data)
data=pickle.loads(data) #ERROR IN THIS LINE
print(type(data))
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
cv2.imshow('server', data)
if cv2.waitKey(10) == 13:
break
This is my code snippet that sends data over ethernet from one PC to another which is working fine.
import cv2, socket, pickle
import numpy as np
while True:
ret, photo = cap.read()
ret, buffer = cv2.imencode(".jpg",photo, [int(cv2.IMWRITE_JPEG_QUALITY),30])
x_as_bytes = pickle.dumps(buffer)
socket.sendto((x_as_bytes),(server_ip,server_port))
At the receiver end, I am not able to decode it. It says:
Traceback (most recent call last):
File "receive.py", line 12, in <module>
data=pickle.loads(data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
This is the snippet at the receiver end
while True:
x=s.recvfrom(1000000)
clientip = x[1][0]
data=x[0]
print(data)
data=pickle.loads(data) #ERROR IN THIS LINE
print(type(data))
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
cv2.imshow('server', data)
if cv2.waitKey(10) == 13:
break
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么还要腌制数据?无论如何,您都可以通过UDP发送二进制数据:
然后在接收端:
Why are you even pickling the data? You can send binary data via UDP anyway:
Then at the receiving end: