Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Created August 3, 2016 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigsnarfdude/5c1456af12577584ecb900abaa48497c to your computer and use it in GitHub Desktop.
Save bigsnarfdude/5c1456af12577584ecb900abaa48497c to your computer and use it in GitHub Desktop.
[self-drving-car] stream camera pi to laptop via sockets
"""
Reference:
PiCamera documentation
https://picamera.readthedocs.org/en/release-1.10/recipes2.html
https://github.com/hamuchiwa/AutoRCCar
"""
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(('192.168.1.72', 8000))
connection = client_socket.makefile('wb')
try:
with picamera.PiCamera() as camera:
camera.resolution = (320, 240) # pi camera resolution
camera.framerate = 10 # 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment