0

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

1 Answers1

0

For your first error, you are defining data as an empty text string (data = "") and then trying to concatenate it with bytes.

Instead try to define data as an empty bytes string (data = b"") and remove all .decode() calls from the incoming data as to keep all your data in bytes.

foglerit
  • 7,792
  • 8
  • 44
  • 64
  • i tried that but it returned a error saying "struct.error: unpack requires a buffer of 8 bytes" at line 63 on server.py(i haven't used struct a lot) –  Mar 01 '20 at 02:07
  • For that secondary error, you probably need to chage `payload_size = struct.calcsize("H")` to use `"L"`, since that's what you're actually sending and decoding. – Blckknght Mar 01 '20 at 02:19