Created
August 3, 2016 02:00
-
-
Save bigsnarfdude/5c1456af12577584ecb900abaa48497c to your computer and use it in GitHub Desktop.
[self-drving-car] stream camera pi to laptop via sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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