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
Advertisement
Answer
Why are you even pickling the data? You can send binary data via UDP anyway:
# Synthetic image im = np.random.randint(0, 256,(480, 640, 3), dtype=np.uint8) # JPEG encode ret, buffer = cv2.imencode(".jpg", im, [int(cv2.IMWRITE_JPEG_QUALITY),30]) # Send socket.sendto(buffer.tobytes(), (server_ip,server_port))
Then at the receiving end:
JPEG = s.recvfrom(1000000) im = cv2.imdecode(np.frombuffer(JPEG, dtype=np.uint8), cv2.IMREAD_UNCHANGED)