-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
120 lines (106 loc) · 3.05 KB
/
camera.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Camera testing with the beaglebone black and Adafruit JPEG camera.
Code from bradsmc.blogspot.com/2013/05/adafruit-ttl-serial-jpeg-camera.html
Usage:
camera.py [--outdir=/home/test]
Options:
--outdir: where to save output files (defaults to /tmp)
"""
import os
import serial
import time
import Adafruit_BBIO.UART as UART
import Adafruit_BBIO.GPIO as GPIO
from docopt import docopt
# Initialize serial port, switch and LED.
UART.setup("UART1")
ser = serial.Serial("/dev/ttyO1", baudrate=38400)
GPIO.setup("P8_10", GPIO.OUT)
GPIO.setup("P8_12", GPIO.IN)
def take_photo():
"""Takes a photo with the TTL camera."""
print 'Taking photo..'
# Initialize camera.
ser.write(b'\x56\x00\x26\x00')
resp = ""
time.sleep(1)
while ser.inWaiting() > 0:
data = ser.read()
resp += data
if "Init end\r\n" in resp:
print "Ready"
break
# Set image size to 640 x 480.
ser.write(b'\x56\x00\x54\x01\x00')
resp = ""
time.sleep(1)
while ser.inWaiting() > 0:
data = ser.read()
resp += data
if b'\x76\x00\x54\x00\x00' in resp:
print "Size set"
break
# Take picture.
ser.write(b'\x56\x00\x36\x01\x00')
resp = ""
time.sleep(2)
while ser.inWaiting() > 0:
data = ser.read()
resp += data
if b'\x76\x00\x36\x00\x00' in resp:
print "Picture taken"
break
def save_photo(outdir='/tmp'):
"""Writes image to file. We'll use integer filenames and won't overwrite.
kwargs:
outdir: where to save the output file.
"""
# Get JPG size.
ser.write(b'\x56\x00\x34\x01\x00')
resp = ""
time.sleep(1)
while ser.inWaiting() > 0:
data = ser.read()
resp += data
if b'\x76\x00\x34\x00\x04\x00\x00' in resp:
msb = ser.read()
lsb = ser.read()
print "Image file size: %d bytes" % (ord(msb) << 8 | ord(lsb))
ser.write(
b'\x56\x00\x32\x0C\x00\x0A\x00\x00\x00\x00\x00\x00%c%c\x00\x0A' % (
msb, lsb))
time.sleep(10)
# Create outdir if it doesn't exist.
if not os.path.exists(outdir):
os.makedirs(outdir)
# Find any existing image files in the outdir.
files = [f for f in os.listdir(outdir) if '.jpg' in f]
numbers = [int(f.strip('.jpg')) for f in files]
numbers = sorted(numbers)
if not numbers:
filename = '0.jpg'
else:
filename = '%s.jpg' % (numbers[-1] + 1)
filepath = os.path.join(outdir, filename)
resp = ser.read(size=5)
if b'\x76\x00\x32\x00\x00' in resp:
with open(filepath, 'wb') as f:
while ser.inWaiting() > 0:
data = ser.read()
f.write('%c' % data)
print "Image written to %s" % filepath
if __name__ == '__main__':
args = docopt(__doc__)
GPIO.output('P8_10', GPIO.HIGH)
# If the switch is pressed, turn off the LED and take a photo.
old_switch_state = 0
while True:
new_switch_state = GPIO.input('P8_12')
if new_switch_state == 1 and old_switch_state == 0:
GPIO.output('P8_10', GPIO.LOW)
take_photo()
if not args['--outdir']:
save_photo()
else:
save_photo(outdir=args['--outdir'])
GPIO.output('P8_10', GPIO.HIGH)
old_switch_state = new_switch_state