Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hamuchiwa authored Aug 10, 2018
1 parent 769732c commit b7392f0
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions raspberryPi/stream_client_fast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import io
import socket
import struct
import time
import picamera
import sys

class SplitFrames(object):
def __init__(self, connection):
self.connection = connection
self.stream = io.BytesIO()
self.count = 0

def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# Start of new frame; send the old one's length
# then the data
size = self.stream.tell()
if size > 0:
self.connection.write(struct.pack('<L', size))
self.connection.flush()
self.stream.seek(0)
self.connection.write(self.stream.read(size))
self.count += 1
self.stream.seek(0)
self.stream.write(buf)

my_server = '192.168.1.100'
res = (320, 240)
client_socket = socket.socket()
client_socket.connect((my_server, 8000))
connection = client_socket.makefile('wb')
try:
output = SplitFrames(connection)
with picamera.PiCamera(resolution=res, framerate=30) as camera:
time.sleep(2)
start = time.time()
camera.start_recording(output, format='mjpeg')
camera.wait_recording(sys.maxint)
camera.stop_recording()
# Write the terminating 0-length to the connection to let the
# server know we're done
connection.write(struct.pack('<L', 0))
finally:
finish = time.time()
print('Sent %d images in %d seconds at %.2ffps' % (
output.count, finish-start, output.count / (finish-start)))
connection.close()
client_socket.close()

0 comments on commit b7392f0

Please sign in to comment.