-
Notifications
You must be signed in to change notification settings - Fork 3
/
raspicam.py
79 lines (69 loc) · 2.35 KB
/
raspicam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Raspberry Pi Camera Script
Uses picamera library to take pictures from the Raspberry Pi Camera
Outputs individual snapshots to a stream
Set the stream-to location with --server <ADDRESS>
"""
import json
import os
import io
import socket
import struct
import time
import picamera
from optparse import OptionParser
#constants
CAMERA_WIDTH = 320
CAMERA_HEIGHT = 240
#general
server_address = '127.0.0.1'
server_port = 8000
#command line arguments
parser = OptionParser()
parser.add_option("-s", "--server", dest="server",
help="address of the server to send picamera image to")
parser.add_option("-p", "--port", dest="port",
help="port socket will use")
parser.add_option("-x", "--width", dest="width",
help="width of camera resolution")
parser.add_option("-y", "--height", dest="height",
help="height of camera resolution")
(options, args) = parser.parse_args()
#server address
if options.server:
server_address = options.server
if options.port:
server_port = options.port
#adjust camera resolution
if options.width:
CAMERA_WIDTH = options.width
if options.height:
CAMERA_HEIGHT = options.height
#connect a client socket to server
client_socket = socket.socket()
client_socket.connect((server_address, server_port))
#file object to be sent over connection
connection = client_socket.makefile('wb')
#capture_continuous actually drops after a period of time, so do again in that instance
while True:
with picamera.PiCamera() as camera:
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT)
start = time.time()
stream = io.BytesIO()
for foo in camera.capture_continuous(stream, 'jpeg'):
# Write the length of the capture to the stream and flush to
# ensure it actually gets sent
connection.write(struct.pack('<L', stream.tell()))
connection.flush()
# Rewind the stream and send the image data over the wire
stream.seek(0)
connection.write(stream.read())
# If we've been capturing for more than 30 seconds, quit
if time.time() - start > 30:
break
# Reset the stream for the next capture
stream.seek(0)
stream.truncate()
# Write a length of zero to the stream to signal we're done
connection.write(struct.pack('<L', 0))
connection.close()
client_socket.close()