I'm trying to send frames from a raspberry pi to different PC. I came across some python2 code but I change it to python3. then this error appeared. I can only concatenate strings with bytes, so I tried to change the bytes to a string(utf-8). but it said that there were chars that weren't in utf-8... How do i solve this? it seems that the errors contradict themselves... shouldn't i turn it into a string? (mind you I googled the error and it said I'm trying to decode chars that don't exist but no answer was given.(original code: Sending live video frame over network in python opencv))
Server.py
PORT=8089
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print('Socket created')
s.bind((HOST,PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')
conn,addr=s.accept()
### new
data = ""
payload_size = struct.calcsize("H")
while True:
while len(data) < payload_size:
incomingData = conn.recv(4096)
data += incomingData.decode("utf-8")
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack("L", packed_msg_size)[0]
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
###
frame=pickle.loads(frame_data)
print(frame)
cv2.imshow('frame',frame)
Client.py
import cv2
import numpy as np
import socket
import sys
import pickle
import struct ### new code
cap=cv2.VideoCapture(0)
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('localhost',8089))
while True:
ret,frame=cap.read()
data = pickle.dumps(frame) ### new code
clientsocket.sendall(struct.pack("L", len(data))+data) ### new code
1st Error
File "Server.py", line 58, in <module>
data += conn.recv(4096)
TypeError: can only concatenate str (not "bytes") to str
2nd Error
File "Server.py", line 60, in <module>
data += str(conn.recv(4096), "utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 0: invalid start byte