0

I am trying to send video from raspberry pi to my laptop via laptop and save them as pictures so i found the below code online but I get the following errors when I run them so i run this client code on the pi using Thonny ide that comes preloaded , I apologize for the way code is formatted below and would be very grateful if anybody can help me sort this out

Server on the laptop is run using python 3.6 idle

import sys
import numpy as np
import cv2
import socket


class VideoStreamingTest(object):
def __init__(self):

    self.server_socket = socket.socket()
    self.server_socket.bind(('0.0.0.0', 9006))
    self.server_socket.listen(0)
    self.connection, self.client_address = self.server_socket.accept()
    self.connection = self.connection.makefile('rb')
    self.streaming()

def streaming(self):

    try:
        print("Connection from: ", self.client_address)
        print("Streaming...")
        print("Press 'q' to exit")

        stream_bytes = ' '
        while True:

            stream_bytes += self.connection.read(1024)
            first = stream_bytes.find('\xff\xd8')
            last = stream_bytes.find('\xff\xd9')
            if first != -1 and last != -1:
                jpg = stream_bytes[first:last + 2]
                stream_bytes = stream_bytes[last + 2:]
                #image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE)
                image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
                cv2.imshow('image', image)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
    finally:
        self.connection.close()
        self.server_socket.close()

if __name__ == '__main__':
VideoStreamingTest()

I get the following error

 Connection from:  ('192.168.43.3', 47518)
 Streaming...
 Press 'q' to exit
 Traceback (most recent call last):
 File "C:\Users\John Doe\d-ff\Desktop\AutoRCCar-master 
 3\test\stream_server_test.py", line 46, in <module>
 VideoStreamingTest()
 File "C:\Users\John Doe\d-ff\Desktop\AutoRCCar-master  
 3\test\stream_server_test.py", line 16, in __init__
 self.streaming()
 File "C:\Users\John Doe\d-ff\Desktop\AutoRCCar-master 
 3\test\stream_server_test.py", line 28, in streaming
 stream_bytes += self.connection.read(1024)
 TypeError: must be str, not bytes

Client side on the pi

     import io
     import socket
     import struct
     import time
     import picamera

    # create socket and bind host
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(('ToM', 9006))
    connection = client_socket.makefile('wb')

   try:
    with picamera.PiCamera() as camera:
    camera.resolution = (320, 240)      # pi camera resolution
    camera.framerate = 5               # 10 frames/sec
    time.sleep(2)                       # give 2 secs for camera to initilize
    start = time.time()
    stream = io.BytesIO()

    # send jpeg format video stream
    for foo in camera.capture_continuous(stream, 'jpeg', use_video_port = True):
        connection.write(struct.pack('<L', stream.tell()))
        connection.flush()
        stream.seek(0)
        connection.write(stream.read())
        if time.time() - start > 600:
            break
        stream.seek(0)
        stream.truncate()
connection.write(struct.pack('<L', 0))
finally:
connection.close()
client_socket.close()

I get the following error

 During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/Desktop/stream_client.py", line 40, in <module>
connection.close()
File "/usr/lib/python3.5/socket.py", line 594, in write
return self._sock.send(b)
BrokenPipeError: [Errno 32] Broken pipe

I first thought it might be because of the limited bandwidth since i was running vnc viewer (remote desktop) via wifi on the pi but I don't think it is

2 Answers 2

1

I also had same problem. After some searching I found solution. In python 3 we have to specify whether string is regular string or binary.Thats why we use b'string' instead of just 'string'

Change

stream_bytes = ' '

to

stream_bytes = b' '

Also change

first = stream_bytes.find('\xff\xd8') last = stream_bytes.find('\xff\xd9')

to

first = stream_bytes.find(b'\xff\xd8') last = stream_bytes.find(b'\xff\xd9')

Note that you are using cv2.CV_LOAD_IMAGE_UNCHANGED which is not available in opencv3.0 Use cv2.IMREAD_COLOR to show image in color.

Edit these changes and your stream should run smoothly.

Sign up to request clarification or add additional context in comments.

Comments

0
connection.write(struct.pack('<L', 0))

Check out by inserting the above within try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.